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);
相关推荐
czhc114007566340 分钟前
通信 28
c#
bugcome_com4 小时前
C# 程序结构详解:从 Hello World 开始
c#
唐梓航-求职中5 小时前
编程-技术-算法-leetcode-288. 单词的唯一缩写
算法·leetcode·c#
tiantangzhixia7 小时前
Master PDF Linux 平台的 5.9.35 版本安装与自用
linux·pdf·master pdf
bugcome_com7 小时前
阿里云 OSS C# SDK 使用实践与参数详解
阿里云·c#
懒人咖17 小时前
缺料分析时携带用料清单的二开字段
c#·金蝶云星空
bugcome_com18 小时前
深入了解 C# 编程环境及其开发工具
c#
wfserial20 小时前
c#使用微软自带speech选择男声仍然是女声的一种原因
microsoft·c#·speech
阔皮大师21 小时前
INote轻量文本编辑器
java·javascript·python·c#
kylezhao20191 天前
C# 中的 SOLID 五大设计原则
开发语言·c#