API to manipulate Publisher files in C#

Aspose.PUB for .NET lets you work with PUB files in your .NET applications. It supports manipulating .pub files with a simple and well-defined structure. 

Edit MetaData of PUB Files

Metadata of .pub files accessible by Aspose.PUB

Microsoft Publisher (.pub) files have metadata information about the file itself. There is examples of information that you can get using this API Solution:

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.
KeywordsRelevant keywords or tags to describe the content of the publication.
CommentsAdditional notes related to the publication.
Creation DateThe date when the publication was originally created.
Modification DateThe date when the publication was last edited.
Last AuthorThe name of the person who last made changes to the document.
TemplateApplication-specific template from which the document was created.
Character CountProvides the total number of characters in the document.
Word CountProvides the total number of words in the document.
Page CountProvides the total number of pages in the document.

Functionality to work with metadata of .pub files provided by Aspose.PUB for .NET

Metadata describes document properties such as title, company, language, and related information. The following example parses halloween-flyer.pub and updates eight properties through the DocSummaryInfo and SummaryInfo classes. These changes affect the parsed Document in memory; the example does not save a modified PUB file.

In these examples, DataDir is the input directory and OutputDir is the output directory. Set them to your own directories and ensure the output directory exists before running the stream example. Import the Aspose.Pub and System.IO namespaces when using the snippets in your application.

 1// Edit the metadata of a parsed PUB document in memory
 2
 3// Parse the input Publisher document.
 4string fileName = Path.Combine(DataDir, "halloween-flyer.pub");
 5IPubParser parser = PubFactory.CreateParser(fileName);
 6Document document = parser.Parse();
 7
 8// Update document metadata. These changes do not save a PUB file.
 9document.DocumentSummaryInfo.SetCategory("category");
10document.DocumentSummaryInfo.SetCompany("company");
11document.DocumentSummaryInfo.SetLanguage("language");
12
13document.SummaryInfo.SetComments("comments");
14document.SummaryInfo.SetKeywords("keywords");
15document.SummaryInfo.SetLastAuthor("last author");
16document.SummaryInfo.SetTitle("title");
17document.SummaryInfo.SetSubject("subject");
Example-EditMetadata.cs hosted with ❤ by GitHub

Write output to stream

The following example converts a PUB document to TIFF and writes the result to a file stream. The using block closes the stream after conversion. Here is how the example works:

  1. Construct the input path with Path.Combine(DataDir, "halloween-flyer.pub").
  2. Create an IPubParser using PubFactory.CreateParser(fileName), then call parser.Parse() to obtain a Document object.
  3. Construct outPath with Path.Combine(OutputDir, "halloween-flyer_out.tiff") and open the output stream with File.Create(outPath). This creates the file or truncates an existing file.
  4. Call PubFactory.CreatePubConverter() to create a converter, then call ConvertToFormat(doc, outputStream, PubExportFormats.Tiff) to write TIFF data to the stream. The stream is disposed when the using block ends.

A similar way you can convert PUB to another format, using one of the supported PubExportFormats

 1// Convert a PUB document to TIFF using an output stream
 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// Write TIFF output to a file stream and close it after conversion.
 9string outPath = Path.Combine(OutputDir, "halloween-flyer_out.tiff");
10using (FileStream outputStream = File.Create(outPath))
11{
12    PubFactory.CreatePubConverter().ConvertToFormat(doc, outputStream, PubExportFormats.Tiff);
13}

To learn all the .NET code examples and data files, please go to Aspose.PUB-Documentation for C# Github project.

Also take a look at the functionality provided by Aspose.PUB implemented into a set of cross-platform applications to manipulate .pub files.