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:
Not all software or devices support the PUB file format so conversion will let you open and view it on many platforms.
If you need to collaborate with others who don’t have Microsoft Publisher, converting PUB files to a common format allows everyone to access and work on the files.
Converting PUB files to formats such as PDF or XPS will let you exclude all the possible printing issues.
PUB files to a more standard format ensures their long-term accessibility so, you can still access your documents in the future, even if Microsoft Publisher becomes outdated.
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.
Construct the input path with
Path.Combine(DataDir, "halloween-flyer.pub")and store it infileName.Call the
CreateParsermethod from PubFactory withfileNameto obtain anIPubParser.Call
parser.Parse()to read the PUB file into aDocumentobject nameddoc.Construct
outPathwithPath.Combine(OutputDir, "halloween-flyer_out.pdf").Call
PubFactory.CreatePdfConverter()to create a PDF converter, then call ConvertToPdf withdocandoutPathto 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:
- Construct the input path with
Path.Combine(DataDir, "halloween-flyer.pub"). - Create an
IPubParserusing PubFactory.CreateParser(fileName), then callparser.Parse()to obtain aDocumentobject. - Construct
outPathwithPath.Combine(OutputDir, "halloween-flyer_out.jpg"). - Call
PubFactory.CreatePubConverter()to create a converter, then callConvertToFormat(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);