xamarin android pdf,c# – Xamarin.Android pdf生成器

首先确保在Manifest文件中允许WriteExternalStorage

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:

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);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值