C#如何通过使用XpsToPdf库来转换xps为pdf文件

文章目录

XpsToPdf库地址

调用方法:

英文描述

Output to PDF in WPF (for free!)

There are two general strategies to outputting to a PDF in WPF. One is to output directly to a PDF which requires you traverse a visual or flow document and translate to a PDF. But the other and more common method is to output to XPS as an intermediary and then convert the XPS file to a PDF. The latter is preferred (to me) since .NET will do all the work converting to XPS which, as a document format, is closer to the structure of a PDF and therefore easier to convert.

It's difficult to find a free, open source library to output to a PDF from a WPF program. I've been working on a project that needs direct output to a PDF, but everything I found was either very expensive or command line only (Ghostscript 9.06). But with a little help from Alex Hope O'Connor, I was pointed in the right direction.

Version 1.31 of the PDFSharp project included a beta project that converts an XPS to PDF. (For whatever reason it wasn't included in 1.32 which has confused a lot of people directed to the project for this purpose.) Because it was in beta, many people had issues with it so I first applied fixes from here.

But I was still having issues with invalid bounding of a tiled brush, improperly scaled path geometry, and tiling of a vector image (leaving artifacts around the edges). I managed to work through these by opening the XPS with 7-zip and examining the raw page data. In this process, the PDF Reference for version 1.4 was indispensable in playing with the output PDF to make it work right.

Here's some sample code that writes a DocumentPaginator called dp to an in-memory XPS and then passes that to the PDFSharp converter. (I got this snippet from here.) Note that you could just as easily pass in a Visual or any of the other supported arguments to the Write method. In addition to including the PDFSharp.Xps library, you'll also need to reference System.Printing and ReachFramework.

中文描述

在WPF中输出PDF

在WPF中输出PDF有两种通用策略。一种是直接输出到PDF,这需要遍历可视化或流程文档并转换为PDF。但另一种更常见的方法是将输出到XPS作为中介,然后将XPS文件转换为PDF。(对我来说)后者是首选,因为. net将完成所有转换到XPS的工作,XPS作为一种文档格式,更接近PDF的结构,因此更容易转换。

很难找到一个免费的开源库来从WPF程序输出到PDF。我一直在做一个需要直接输出到PDF的项目,但我发现的一切要么非常昂贵,要么只有命令行(Ghostscript 9.06)。但在亚历克斯·霍普·奥康纳的帮助下,我找到了正确的方向。

PDFSharp项目的1.31版本包括一个测试版项目,可以将XPS文件转换为PDF文件。(不管出于什么原因,它没有包含在1.32中,这让很多为了这个目的而参与项目的人感到困惑。)因为它还在测试阶段,很多人都有问题,所以我首先从这里应用了修复程序。

但我仍然有一些问题,如平铺笔刷的边界无效,路径几何缩放不正确,矢量图像的平铺(在边缘留下伪影)。我通过使用7-zip打开XPS并检查原始页面数据来解决这些问题。在这个过程中,版本1.4的PDF参考是处理输出PDF以使其正常工作所不可或缺的。

下面是一些示例代码,它们将名为dp的DocumentPaginator写入内存中的XPS,然后将其传递给PDFSharp转换器。(我从这里得到了这个片段。)请注意,您可以很容易地将Visual或任何其他支持的参数传递给Write方法。除了包括PDFSharp。Xps库,还需要参考系统。打印和ReachFramework。

csharp 复制代码
using System.IO;
using System.IO.Packaging;
using System.Windows.Xps.Packaging;
using System.Windows.Xps;

MemoryStream lMemoryStream = new MemoryStream();
Package package = Package.Open(lMemoryStream, FileMode.Create);
XpsDocument doc = new XpsDocument(package);
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);
writer.Write(dp);
doc.Close();
package.Close();

var pdfXpsDoc = PdfSharp.Xps.XpsModel.XpsDocument.Open(lMemoryStream);
PdfSharp.Xps.XpsConverter.Convert(pdfXpsDoc, d.FileName, 0);

如果你已经有一个XPS文件,你可以简单地用一行转换它:

csharp 复制代码
PdfSharp.Xps.XpsConverter.Convert(sourceXpsFile, destPdfFile, 0);
相关推荐
慢慢沉6 小时前
C#(基本语法)
c#
★YUI★8 小时前
学习游戏制作记录(克隆技能)7.25
学习·游戏·unity·c#
坚持吧202110 小时前
【无标题】word 中的中文排序
开发语言·c#
_oP_i10 小时前
c# openxml 打开加密 的word读取内容
开发语言·c#·word
醉酒的李白、11 小时前
C#观察者模式示例代码
观察者模式·c#
咩咩觉主19 小时前
Unity编辑器拓展 IMGUI与部分Utility知识总结(代码+思维导图)
unity·c#·编辑器·游戏引擎
无规则ai20 小时前
C#入门实战:数字计算与条件判断
c#·visual studio
bianguanyue1 天前
WPF——自定义ListBox
c#·wpf
Eiceblue1 天前
PDF转Markdown - Python 实现方案与代码
开发语言·vscode·python·pdf
Bruce_Liuxiaowei1 天前
Python实现PDF按页分割:灵活拆分文档的技术指南
windows·python·pdf