转换 Publisher PUB 文件的 C# API

通过 .NET 实现概述到 .pub 转换功能

当您想在其他软件中打开或编辑 Microsoft Publisher 文件时,可以选择转换 PUB 文件。让我们考虑一下为什么要转换 PUB 文件的其他原因:

您可以将 PUB 文件转换为 各种文件格式,如 PDF、DOCX(Microsoft Word)、RTF(富文本格式)或 HTML(网页格式)。格式的选择取决于您的具体需求以及您打算使用的软件或平台。如果您想在不编写任何代码的情况下在线完成,可以使用 跨平台转换器 来实现这一目的。

使用 C# 将 PUB 转换为 PDF

Aspose.PUB for .NET可以读取出版物文件(.pub)并将其转换为 PDF。PubFactory 类从 .pub 文件创建内容,供 API 的 Document 类进一步处理。

目前,API 不支持将 PUB 文件中的图像转换为输出 PDF。

便携式文档格式(PDF)由 Adobe 推出,用于表示可在数字设备上阅读的文档。PUB 文件需要在计算机上安装 Microsoft Publisher 才能打开这些文件。Aspose.PUB for .NET 可让您将 PUB 文件转换为 PDF,几乎所有计算机都可以打开 PDF,而无需安装 Microsoft Publisher。以下步骤和代码片段展示了如何在 .NET 应用程序中使用 C# 将 PUB 转换为 PDF。

Aspose.PUB for .NET 支持使用本文给出的相同代码行将多页 PUB 文档转换为 PDF。

所提供的代码片段使用 Aspose.PUB library for .NET 将 Microsoft Publisher (.pub) 文件转换为 PDF 格式。下面是代码的详细说明:

在这些示例中,DataDir 是输入目录,OutputDir 是输出目录。请将它们设置为您自己的目录,并确保输出目录存在。在应用程序中使用这些代码片段时,请导入 Aspose.Pub 和 System.IO 命名空间。

  1. 使用 Path.Combine(DataDir, "halloween-flyer.pub") 构建输入路径,并将其存储在 fileName 中。

  2. 调用 PubFactory 的 CreateParser 方法,传入 fileName,以获取 IPubParser。

  3. 调用 parser.Parse(),将 PUB 文件读取到名为 doc 的 Document 对象中。

  4. 使用 Path.Combine(OutputDir, "halloween-flyer_out.pdf") 构建 outPath。

  5. 调用 PubFactory.CreatePdfConverter() 创建 PDF 转换器,然后调用 ConvertToPdf,传入 doc 和 outPath,以保存 PDF 文件。

 1// Convert a PUB document to PDF
 2
 3// Parse the input Publisher document.
 4string fileName = Path.Combine(DataDir, "halloween-flyer.pub");
 5IPubParser parser = PubFactory.CreateParser(fileName);
 6Document doc = parser.Parse();
 7
 8// Convert the document to PDF and save the result.
 9string outPath = Path.Combine(OutputDir, "halloween-flyer_out.pdf");
10PubFactory.CreatePdfConverter().ConvertToPdf(doc, outPath);

如需完整示例和数据文件,请访问 Github。要了解如何在跨平台应用程序中实现该功能,请学习我们的 在线 PUB 转换器。请访问产品页面,全面了解 通过 .NET 将 PUB 转换为 PDF

使用 C# 将 PUB 转换为 JPEG

下面的 C# 代码演示了使用 Aspose.PUB for .NET 库将 Publisher (.pub) 文件转换为 JPEG 文件,该库提供了 PubFactory 和相关类。下面是代码的详细说明:

  1. 使用 Path.Combine(DataDir, "halloween-flyer.pub") 构建输入路径。
  2. 使用 PubFactory.CreateParser(fileName) 创建 IPubParser,然后调用 parser.Parse() 获取 Document 对象。
  3. 使用 Path.Combine(OutputDir, "halloween-flyer_out.jpg") 构建 outPath。
  4. 调用 PubFactory.CreatePubConverter() 创建转换器,然后调用 ConvertToFormat(doc, outPath, PubExportFormats.Jpg) 保存 JPEG 文件。 您可以使用支持的 PubExportFormats 方法之一,以类似的方式将 PUB 转换为其他格式
 1// Convert a PUB document to JPEG
 2
 3// Parse the input Publisher document.
 4string fileName = Path.Combine(DataDir, "halloween-flyer.pub");
 5IPubParser parser = PubFactory.CreateParser(fileName);
 6Document doc = parser.Parse();
 7
 8// Convert the document to JPEG and save the result.
 9string outPath = Path.Combine(OutputDir, "halloween-flyer_out.jpg");
10PubFactory.CreatePubConverter().ConvertToFormat(doc, outPath, PubExportFormats.Jpg);