iTextSharp从WPF FixedDocument生成PDF [英] iTextSharp to generate PDF from WPF FixedDocument

查看:250
本文介绍了iTextSharp从WPF FixedDocument生成PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的WPF应用程序,它显示并打印一些带有FixedDocument的
报告。

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

如何通过免费开放生成PDF文件解决方案,
如iTextSharp?

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

推荐答案

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

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.

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

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. http://ghostscript.com/releases/ghostpdl-8.71.tar.bz2 并进行编译。

  2. 将gxps.exe可执行文件作为内容复制到项目中,并使用Process.Start从代码中调用它。

  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

这篇关于iTextSharp从WPF FixedDocument生成PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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