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// For complete examples and data files, please go to https://github.com/aspose-pub/Aspose.PUB-for-.NET
 2// The path to the documents directory.
 3string dataDir = RunExamples.GetDataDir_Data();
 4
 5string fileName = dataDir + "halloween-flyer.pub";
 6
 7var parser = PubFactory.CreateParser(fileName);
 8
 9var doc = parser.Parse();
10
11Aspose.Pub.PubFactory.CreatePdfConverter().ConvertToPdf(doc, dataDir + "result_out.pdf");

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

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.