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:

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. Import the Aspose.Pub and System.IO namespaces when using the snippets in your application.

  1. Construct the input path with Path.Combine(DataDir, "halloween-flyer.pub") and store it in fileName.

  2. Call the CreateParser method from PubFactory with fileName to obtain an IPubParser.

  3. Call parser.Parse() to read the PUB file into a Document object named doc.

  4. Construct outPath with Path.Combine(OutputDir, "halloween-flyer_out.pdf").

  5. Call PubFactory.CreatePdfConverter() to create a PDF converter, then call ConvertToPdf with doc and outPath to save the PDF file.

 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);

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. 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.jpg").
  4. Call PubFactory.CreatePubConverter() to create a converter, then call ConvertToFormat(doc, outPath, PubExportFormats.Jpg) to save the JPEG file.

You can convert PUB to another format in a similar way, using one of the supported PubExportFormats

 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);