Xamarin.Android pdf生成器 [英] Xamarin.Android pdf generator
问题描述
我最近一直在开发 Xamarin.Android
.我需要使用pdf生成器通过电子邮件发送报告.
I have been working on Xamarin.Android
recently. I need to use pdf generator to send a report via email.
I have been came across to the following blog. I do not really know what to put in the FileStream fs = new FileStream (???????);
In addition to that, I would like to open or see that pdf on the screen.
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.IO;
using XamiTextSharpLGPL;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp;
namespace PDFAapp
{
[Activity (Label = "PDFAapp", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
int count = 1;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
FileStream fs = new FileStream (???????);
Document document = new Document (PageSize.A4, 25, 25, 30, 30);
PdfWriter writer = PdfWriter.GetInstance (document, fs);
document.Add(new Paragraph("Hello World"));
document.Close();
writer.Close();
fs.Close();
}
}
}
具有以下代码以打开生成的Pdf,但它显示pdf格式无效.
Having the following code to open the generated Pdf, but it shows pdf format is not valid.
Java.IO.File file = new Java.IO.File(filePath);
Intent intent = new Intent(Intent.ActionView);
intent.SetDataAndType(Android.Net.Uri.FromFile(file), "application/pdf");
StartActivity(intent);
推荐答案
首先请确保在清单
文件中允许 WriteExternalStorage
First make sure in the Manifest
file you allow WriteExternalStorage
为了读取或写入外部存储中的文件,您的应用必须获取READ_EXTERNAL_STORAGE或WRITE_EXTERNAL_STORAGE系统权限.例如:
In order to read or write files on the external storage, your app must acquire the READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE system permissions. For example:
<manifest ...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
FileStream
构造函数(众多方法之一)接受路径的字符串和 FileMode
,因此将提供示例解决方案.
FileStream
constructor (one of the many) accepts string for a path and FileMode
so an example solution would be.
var directory = new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory, "pdf").ToString();
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
var path = Path.Combine(directory, "myTestFile.pdf");
var fs = new FileStream(path, FileMode.Create);
这将在外部存储中创建一个名为"pdf"的文件夹,然后创建pdf文件.在创建文件之前,可以包含其他代码来检查文件是否存在.
This creates folder called "pdf" in to external storage and then creates the pdf file. Additional code could be included to check if file exists before creating it..
完整的示例,刚刚在我的设备上进行了测试:
complete example, just tested on my device:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
var directory = new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory, "pdf").ToString();
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
var path = Path.Combine(directory, "myTestFile.pdf");
if (File.Exists(path))
{
File.Delete(path);
}
var fs = new FileStream(path, FileMode.Create);
Document document = new Document(PageSize.A4, 25, 25, 30, 30);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
document.Open();
document.Add(new Paragraph("Hello World"));
document.Close();
writer.Close();
fs.Close();
Java.IO.File file = new Java.IO.File(path);
Intent intent = new Intent(Intent.ActionView);
intent.SetDataAndType(Android.Net.Uri.FromFile(file), "application/pdf");
StartActivity(intent);
}
这篇关于Xamarin.Android pdf生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!