目录

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);
本文是转载文章,点击查看原文
如有侵权,请联系 xyy@jishuzhan.net 删除
相关推荐
Wythzhfrey34 分钟前
单片机Day05---静态数码管
c语言·单片机·嵌入式硬件·学习·c#·51单片机
夜月yeyue2 小时前
STM32启动流程详解
linux·c++·stm32·单片机·嵌入式硬件·c#
码观天工2 小时前
解锁.NET 9性能优化黑科技:从内存管理到Web性能的最全指南
性能优化·c#·.net·内存管理·异步·.net 9
Tdm_8883 小时前
SQL Server中OPENJSON + WITH 来解析JSON
java·数据库·sql·c#·json·mssql
非凡ghost4 小时前
MJ PDF汉化版:功能强大,阅读无忧
学习·智能手机·pdf·软件需求
188_djh6 小时前
# 使用python写一个PDF文件转换成word 文件
python·pdf·word·python-docx·pypdf2·python3.9·pdf_to_word.py
观无7 小时前
关于Newtonsoft.Json
c#
唐青枫7 小时前
C# 如何比较两个List是否相等?
c#·.net
搬砖工程师Cola12 小时前
<C#>在 C# .NET 6 中,使用IWebHostEnvironment获取Web应用程序的运行信息。
开发语言·c#·.net
Q1860000000016 小时前
PDF解析示例代码学习
学习·pdf