我最近一直在Xamarin.Android
工作。我需要使用pdf生成器通过电子邮件发送报告。
我遇到了以下blog。我真的不知道在FileStream fs = new FileStream (???????);
放什么。除此之外,我想在屏幕上打开或看到pdf。
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();
}
}
}
编辑1:
使用以下代码打开生成的Pdf,但显示pdf格式无效。
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);
答案 0 :(得分:7)
首先确保Manifest
文件允许WriteExternalStorage
要在外部存储上读取或写入文件,您的应用必须 获取READ_EXTERNAL_STORAGE或WRITE_EXTERNAL_STORAGE系统 权限。例如:
<manifest ...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
FileStream
构造函数(其中一个)接受路径的字符串和FileMode
,因此示例解决方案就是。
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文件。可以包含附加代码以在创建文件之前检查文件是否存在..
编辑:完整示例,刚刚在我的设备上测试过:
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);
}
答案 1 :(得分:1)
如果您正在进行跨平台开发,那么它的下注者可以在commen中进行PDF格式化。请使用iTEXT sharp在PCL中查看以下有关PDF创建的代码。 iTEXT PDF
这里我使用PCL存储库进行IO访问,这对PCL非常有用。 你可以在这里找到更多信息PCL Storage
安装包PCLStorage
将iTEXT PDF添加到您的PCL项目
安装包装iTextSharp
在PCL项目中使用以下代码
public class PdfHelper
{
public async Task PCLGenaratePdf(string path)
{
IFolder rootFolder = await FileSystem.Current.GetFolderFromPathAsync(path);
IFolder folder = await rootFolder.CreateFolderAsync("folder", CreationCollisionOption.OpenIfExists);
IFile file = await folder.CreateFileAsync("file.pdf", CreationCollisionOption.ReplaceExisting);
using (var fs = await file.OpenAsync(FileAccess.ReadAndWrite))
{
var document = new Document(PageSize.A4, 25, 25, 30, 30);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
document.Open();
document.Add(new Paragraph("heloo everyone"));
document.Close();
writer.Close();
}
}
public async Task<string> PCLReadFile(string path)
{
IFile file = await FileSystem.Current.GetFileFromPathAsync(path);
return file.Path;
}
}
生成PDF
private async void Genarate(string path)
{
var creator = new PdfHelper();
await creator.PCLGenaratePdf(path);
}
使用意图阅读PDF
private async void ReadPDF(object sender, EventArgs e)
{
String sdCardPath = Environment.ExternalStorageDirectory.AbsolutePath;
String fullpath = Path.Combine(sdCardPath, "folder/" + "file.pdf");
var creator = new PdfHelper();
string filePath = await creator.PCLReadFile(fullpath);
Uri pdfPath = Uri.FromFile(new File(filePath));
Intent intent = new Intent(Intent.ActionView);
intent.SetDataAndType(pdfPath, "application/pdf");
intent.SetFlags(ActivityFlags.NewTask);
Application.Context.StartActivity(intent);
}
注意:一旦编译PCL项目,有时可能会出现system.drawings和system.security错误。解决方案是使用iTEXT sharp源代码删除所有system.drawings和system.security依赖项。
快乐编码。