In this step by step topic, you’ll learn how to convert PUB to PNG in C#. The need to convert PUB file to PNG format in C# occurs when you need to show the pages of a PUB file as images in your application. The tutorial helps you achieve this with few lines of C# code.
Steps to Convert PUB to PNG in C#
- Install Aspose.PUB for .NET and Aspose.PDF for .NET packages from NuGet.org
- Reference four required namespaces to make the code work
- Set license for both APIs separately by using SetLicense method
- Create a parser for PUB file using PubFactory class
- Load parsed PUB file into a Document object
- Save intermediate PDF file using IPdfConverter interface
- Create a new PDF Document object
- Loop through all pages of the PDF file
- Save each PDF page as PNG image using PngDevice object
In the above steps, we have used Aspose.PUB for .NET, and Aspose.PDF for .NET APIs to first convert a PUB file into an intermediate PDF file format, and then convert that intermediate PDF file to PNG images.
Code to Convert PUB to PNG in C#
using System; | |
//Add reference to Aspose.PUB for .NET API | |
//Use following namespaces to convert PUB to PNG image format | |
using Aspose.Pub; | |
using PDF = Aspose.Pdf; | |
using Aspose.Pdf.Facades; | |
using Aspose.Pdf.Devices; | |
namespace ConvertPUBToPNG | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//Set Aspose license before converting PUB to PNG format | |
//using Aspose.PUB for .NET | |
Aspose.Pub.License AsposePUBLicense = new Aspose.Pub.License(); | |
AsposePUBLicense.SetLicense(@"c:\asposelicense\license.lic"); | |
Aspose.Pdf.License AsposePDFLicense = new Aspose.Pdf.License(); | |
AsposePDFLicense.SetLicense(@"c:\asposelicense\license.lic"); | |
//Load a parsed version of Pub file to Document object | |
IPubParser PubFileParser = PubFactory.CreateParser("InputPUBFileToConvert.pub"); | |
Document PubDocument = PubFileParser.Parse(); | |
//convert to PDF using PDFConvert object | |
IPdfConverter PDFConverter = PubFactory.CreatePdfConverter(); | |
PDFConverter.ConvertToPdf(PubDocument, "IntermediatePDFFile.pdf"); | |
//create a PDF document | |
PDF.Document PDFDocument = new PDF.Document("IntermediatePDFFile.pdf"); | |
PdfFileInfo PDFFileInfo = new PdfFileInfo(PDFDocument); | |
//loop through each page and save it as PNG | |
foreach (PDF.Page PDFPage in PDFDocument.Pages) | |
{ | |
PDF.PageSize PDFPageSize = new PDF.PageSize( | |
Convert.ToInt32(PDFFileInfo.GetPageWidth(PDFPage.Number)), | |
Convert.ToInt32(PDFFileInfo.GetPageHeight(PDFPage.Number))); | |
PDF.Devices.PngDevice PNGDevice = new PDF.Devices.PngDevice(PDFPageSize); | |
PNGDevice.Process(PDFPage, "Page" + PDFPage.Number + ".png"); | |
} | |
} | |
} | |
} |
The above code helps to convert PUB to PNG in C# code with a few steps. Each individual page of the PDF file is saved as a separate PNG image. Using this code and following the steps, you can create your own PUB to PNG C# converter in your .NET applications including Windows, Web, Desktop, or Services etc. Note that this does not require Microsoft Publisher to be installed on your machine or server.