C# API to convert Publisher PUB files

Overview to .pub conversion functionality via .NET

Converting PUB files is an option when you want to open or edit Microsoft Publisher files in other software. Let’s consider other reasons why would you want to convert PUB files:

There are various file formats you can convert PUB files to, such as PDF, DOCX (Microsoft Word), RTF (Rich Text Format), or HTML (web page format). The choice of format depends on your specific needs and the software or platform you intend to use. And if you want to do it online without coding anything you can use cross-platform converters for such a purpose.

PUB to PDF Conversion using C#

Aspose.PUB for .NET can read and convert publication files (.pub) to PDF. The  PubFactory class creates content from a .pub file for further processing by the Document class of the API.

At present, the API doesn’t support the conversion of images in a PUB file to output PDF.

Portable Document Format (PDF) was introduced by Adobe to represent documents that can be read on digital devices. PUB files require Microsoft Publisher to be installed on the computer in order to open these files. Aspose.PUB for .NET lets you convert PUB files to PDF that can be opened on almost all computers without the need for Microsoft Publisher being installed. The following steps and code snippet show how to convert PUB to PDF using C# in your .NET applications.

Aspose.PUB for .NET supports converting multi-page PUB documents to PDF using the same lines of code given in this article.

The provided code snippet uses Aspose.PUB library for .NET to convert a Microsoft Publisher (.pub) file into a PDF format. Here’s a breakdown of the code:

  1. Declare a string variable named dataDir and assign it the path to the directory where the data files are located. The GetDataDir_Data() method retrieves the directory path.

  2. Declare a string variable named fileName and assign it the path of the specific .pub file to be converted. The dataDir variable is used to construct the complete file path by appending the file name, to the directory path.

  3. Create an instance of the PubParser class using the CreateParser method from the PubFactory class, passing the fileName as a parameter.

  4. Call the Parse method on the parser object to parse the .pub file and obtain a PubDocument object. The Parse method is responsible for reading and extracting the content of the .pub file, storing it in a format that can be further processed.

  5. Use the PubFactory.CreatePdfConverter() method to create a PDF converter instance.

  6. Call the ConvertToPdf method on the PDF converter object, passing the doc object and the desired output file path (dataDir + “result_out.pdf”) as parameters. This method converts the parsed .pub document (doc) into a PDF format and saves it to the specified output file.

 1    // The path to the documents directory.
 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    Aspose.Pub.PubFactory.CreatePdfConverter().ConvertToPdf(doc, dataDir + "result_out.pdf");

For complete examples and data files, please go to Github. To see how the functionality can be implemented into a cross-platform application learn our online PUB Converter. Go to the product page to fully discover PUB to PDF conversion via .NET

PUB to JPEG Conversion using C#

The C# code below demonstrates the conversion of the Publisher (.pub) file to JPEG using Aspose.PUB for .NET library that provides PubFactory and related classes. Here’s a breakdown of what the code does:

  1. Get the directory path where the input and output files are stored using the method RunExamples.GetDataDir_Data().
  2. Construct the full path of the input .pub file by appending the file name to the data directory path.
  3. Create a parser for the .pub file using PubFactory.CreateParser(fileName), then parse the file to obtain a Document object representing the .pub file’s contents.
  4. Convert the parsed document to a jpg file and save it. The ConvertToFile method is called with the document, the desired export format (PubExportFormats.Jpg), and the output file path. You can convert PUB to another format in a similar way, using one of the supported PubExportFormats
 1    // The path to the documents directory.
 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 jpg and save the result as file "halloween-flyer_out.jpg"
11    ConvertToFile(doc, PubExportFormats.Jpg, dataDir + "halloween-flyer_out.jpg");
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.