用 C# 操作 Publisher 文件的 API

Aspose.PUB for .NET 可让您在 .NET 应用程序中处理 PUB 文件。它支持操作结构简单、定义明确的 .pub 文件。

编辑 PUB 文件的元数据

Aspose.PUB 可访问 .pub 文件的元数据###

Microsoft Publisher (.pub) 文件有关于文件本身的元数据信息。以下是使用此 API 解决方案可以获取的信息示例:

MetadataDescription
Application NameThe name of the application that was used to create the document
TitleThe title of the publication.
AuthorThe name of the person or entity who created the publication.
VersionVersion value
SubjectA brief description or topic associated with the publication.
1,033 / 5,000
元数据描述
:—:—
应用程序名称用于创建文档的应用程序的名称
标题出版物的标题。
作者创建出版物的个人或实体的名称。
版本版本值
主题与出版物相关的简短描述或主题。
关键词用于描述出版物内容的相关关键词或标签。
注释与出版物相关的附加说明。
创建日期出版物最初创建的日期。
修改日期出版物最后编辑的日期。
最后一位作者上次更改文档的人员的姓名。
模板创建文档的特定于应用程序的模板。
字符数提供文档中的总字符数。
字数提供文档中的总字数。
页数提供文档的总页数。

Aspose.PUB for .NET 提供的处理 .pub 文件元数据的功能###

文件的元数据描述了文件的属性,如作者、标题、最后作者、公司、语言和其他类似信息。这些都是与文档一起存储的有用信息。Aspose.PUB for .NET 可让您使用 DocSummaryInfoSummaryInfo 类编辑 PUB 文件的元数据,如以下代码示例所示。

 1    string dataDir = RunExamples.GetDataDir_Data()
 2
 3    string pubFile = dataDir + "document.pub";
 4
 5    IPubParser parser = PubFactory.CreateParser(pubFile);
 6    Document document = parser.Parse();
 7
 8    document.DocumentSummaryInfo.SetCategory("category");
 9    document.DocumentSummaryInfo.SetCompany("company");
10    document.DocumentSummaryInfo.SetLanguage("language");
11
12    document.SummaryInfo.SetComments("comments");
13    document.SummaryInfo.SetKeywords("keywords");
14    document.SummaryInfo.SetLastAuthor("last author");
15    document.SummaryInfo.SetTitle("title");
16    document.SummaryInfo.SetSubject("subject");

将输出写入数据流

下面的代码定义了将 PUB 转换为 TIFF 并将结果保存到数据流的过程。将输出写入流可以保存输出,确保高效处理潜在的大型图像文件、妥善管理资源以及灵活选择输出目标。下面是对代码作用的解释:

  1. 使用 RunExamples.GetDataDir_Data() 方法获取存储输入和输出文件的目录路径。
  2. 通过将文件名追加到数据目录路径,构建输入 .pub 文件的完整路径。
  3. 使用 PubFactory.CreateParser(fileName) 为 .pub 文件创建一个解析器,然后解析该文件以获得代表 .pub 文件内容的 Document 对象。
  4. 打开文件流以写入输出的 tiff 文件。然后调用 ConvertToStream 方法,同时调用文档、所需的导出格式(PubExportFormats.Tiff)和打开的文件流。与 ConvertToFile 类似,该方法将文档转换为指定格式,但将输出写入流,而不是文件。

使用支持的 PubExportFormats 将 PUB 转换为另一种格式的方法与此类似

 1    // 文件目录的路径。
 2    string dataDir = RunExamples.GetDataDir_Data()
 3
 4    string fileName = dataDir + "halloween-flyer.pub";
 5
 6    var parser = PubFactory.CreateParser(fileName);
 7
 8    var doc = parser.Parse();
 9
10    //Convert to tiff using file stream and save resultant stream as "halloween-flyer_out.tiff"
11    using (FileStream st = File.OpenWrite(dataDir + "halloween-flyer_out.tiff"))
12    {
13        ConvertToStream(doc, PubExportFormats.Tiff, st);
14    }

要了解所有 .NET 代码示例和数据文件,请访问 Aspose.PUB-Documentation for C# Github 项目

还可查看 Aspose.PUB 在一组 用于处理 .pub 文件的跨平台应用程序 中提供的功能。

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.