iTextSharp to generate PDF from WPF FixedDocument

leo*_*yer 7 pdf wpf itextsharp fixeddocument

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?

Ray*_*rns 7

WPF FixedDocument,也称为XPS文档,是对PDF的明确改进.它具有PDF缺乏的许多功能.在大多数情况下,最好是你的文件分发为XPS而不是PDF,但有时如果你需要打开在只有PDF支持设备上的文件需要从XPS转换为PDF格式,例如.不幸的是,大多数从XPS转换为PDF的免费工具,如CutePDF和BullzipPDF,都需要安装打印机驱动程序或不是开源的.

一个好的开源解决方案是使用GhostPDL中的"gxps"工具.GhostPDL是Ghostscript项目的一部分,在GPL2下是开源许可的.

  1. http://ghostscript.com/releases/ghostpdl-8.71.tar.bz2下载GhostPDL 并编译.
  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
Run Code Online (Sandbox Code Playgroud)


Gid*_*sey 1

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

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