
Microsoft Publisher Files are used for printing or publishing information. You can convert PUB) to JPG, PNG, or TIFF images programmatically using Java. In this article, you will learn how to convert the PUB file to different image formats:
- PUB to Image Conversion – Java API Installation
- Convert PUB to JPG Image Programmatically using Java
- Convert PUB to PNG Image Programmatically with Java
- PUB to TIFF Conversion Programmatically in Java
PUB to Image Conversion – Java APIs Installation
Aspose.PUB for Java API supports working with PUB files. You can render the PUB files to PDF and then utilize Aspose.PDF for Java API to convert the output PDF file to image formats like JPG, PNG, TIFF, etc. You can download the JAR files for Aspose.PUB for Java and Aspose.PDF for Java API or specify the following configurations to download the APIs from the Maven repository:
Repository:
<repositories>
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>http://repository.aspose.com/repo/</url>
</repository>
</repositories>
Dependency:
<dependencies>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pub</artifactId>
<version>20.8</version>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>20.5</version>
</dependency>
</dependencies>
Convert PUB to JPG Image Programmatically using Java
You can convert a Microsoft Publisher (PUB) file to JPG image with the following steps:
- Load input PUB file.
- Convert PUB to PDF file with convertToPdf method.
- Get page dimensions from the PDF document.
- Specify the Resolution and dimensions of the output image.
- Save output JPG image using JpegDevice.
The code below explains how to convert PUB to JPG image using Java:
// Load input PUB file | |
IPubParser parser = PubFactory.createParser("Test.pub"); | |
Document doc = parser.parse(); | |
// Convert PUB to PDF file | |
PubFactory.createPdfConverter().convertToPdf(doc, "Test.pdf"); | |
// Load the PDF file | |
Document document = new Document("Test.pdf"); | |
facades.PdfFileInfo info = new facades.PdfFileInfo(document); | |
for (Page page : document.getPages()) | |
{ | |
// Get page dimensions from the PDF document | |
int width = (int) info.getPageWidth(page.getNumber()); | |
int height = (int) (info.getPageHeight(page.getNumber())); | |
// Set resolution for the output image | |
devices.Resolution resolution = new devices.Resolution(300); | |
// Create JPEG device with specified Width and Height | |
devices.JpegDevice jpegDevice = new devices.JpegDevice(width, height , resolution); | |
// Convert PUB to JPG image | |
jpegDevice.process(page, "Page" + page.getNumber() + ".jpg"); | |
} |
Convert PUB to PNG Image Programmatically with Java
You can convert a PUB file to PNG format image with the steps below:
- Load input PUB file with createParser() method.
- Convert PUB to PDF file.
- Specify width and height of output image.
- Finally, convert PUB to PNG image.
The following code shows how to convert PUB to PNG image with Java:
// Load input PUB file | |
IPubParser parser = PubFactory.createParser("Test.pub"); | |
Document doc = parser.parse(); | |
// Convert PUB to PDF file | |
PubFactory.createPdfConverter().convertToPdf(doc, "Test.pdf"); | |
Document document = new Document("Test.pdf"); | |
facades.PdfFileInfo info = new facades.PdfFileInfo(document); | |
for (Page page : document.getPages()) | |
{ | |
// Get page dimensions from the PDF document | |
int width = (int) info.getPageWidth(page.getNumber()); | |
int height = (int) (info.getPageHeight(page.getNumber())); | |
// Create PNG device with specified Width and Height | |
devices.PngDevice pngDevice = new devices.PngDevice(width, height); | |
// Convert PUB to PNG image | |
pngDevice.process(page, "Page" + page.getNumber() + ".png"); | |
} |
PUB to TIFF Conversion Programmatically in Java
You can convert PUB file to a TIFF image with the steps listed below:
- Firstly, load the input Publisher (PUB) file.
- Convert PUB to PDF file using convertToPdf() method.
- Specify properties for TIFF image using TiffSettings class.
- Finally, convert PUB to TIFF image with TiffDevice instance.
The following code demonstrates how to convert PUB to TIFF image programmatically using Java:
// Load input PUB file | |
IPubParser parser = PubFactory.createParser("Test.pub"); | |
Document doc = parser.parse(); | |
// Convert PUB to PDF file | |
PubFactory.createPdfConverter().convertToPdf(doc, "Test.pdf"); | |
Document document = new Document("Test.pdf"); | |
facades.PdfFileInfo info = new facades.PdfFileInfo(document); | |
// Get page dimensions from the PDF document | |
int width = (int) info.getPageWidth((int)(1)); | |
int height = (int) info.getPageHeight((int)(1)); | |
devices.Resolution resolution = new devices.Resolution(300); | |
devices.TiffSettings settings = new devices.TiffSettings(); | |
settings.setCompression(devices.CompressionType.None); | |
settings.setDepth(devices.ColorDepth.Default); | |
// Create TIFF device with specified Width and Height | |
devices.TiffDevice tiffDevice = new devices.TiffDevice(width, height , resolution, settings); | |
// Convert PUB to TIFF image | |
tiffDevice.process(document, "Output.tiff"); |
Get Free API License
You can evaluate the API in its full capacity by requesting a Free Temporary License.
Conclusion
In conclusion, you have learned how to convert Microsoft Publisher, PUB, files to different image formats like JPG, PNG, TIFF, etc, programmatically using Java. Moreover, you can learn about more features by visiting the Documentation of Aspose.PUB for Java API. Furthermore, please feel free to contact us via the Free Support Forum for any of your concerns.