无法在 Xamarin Android 中从 HTML 创建 PDF [英] Unable to create PDF from HTML in Xamarin Android

查看:72
本文介绍了无法在 Xamarin Android 中从 HTML 创建 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Xamarin 中为 Android 开发应用程序.我已经使用 StringBuilder 生成了 HTML 文件.现在我的外部存储中有一个 HTML 文件,PDF 需要相同的模板.因此,当我尝试使用 iTextSharp using XML Worker & 将 HTML 转换为 PDF 时PDFSharp 库,由于缺少 System.Drawing.dll,我收到构建错误.然后我从 Xamarin 论坛 &Xamarin.Android 不支持的 Stackoverflow 链接.

I am developing an Application in Xamarin for Android. I have already generated HTML file using StringBuilder. Now I have a HTML file in my External storage and the same template is required for PDF. So when I try to convert HTML to PDF using iTextSharp using XML Worker & PDFSharp libraries, I am getting build errors due to missing System.Drawing.dll. Then I found from Xamarin forums & Stackoverflow links that it is not supported for Xamarin.Android.

谁能告诉我关于如何为 PDF 创建模板或为 Xamarin.Android 创建任何其他工作 nuget 包的其他替代方法,它将 html 文件转换为 pdf.

Can anyone please tell me other alternative about how to create template for PDF or any other working nuget package for Xamarin.Android which will convert html file to pdf.

注意:我可以生成 PDF,但不能将 HTML 转换为 PDF.

NOTE: I am able to generate PDF but not able to convert HTML to PDF.

这会很有帮助!.非常感谢!

It would be of great help!. Thanks a ton!.

推荐答案

使用 Nuget 包 Xam.iTextSharpLGPL

Use Nuget package Xam.iTextSharpLGPL

以下是示例代码

using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using Android.Graphics;


  string path = Android.OS.Environment.ExternalStorageDirectory.Path;
  string pdfPath = System.IO.Path.Combine(path, "samplee.pdf");
  System.IO.FileStream fs = new FileStream(pdfPath, FileMode.Create);    
  Document document = new Document(PageSize.A4);
  PdfWriter writer = PdfWriter.GetInstance(document, fs);
  HTMLWorker worker = new HTMLWorker(document);
  document.Open();
  StringBuilder html = new StringBuilder();
  html.Append("<? xml version='1.0' encoding='utf-8' ?><html><head><title></title></head>");
  html.Append("<CENTER>Simple Sample html</H1>");
  html.Append("<H4>By User1</H4>");
  html.Append("<H2>Demonstrating a few HTML features</H2>");
  html.Append("</CENTER>");
  html.Append("<p>HTML doesn't normally use line breaks for ordinary text. A white space of any size is treated as a single space. This is because the author of the page has no way of knowing the size of the reader's screen, or what size type they will have their browser set for.");
  html.Append("</p></body</html>");
  TextReader reader = new StringReader(html.ToString());
  worker.StartDocument();
  worker.Parse(reader);
  worker.EndDocument();
  worker.Close();
  document.Close();
  writer.Close();
  fs.Close();

这篇关于无法在 Xamarin Android 中从 HTML 创建 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆