C# Convert XPS to PDF | Convert OXPS to PDF in C#

XPS(开放XML纸规格)OXPS(OpenXPS) 是固定页面文档格式,旨在保留原始内容的布局和配置。 PDF(可用的文档格式), 在本文中,我们将展示如何将 XPS 或 OXPS 转换为 PDF 在 C#,为开发人员提供一个全面的指南,希望通过使用最好的 C# 图书馆来提高他们的文档管理能力,将 XPS 转换为 PDF。

内容表

C# API 将 XPS 转换为 PDF

要有效地进行 XPS 到 PDF 转换为 C#,我们将使用 Aspose.Page 为 .NET 这个坚实的图书馆允许开发人员创建、编辑、操纵和转换XPS和 EPS / PS Aspose.Page for .NET 是一个可靠、独立的 API,无缝地集成到您的 .net 应用程序中。

你也可以 下载DLL 直接或使用下列 API 安装 诺基亚 命令:

PM> Install-Package Aspose.Page

将 XPS 转换为 PDF 使用 C#

要将整个 XPS 文档转换为 PDF 格式,请遵循以下步骤:

  • 输入 XPS 文件。
  • 以所需的参数启动选项对象。
  • 创建一个例子的 PdfDevice 播放。
  • 将 XPS 文件导出到 PDF 文件中。

下面是一个代码样本显示 如何将XPS文件转换为PDF使用C#:

// Initialize PDF output stream
using (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,
};
// 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);
}
view raw XPStoPDFfile.cs hosted with ❤ by GitHub

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

如果您需要将特定页面从 XPS 文档转换为 PDF 格式,请遵循以下步骤:

  • 启动 XPS 输入流。
  • 将 XPS 文件从流中加载。
  • 创建 PdfSaveOptions 对象的例子。
  • 指定转换页面号码。
  • 将文件保存为 PDF 文件。

下面的代码样本描述了 如何将特定页面从XPS转换为PDF为C#:

// Initialize PDF output stream
using (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,如下所示。

XPS to PDF

OXPS 到 PDF 转换器在 C#

OXPS 格式是 XPS 文件格式的先进版本,但它可能不受旧操作系统的支持。

  • 启动 OXPS 输入流。
  • 从流中下载 OXPS 文件。
  • 创建 PdfSaveOptions 类的例子。
  • 将 OXPS 文件导出到 PDF 文件中。

下面是一个代码样本显示 如何将 OXPS 转换为 PDF 在 C#:

// Initialize PDF output stream
using (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 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 format
Aspose.Page.XPS.Presentation.Pdf.PdfDevice device = new Aspose.Page.XPS.Presentation.Pdf.PdfDevice(pdfStream);
document.Save(device, options);
}

将 OXPS 的特定页面转换为 PDF 以 C#

您也可以将特定页面从 OXPS 文档转换为 PDF 格式。

  • 加载 OXPS 文件。
  • 宣布 PdfSaveOptions 对象。
  • 指定您要转换的页数(页数)。
  • 将 OXPS 文件转换为 PDF。

下面的代码剪辑显示了 如何将特定页面从 OXPS 转换为 PDF 在 C#,重点是转换第一页:

// Initialize PDF output stream
using (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);
}

获得免费许可证

你可以 获得免费的临时许可证 可以测试 Aspose.Page 图书馆,没有任何评估限制。

学习资源为XPS转换到PDF

除了将 XPS 或 OXPS 文档转换为 PDF 格式之外,请使用以下资源探索图书馆的各种功能:

结论

在本文中,我们探讨了如何通过 C# 编程 转换 XPS 到 PDF 和 OXPS 为 PDF. 我们还讨论了从 xps 及 oxp 文件中将特定页面导出到 pdf 格式。 免费支持论坛.

More in this category