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);
相关推荐
技术钱2 小时前
vue3 封装图片上传预览组件支持docx、excel、pdf、图片、txt格式
vue.js·pdf·excel
xingxing_F4 小时前
PDF Protector for mac PDF文档加密解密工具
macos·pdf
985小水博一枚呀6 小时前
【AI大模型学习路线】第三阶段之RAG与LangChain——第十九章(实战基于Advanced RAG的PDF问答)系统部署与测试?
人工智能·学习·langchain·pdf
励志成为美貌才华为一体的女子8 小时前
本地用docling实现pdf转markdown操作笔记
笔记·pdf
William_cl10 小时前
一、前置基础(MVC学习前提)_核心特性_【C# 泛型入门】为什么说 List<T>是程序员的 “万能收纳盒“?避坑指南在此
学习·c#·mvc
星空的资源小屋13 小时前
RoboIntern,一款自动化办公小助手
运维·人工智能·pdf·自动化·电脑·excel
c#上位机14 小时前
wpf之命令
c#·wpf
mucheni15 小时前
迅为RK3568开发板OpenHarmony系统南向驱动开发手册-pdf配置 rk3568_uart_config.hcs
驱动开发·pdf
曹牧17 小时前
C#:函数默认参数
开发语言·c#
ONLYOFFICE17 小时前
ONLYOFFICE 桌面编辑器9.1版本已发布:PDF密文功能和全新注释、工作表公式优化及文件恢复便捷化等
pdf·编辑器·onlyoffice 模板