iTextSharp 从 WPF 固定文档生成 PDF

发布于 2024-09-12 14:16:15 字数 93 浏览 5 评论 0原文

我有一个简单的 WPF 应用程序,可以显示和打印一些内容 使用固定文档进行报告。

如何使用免费且开放的解决方案从中生成 PDF, 比如iTextSharp?

I have a simple WPF app that displays and prints some
reports with a FixedDocument.

How can generate PDF's from that, with a free and open solution,
such as iTextSharp?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

少钕鈤記 2024-09-19 14:16:15

WPF 固定文档(也称为 XPS 文档)是对 PDF 的明显改进。它具有 PDF 所缺乏的许多功能。在大多数情况下,最好将文档作为 XPS 而不是 PDF 分发,但有时需要从 XPS 转换为 PDF,例如,如果您需要在仅支持 PDF 的设备上打开文档。不幸的是,大多数从 XPS 转换为 PDF 的免费工具(例如 CutePDF 和 BullzipPDF)需要安装打印机驱动程序或者不是开源的。

一个好的开源解决方案是使用 GhostPDL 的“gxps”工具。 GhostPDL 是 Ghostscript 项目的一部分,并在 GPL2 下获得开源许可。

  1. http://ghostscript.com/releases/ghostpdl-8.71.tar.bz2< 下载 GhostPDL /a> 并编译它。
  2. 将 gxps.exe 可执行文件作为内容复制到项目中,并使用 Process.Start 从代码中调用它。

您的代码可能如下所示:

string pdfPath = ... // Path to place PDF file

string xpsPath = Path.GetTempPath();
using(XpsDocument doc = new XpsDocument(xpsPath, FileAccess.Write))
  XpsDocument.CreateXpsDocumentWriter(doc).Write(... content ...);

Process.Start("gxps.exe",
              "-sDEVICE=pdfwrite -sOutputFile=" +
                  pdfPath +
                  "-dNOPAUSE " +
                  xpsPath).WaitForExit();

// Now the PDF file is found at pdfPath

A WPF FixedDocument, also known as an XPS document, is a definite improvement over PDF. It has many capabilities that PDF lacks. In most cases it is better to distribute your document as XPS rather than PDF, but sometimes it is necessary to convert from XPS to PDF, for example if you need to open the document on devices that have only PDF support. Unfortunately most free tools to convert from XPS to PDF, such as CutePDF and BullzipPDF, require installing a printer driver or are not open source.

A good open-source solution is to use the "gxps" tool that is part of GhostPDL. GhostPDL is part of the Ghostscript project and is open-source licensed under GPL2.

  1. Download GhostPDL from http://ghostscript.com/releases/ghostpdl-8.71.tar.bz2 and compile it.
  2. Copy the gxps.exe executable into your project as Content and call it from your code using Process.Start.

Your code might look like this:

string pdfPath = ... // Path to place PDF file

string xpsPath = Path.GetTempPath();
using(XpsDocument doc = new XpsDocument(xpsPath, FileAccess.Write))
  XpsDocument.CreateXpsDocumentWriter(doc).Write(... content ...);

Process.Start("gxps.exe",
              "-sDEVICE=pdfwrite -sOutputFile=" +
                  pdfPath +
                  "-dNOPAUSE " +
                  xpsPath).WaitForExit();

// Now the PDF file is found at pdfPath
明媚如初 2024-09-19 14:16:15

一种简单的方法(很容易,但可能不是最有效的方法)是将固定文档渲染为图像,然后使用 iTextSharp 将图像嵌入到 PDF 中。

我以前也是按照这个方法成功完成的。最初,我尝试将控件基元(形状)转换为 PDF 等效项,但事实证明这太难了。

A simple way, which is easy, but probably not the most efficient way is to render the Fixed document to an image and then embed the image in a PDF using iTextSharp.

I have done it this way before successfully. Initially I tried to convert the control primitives (shapes) to PDF equivalents, but this proved too hard.

只等公子 2024-09-19 14:16:15

如果您可以将其从 WPF 中获取到图像中,那么您可以将其导入到 iTextSharp 中,就像本文中所做的那样。如果将文件系统写入 MemoryStream,然后使用它而不是使用 FileStream,您甚至可以完全避免使用文件系统。

http://www.mikesdotnetting.com/Article/87/iTextSharp-处理图像

If you can get it into an image from WPF then you can import it into iTextSharp like they do in this article. You can even avoid the filesystem all together if you write it to a MemoryStream and then use that instead of using a FileStream.

http://www.mikesdotnetting.com/Article/87/iTextSharp-Working-with-images

晨与橙与城 2024-09-19 14:16:15

如果您想以编程方式执行此操作,则最好的选择是以下路径 XPS(固定文档)->打印到 PS ->使用 Ghostscript 读取 PS 并转换为 PDF。
如果您不关心在代码中读回 PDF,则可以打印到任何一台可以传递目标路径的免费 PDF 打印机。这样,如果您的报告中有任何测试,您的目标 PDF 文件仍然可以搜索。

IF you want to do it programatically, your Best bet would be the following path XPS (Fixed Document) -> Print to PS -> Use Ghostscript to read the PS and convert to PDF.
If you dont care about reading the PDF back in the code, you can print to any one of the free PDF printers to which you can pass the destination path. This way your target PDF file will still be searchable if you have any test in your report.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文