c# - Xamarin.Android pdf 生成器

标签 c# android pdf xamarin filestream

我最近一直在研究 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);

最佳答案

首先确保在 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:

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

关于c# - Xamarin.Android pdf 生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33090681/

相关文章:

c# - ArgumentOutOfRangeException 出现奇怪的本地化问题

java - 关于android开发中使用java语法的问题

java - 如何避免在 Activity 中设置我所有组件的模块以注入(inject) Dagger 2 的依赖项?

image - iText - 使用 Chunk 添加外部图像

svn - 通过 apache webdav 访问 subversion 存储库时 Pdf 被破坏

c# - .NET Web API - 添加日志记录

c# - 使用枚举表示C#中的ID

c# - IEnumerable<T> 如何在后台工作

android - 如何使用 Android 读取无源 RFID 卡?

pdf - 将图形导出为 PDF 时,可以在绘图标签中导出特殊符号/西里尔字母吗?