将XPS文件转换成PDF格式有哪些方法?

XPS文件在我们日常工作中使用比较少,大部分时候我们都会将它转换成PDF格式后进行修改或保存等操作,那么如何将XPS转换成PDF呢?

方法1:修改后缀

我们都知道XPS跟PDF一样都可以通过虚拟打印生成,所以这两种格式比较类似,我们可以直接通过修改后缀名的方法转换。在XPS文件处右击选择“重命名”,然后直接将文档名称中后缀.xps修改为.pdf即可。不过这种方式有时可能会导致文档无法打开。
在这里插入图片描述

方法2:另存

目前很多PDF软件都可以直接打开XPS文件,我们在文件处右击将打开方式修改为PDF阅读器打开文档后,将文档另存为PDF格式就可以了。(或者使用PDF虚拟打印机也是一样的)
在这里插入图片描述

方法3:工具转换

以上两种方法并不能保存每次都成功,所以我们还是需要借助转换工具,比如我们可以使用speedpdf这款在线转换工具,直接搜索进入工具首页后,选择XPS to PDF进入转换;
在这里插入图片描述

根据页面提示上传需要转换的XPS文件后,点击开始转换,完成后下载就可以了,操作也非常简单,而且还可以免费试用哦。
在这里插入图片描述

你可以使用iTextSharp库来实现将XPS转换为PDF的操作。以下是一个简单的C#代码示例: ```csharp using System.IO; using iTextSharp.text; using iTextSharp.text.pdf; public void ConvertXpsToPdf(string xpsFilePath, string pdfFilePath) { using (Stream xpsStream = new FileStream(xpsFilePath, FileMode.Open, FileAccess.Read)) { using (FileStream pdfStream = new FileStream(pdfFilePath, FileMode.Create, FileAccess.Write)) { // Create a new iTextSharp document Document pdfDoc = new Document(); // Create a PdfWriter that listens to the document PdfWriter writer = PdfWriter.GetInstance(pdfDoc, pdfStream); // Open the iTextSharp document pdfDoc.Open(); // Create a new XpsDocument from the XPS file XpsDocument xpsDoc = new XpsDocument(xpsStream, FileAccess.Read); // Create a FixedDocumentSequence from the XpsDocument FixedDocumentSequence fixedDocSeq = xpsDoc.GetFixedDocumentSequence(); // Loop through each FixedDocument in the FixedDocumentSequence foreach (FixedDocument fixedDoc in fixedDocSeq.Documents) { // Loop through each PageContent in the FixedDocument foreach (PageContent pageContent in fixedDoc.Pages) { // Convert the PageContent to a WPF Visual FixedPage fixedPage = pageContent.GetPageRoot(false); System.Windows.Controls.Canvas canvas = new System.Windows.Controls.Canvas(); canvas.Children.Add(fixedPage); canvas.Measure(new System.Windows.Size(double.PositiveInfinity, double.PositiveInfinity)); canvas.Arrange(new System.Windows.Rect(new System.Windows.Point(), canvas.DesiredSize)); // Convert the WPF Visual to an iTextSharp image RenderTargetBitmap bitmap = new RenderTargetBitmap((int)canvas.ActualWidth, (int)canvas.ActualHeight, 96, 96, PixelFormats.Default); bitmap.Render(canvas); MemoryStream ms = new MemoryStream(); BitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bitmap)); encoder.Save(ms); Image iTextImage = Image.GetInstance(ms.ToArray()); // Add the iTextSharp image to the PDF document pdfDoc.Add(iTextImage); } } // Close the iTextSharp document pdfDoc.Close(); } } } ``` 你需要将 `xpsFilePath` 和 `pdfFilePath` 参数分别替换为你要转换的XPS文件的路径和要生成的PDF文件的路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值