Microsoft Publisher (PUB) files are used for printing or publishing information. There might be scenarios where you need to share these files, and the receiving party does not have access to Microsoft Publisher. In such cases, converting these files to images before sharing can prove to be helpful. To that end, this article will teach you how to convert PUB files to various image formats using C++.
- C++ APIs for Converting PUB Files to Image Format
- Convert PUB Files to JPG Images using C++
- Convert PUB Files to PNG Images using C++
- Converting PUB Files to TIFF Images using C++
C++ APIs for Converting PUB Files to Image Format
We will use the Aspose.PUB for C++ and Aspose.PDF for C++ APIs to perform this conversion. The former is a library for working with Microsoft Publisher (PUB) files, whereas the latter is the library for creating, reading, and modifying PDF files. We will use the Aspose.PUB for C++ API to convert PUB files to PDF format and Aspose.PDF for C++ API to convert the generated PDF files to image format. You can either install the APIs through NuGet or download them directly from the Downloads section.
PM> Install-Package Aspose.PUB.Cpp
PM> Install-Package Aspose.PDF.Cpp
Convert PUB Files to JPG Images using C++
The following are the steps to convert PUB files to JPG images.
- Load the PUB file using the PubFactory::CreateParser(System::String fileName) method.
- Convert the PUB file to PDF format using the ConvertToPdf(System::SharedPtr<Document> doc, System::String fileName) method.
- Load the generated PDF file using the Document class.
- Loop through the pages of the PDF file.
- Get the dimensions of the PDF page.
- Create an instance of the Resolution class.
- Instantiate the JpegDevice class by providing the width, height, and resolution.
- Create an instance of the FileStream for the output image.
- Convert the PDF page to JPG image using the JpegDevice->Process(System::SharedPtr<Page> page, System::SharedPtr< System::IO::Stream > output) method.
- Close the stream.
The following sample code shows how to convert PUB files to JPG images using C++.
Convert PUB Files to PNG Images using C++
In order to convert PUB files to PNG images, follow the steps given below.
- Load the PUB file using the PubFactory::CreateParser(System::String fileName) method.
- Convert the PUB file to PDF format using the ConvertToPdf(System::SharedPtr<Document> doc, System::String fileName) method.
- Load the generated PDF file using the Document class.
- Loop through the pages of the PDF file.
- Get the dimensions of the PDF page.
- Create an instance of the Resolution class.
- Instantiate the PngDevice class by providing the width, height, and resolution.
- Create an instance of the FileStream for the output image.
- Convert the PDF page to PNG image using the PngDevice->Process(System::SharedPtr<Page> page, System::SharedPtr<System::IO::Stream> output) method.
- Close the stream.
The following sample code shows how to convert PUB files to PNG images using C++.
Converting PUB Files to TIFF Images using C++
The following are the steps to convert PUB files to TIFF images.
- Load the PUB file using the PubFactory::CreateParser(System::String fileName) method.
- Convert the PUB file to PDF format using the ConvertToPdf(System::SharedPtr<Document> doc, System::String fileName) method.
- Load the generated PDF file using the Document class.
- Get the dimensions of the first PDF page.
- Create an instance of the Resolution class.
- Create an instance of the TiffSettings class and set the required settings.
- Instantiate the TiffDevice class by providing the width, height, resolution, and TiffSettings.
- Create an instance of the FileStream for the output image.
- Convert the PDF file to TIFF image using the TiffDevice->Process( System::SharedPtr<Aspose::Pdf::Document> document, int32_t fromPage, int32_t toPage, System::SharedPtr<System::IO::Stream> output) method.
- Close the stream.
The following sample code demonstrates how to convert PUB files to TIFF images using C++.
Get a Free License
You can try the API without evaluation limitations by requesting a free temporary license.
Conclusion
In this article, you have learned how to convert Microsoft Publisher (PUB) files to JPG, PNG, and TIFF images using C++. We used the Aspose.PUB for C++ and Aspose.PDF for C++ APIs to achieve this. You can explore these APIs in detail by visiting their official documentation. In case of any questions, please feel free to reach us at our free support forum.