XPS处理控件Aspose.Page推荐功能解析:使用C#以编程方式将OXPS或XPS转换为PDF

XPS和OXPS文件因其分辨率独立性而经常用于打印。但是,有时我们需要将XPS转换为PDF或将OXPS转换为PDF。在本文中,将学习使用C#以编程方式将OXPS或XPS转换为PDF。

XPS和OXPS文件因其分辨率独立性而经常用于打印。但是,有时我们需要将XPS转换为PDF或将OXPS转换为PDF。Aspose.Page可让您以高保真度和快速渲染执行这些转换。

在本文中,将学习使用C#以编程方式将OXPS或XPS转换为PDF。以下是将在此博客中讨论的功能列表:

  • 使用C#将XPS转换为PDF
  • 在C#中将XPS的特定页面转换为PDF
  • 在C#中将XPS的所有页面转换为PDF
  • 使用C#将OXPS转换为PDF
  • 在C#中将OXPS的特定页面转换为PDF
  • 在C#中将OXPS的所有页面转换为PDF

目前,.NET版Aspose.page升级到v20.4版,将XPS文件转换为PDF时不会释放内存,同时修复PS->图像的背景,感兴趣的朋友可点击下方按钮下载最新版。

使用C#将XPS转换为PDF

使用Aspose.Page for .NET API,将XPS转换为PDF很简单。我们将学习以下方法来执行XPS文件转换:

在C#中将XPS的特定页面转换为PDF

要将XPS文档的选定页面转换为PDF,请按照以下步骤操作:

  1. 初始化XPS输入流
  2. 从流中加载XPS文档
  3. 初始化PdfSaveOptions对象
  4. 指定要转换的页码
  5. 将文档另存为PDF文件

下面的代码段遵循这些步骤,并显示如何使用C#将XPS转换为PDF:

// Initialize PDF output streamusing (System.IO.Stream pdfStream = System.IO.File.Open(dataDir + "XPStoPDF.pdf", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))// Initialize XPS input stream//using (System.IO.Stream xpsStream = System.IO.File.Open(dataDir + "input.xps", System.IO.FileMode.Open))using (System.IO.Stream xpsStream = System.IO.File.Open(dataDir + "sample.xps", System.IO.FileMode.Open)){    // Load XPS document form the stream    Aspose.Page.XPS.XpsDocument document = new Aspose.Page.XPS.XpsDocument(xpsStream, new Aspose.Page.XPS.XpsLoadOptions());    // or load XPS document directly from file. No xpsStream is needed then.    // XpsDocument document = new XpsDocument(inputFileName, new XpsLoadOptions());    // Initialize options object with necessary parameters.    Aspose.Page.XPS.Presentation.Pdf.PdfSaveOptions options = new Aspose.Page.XPS.Presentation.Pdf.PdfSaveOptions()    {        JpegQualityLevel = 100,        ImageCompression = Aspose.Page.XPS.Presentation.Pdf.PdfImageCompression.Jpeg,        TextCompression = Aspose.Page.XPS.Presentation.Pdf.PdfTextCompression.Flate,        PageNumbers = new int[] {1, 3}    };    // Create rendering device for PDF format    Aspose.Page.XPS.Presentation.Pdf.PdfDevice device = new Aspose.Page.XPS.Presentation.Pdf.PdfDevice(pdfStream);    document.Save(device, options);}

此代码段使用包含多个页面的XPS文档作为输入文件。而只有页码1和3会按照代码段中的指定转换为PDF。以下屏幕截图显示了渲染到PDF文档的2页:

XPS处理控件Aspose.Page推荐功能解析:使用C#以编程方式将OXPS或XPS转换为PDF
在C#中将XPS的所有页面转换为PDF

可以将整个XPS文件转换为PDF。请按照以下步骤操作,XPS文件的所有页面都将转换为PDF文件:

  1. 加载输入的XPS文件
  2. 用必要的参数初始化选项对象
  3. 创建一个PdfDevice实例进行渲染
  4. 将XPS导出为PDF文档

下面的代码段基于所有这些步骤,这些步骤显示了如何使用C#语言将XPS文件转换为PDF:

// Initialize PDF output streamusing (System.IO.Stream pdfStream = System.IO.File.Open(dataDir + "XPStoPDF.pdf", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))// Initialize XPS input stream//using (System.IO.Stream xpsStream = System.IO.File.Open(dataDir + "input.xps", System.IO.FileMode.Open))using (System.IO.Stream xpsStream = System.IO.File.Open(dataDir + "sample.xps", System.IO.FileMode.Open)){// Load XPS document form the streamAspose.Page.XPS.XpsDocument document = new Aspose.Page.XPS.XpsDocument(xpsStream, new Aspose.Page.XPS.XpsLoadOptions());// or load XPS document directly from file. No xpsStream is needed then.// XpsDocument document = new XpsDocument(inputFileName, new XpsLoadOptions());// Initialize options object with necessary parameters.Aspose.Page.XPS.Presentation.Pdf.PdfSaveOptions options = new Aspose.Page.XPS.Presentation.Pdf.PdfSaveOptions(){    JpegQualityLevel = 100,    ImageCompression = Aspose.Page.XPS.Presentation.Pdf.PdfImageCompression.Jpeg,    TextCompression = Aspose.Page.XPS.Presentation.Pdf.PdfTextCompression.Flate,};// Create rendering device for PDF formatAspose.Page.XPS.Presentation.Pdf.PdfDevice device = new Aspose.Page.XPS.Presentation.Pdf.PdfDevice(pdfStream);document.Save(device, options);}

使用C#将OXPS转换为PDF

OXPS格式是XPS文件格式的更新和高级形式。但是,某些旧的操作系统不支持此类文件。.NET API的Aspose.Page也能够转换OXPS文件。让我们继续学习以下使用场景:

在C#中将OXPS的某些页面转换为PDF

OXPS文件可能包含许多页面,并且可以通过以下步骤将任意数量的页面转换为PDF:

  1. 加载OXPS文件
  2. 声明PdfSaveOptions对象
  3. 设置您要转换的页码
  4. 将OXPS渲染为PDF

以下代码段显示了如何使用C#将OXPS转换为PDF。如代码片段中所述,它将OXPS文件的第一页转换为PDF。

// Initialize PDF output streamusing (System.IO.Stream pdfStream = System.IO.File.Open(dataDir + "OXPStoPDF.pdf", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))// Initialize OXPS input stream//using (System.IO.Stream xpsStream = System.IO.File.Open(dataDir + "input.oxps", System.IO.FileMode.Open))using (System.IO.Stream xpsStream = System.IO.File.Open(dataDir + "sample.oxps", System.IO.FileMode.Open)){    // Load OXPS document form the stream    Aspose.Page.XPS.XpsDocument document = new Aspose.Page.XPS.XpsDocument(xpsStream, new Aspose.Page.XPS.XpsLoadOptions());    // or load XPS document directly from file. No xpsStream is needed then.    // XpsDocument document = new XpsDocument(inputFileName, new XpsLoadOptions());    // Initialize options object with necessary parameters.    Aspose.Page.XPS.Presentation.Pdf.PdfSaveOptions options = new Aspose.Page.XPS.Presentation.Pdf.PdfSaveOptions()    {        JpegQualityLevel = 100,        ImageCompression = Aspose.Page.XPS.Presentation.Pdf.PdfImageCompression.Jpeg,        TextCompression = Aspose.Page.XPS.Presentation.Pdf.PdfTextCompression.Flate,        PageNumbers = new int[] {1}    };    // Create rendering device for PDF format    Aspose.Page.XPS.Presentation.Pdf.PdfDevice device = new Aspose.Page.XPS.Presentation.Pdf.PdfDevice(pdfStream);    document.Save(device, options);}
在C#中将OXPS的所有页面转换为PDF

转换OXPS的所有页面都很简单,并且与我们上面考虑的示例有关。让我们按照以下步骤操作,将OXPS文件的所有页面转换为一个PDF文档:

  1. 初始化OXPS输入流
  2. 从流加载OXPS文件
  3. 实例化PdfSaveOptions类的对象
  4. 将OXPS导出为PDF文件

下面的代码段按以下步骤一步一步进行,并使用C#将OXPS转换为PDF:

// Initialize PDF output streamusing (System.IO.Stream pdfStream = System.IO.File.Open(dataDir + "OXPStoPDF.pdf", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))// Initialize OXPS input stream//using (System.IO.Stream xpsStream = System.IO.File.Open(dataDir + "input.oxps", System.IO.FileMode.Open))using (System.IO.Stream xpsStream = System.IO.File.Open(dataDir + "sample.oxps", System.IO.FileMode.Open)){// Load OXPS document form the streamAspose.Page.XPS.XpsDocument document = new Aspose.Page.XPS.XpsDocument(xpsStream, new Aspose.Page.XPS.XpsLoadOptions());// or load OXPS document directly from file. No xpsStream is needed then.// XpsDocument document = new XpsDocument(inputFileName, new XpsLoadOptions());// Initialize options object with necessary parameters.Aspose.Page.XPS.Presentation.Pdf.PdfSaveOptions options = new Aspose.Page.XPS.Presentation.Pdf.PdfSaveOptions(){    JpegQualityLevel = 100,    ImageCompression = Aspose.Page.XPS.Presentation.Pdf.PdfImageCompression.Jpeg,    TextCompression = Aspose.Page.XPS.Presentation.Pdf.PdfTextCompression.Flate,};// Create rendering device for PDF formatAspose.Page.XPS.Presentation.Pdf.PdfDevice device = new Aspose.Page.XPS.Presentation.Pdf.PdfDevice(pdfStream);document.Save(device, options);}

还想要更多吗可以点击阅读【2019 · Aspose最新资源整合】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询
标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年5月9日
下一篇 2020年5月9日

相关推荐

发表回复

登录后才能评论