当前位置: 首页 > article >正文

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。

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文件,你可以简单地用一行转换它:

PdfSharp.Xps.XpsConverter.Convert(sourceXpsFile, destPdfFile, 0);

http://www.kler.cn/a/516187.html

相关文章:

  • 电子应用设计方案102:智能家庭AI鱼缸系统设计
  • Java 生成 PDF 文档 如此简单
  • 寒假刷题Day12
  • 蚁群算法 (Ant Colony Optimization) 算法详解及案例分析
  • 如何有效使用Python爬虫将网页数据存储到Word文档
  • Android SystemUI——通知栏构建流程(十六)
  • WordPress果果对象存储插件
  • 领域驱动设计(DDD)Spring Boot 3 实现 二
  • 在系统重构中的工作计划与总结
  • Web安全:缓存欺骗攻击;基于缓存、CDN的新型Web漏洞
  • OpenCV图像显示imshow()函数——详解
  • Sharding-JDBC 5.4.1+SpringBoot3.4.1+MySQL8.4.1 使用案例
  • 云计算中的微服务架构是什么
  • autogen 中的 Teams 示例
  • 【数据结构进阶】红黑树超详解 + 实现(附源码)
  • 【探索 Kali Linux】渗透测试与网络安全的终极操作系统
  • 使用github提交Pull Request的完整流程
  • 差分进化算法 (Differential Evolution) 算法详解及案例分析
  • HTML5 新的 Input 类型详解
  • 计算机图形学:实验二 三维模型读取与控制
  • C++ 入门速通1【黑马】
  • 52.this.DataContext = new UserViewModel(); C#例子 WPF例子
  • Python数据类型与操作
  • matlab计算功率谱的四种方法
  • 【Linux】Linux的基本指令(1),包括ls、pwd、cd、touch、mkdir、rm、man、cp、mv、cat
  • Vue2:使用sortablejs实现el-table中行拖拽调整顺序