WPF 转 XPS 转 PDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13848223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF to XPS to PDF
提问by Alex Hope O'Connor
I have implemented a report in standard WPF controls and have also implemented a DocumentPaginatorto take these controls and convert them into a document for printing.
我已经在标准 WPF 控件中实现了一个报告,并且还实现了一个DocumentPaginator来获取这些控件并将它们转换为用于打印的文档。
I have also implemented some code which uses the document paginator to render the pages to images and write them out to a PDF using PDFSharp, however this does not allow for copying and pasting, also the image quality is questionable.
我还实现了一些使用文档分页器将页面呈现为图像并使用PDFSharp将它们写出到 PDF 的代码,但是这不允许复制和粘贴,图像质量也有问题。
I have experimented with the GhostXPSutility and was thinking of using it by saving out to an XPS document and then using GhostXPSto convert it to a PDF, this was promising, however the current version contains a bug that renders data copied from the generated PDF useless...
我已经尝试过GhostXPS实用程序,并考虑通过保存到 XPS 文档然后使用GhostXPS将其转换为 PDF 来使用它,这很有希望,但是当前版本包含一个错误,该错误会呈现从生成的 PDF 复制的数据无用...
So I thought I would ask here to try and find a efficient method for doing this, Can someone please suggest a good way to take a DocumentPaginatorand either save it directly out to a PDF, with copy/paste functionality or convert it from an XPS document to a PDF with this functionality?
所以我想我会在这里要求尝试找到一种有效的方法来做到这一点,有人可以提出一种很好的方法来获取DocumentPaginator并直接将其保存为 PDF,具有复制/粘贴功能或从 XPS 文档转换它到具有此功能的 PDF?
Thanks, Alex.
谢谢,亚历克斯。
EDIT - PDFSharp fix: Ok as requested here is what I did to compile the fix for the PDFSharp 1.31 XPS converter:
编辑 - PDFSharp 修复:好的,按照这里的要求,我为 PDFSharp 1.31 XPS 转换器编译了修复:
1) I downloaded the source from here:
1)我从这里下载了源代码:
2) I followed the instruction in this post:
2)我按照这篇文章中的说明进行操作:
XpsParser.ImageBrush.cs (ln 22, added): brush.Opacity = 1;
PdfContentWriter.cs (ln 526, changed): if (opacity <= 1)
3) Then just compile the source, first open the 'PdfSharp-WPF.csproj' and build that, then open the 'PdfSharp.Xps.csproj' and build it as well, the relevent libraries are 'PdfSharp-WPF.dll' and 'PdfSharp.Xps.dll'
3)然后只编译源代码,首先打开'PdfSharp-WPF.csproj'并构建它,然后打开'PdfSharp.Xps.csproj'并构建它,相关库是'PdfSharp-WPF.dll'和'PdfSharp.Xps.dll'
Note upon trying to build the source you will likely receive a few errors depending on which version of .NET you are targeting, these are simple to fix if you just read the error messages.
请注意,在尝试构建源代码时,您可能会收到一些错误,具体取决于您所针对的 .NET 版本,如果您只是阅读错误消息,这些错误很容易修复。
EDIT(2): NJones created a blog post with more details on the PdfSharp fix: Output to PDF in WPF
编辑(2):NJones 创建了一篇博客文章,其中包含有关 PdfSharp 修复的更多详细信息:在 WPF 中输出到 PDF
采纳答案by I liked the old Stack Overflow
There is an XPS Converter that was included with PDFsharp 1.31. It's a beta version, but AFAIK it supports nearly everything and may work for your needs.
PDFsharp 1.31 中包含一个 XPS 转换器。这是一个测试版,但 AFAIK 几乎支持所有内容,并且可以满足您的需求。
PDFsharp 1.31 can be downloaded from SourceForge or CodePlex.
PDFsharp 1.31 可以从 SourceForge 或 CodePlex 下载。
回答by David van Driessche
If a commercial SDK would be an option, there is one company I came across that has a professional XPS to PDF conversion option (and more).
如果可以选择商业 SDK,我遇到的一家公司提供专业的 XPS 到 PDF 转换选项(以及更多选项)。
Warning: I'm not affiliated with this company nor have I tried their products. I haveon the other hand heard good things about them.
警告:我不隶属于这家公司,也没有尝试过他们的产品。我有关于他们另一方面听到好东西。
回答by Alan
I use Telerik Reportingwhich handles document pagination and allows you to create reports in a designer similar to creating a GUI. I decided to just buy a 3rd party product because I didn't want to spend development time writing my own paginator, etc. Also, this tool is probably more bug proof then a custom solution and allows a lot of flexibility for how you want to design your printout/report. In the future if you have more complicated printing or reports required, this tool does a lot more. I do not work for Telerik, but there product is good.
我使用Telerik Reporting来处理文档分页,并允许您在类似于创建 GUI 的设计器中创建报告。我决定只购买 3rd 方产品,因为我不想花费开发时间来编写自己的分页器等。此外,该工具可能比自定义解决方案更能证明错误,并且为您提供了很大的灵活性。设计您的打印输出/报告。将来,如果您需要更复杂的打印或报告,此工具会做得更多。我不为 Telerik 工作,但那里的产品很好。
It allows exporting to PDF, Word, Excel, HTML, PowerPoint, PNG, etc. etc.
它允许导出为 PDF、Word、Excel、HTML、PowerPoint、PNG 等。
回答by Algorath
IMHO, you've got the right idea: XPS is, in my mind, the simplest method of serializing the data into a usable format, especially for conversion.
恕我直言,您的想法是正确的:在我看来,XPS 是将数据序列化为可用格式的最简单方法,尤其是对于转换。
As for a third party tool, I've a peer (as in, not myself,)who's used CheckPrixawith some measure of success; it's freeware and it offers command line conversions. (i.e. serializing a document and converting a la generated .bat file.)
至于第三方工具,我有一个同行(例如,不是我自己)使用CheckPrixa取得了一定程度的成功;它是免费软件,并提供命令行转换。(即序列化文档并转换 la 生成的 .bat 文件。)
It also allows you to handle keywords, titles, etc. along with more important items like even pagination.
它还允许您处理关键字、标题等以及更重要的项目,例如分页。
As far as I'm aware, there are no known discrepancies.
据我所知,没有已知的差异。
Alternatively, if converting the MSXPS is what's giving you headaches, you might want to look at XpsConverter, it converts MSXPS to OpenXPS, and as a Window's driver, it's available on any machine using .NET 3 and above.
或者,如果转换 MSXPS 是让您头疼的原因,您可能需要查看XpsConverter,它将 MSXPS 转换为 OpenXPS,并且作为 Window 的驱动程序,它可以在使用 .NET 3 及更高版本的任何机器上使用。
Keep us updated with whatever solution you try, virtual reports are a growing deal.
让我们随时了解您尝试的任何解决方案,虚拟报告越来越受欢迎。

