Merge PDF to PNG via Java
Sample code for concatenation PDF documents into single formats PNG on Java. Programmers can use this example code to placing PDF into PNG within any Web or Desktop Java based Application.
Merge PDF to PNG Using Java
In order to merge PDF to PNG, we’ll use Aspose.PDF for Java API which is a feature-rich, powerful, and easy to use concatenation API for Java platform. You can download its latest version directly from Maven and install it within your Maven-based project by adding the following configurations to the pom.xml.
Repository
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
Dependency
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>version of aspose-pdf API</version>
<classifier>jdk17</classifier>
</dependency>
How to merge PDF to PNG via Java
Java developers can easily load & merge PDF files to PNG in just a few lines of code.
- Initialize a new Document, and run a loop for merging files
- In loop: add a new page to PNG document
- In loop: add PDF file to new page
- After the loop save the result
System Requirements
Aspose.PDF for Javais supported on all major operating systems. Just make sure that you have the following prerequisites.
- Microsoft Windows or a compatible OS with Java Runtime Environment for JSP/JSF Application and Desktop Applications.
- Development environment like Eclipse or IntelliJ IDEA.
- Aspose.PDF for Java library referenced in your project.
This sample code shows PDF to PNG Java concatenation
// create empty image with calculated width and hight
// use file system as source for save image
fileSource = new com.aspose.imaging.sources.FileCreateSource(
"Merger_pdf_png.png",
false);
options = new com.aspose.imaging.imageoptions.PngOptions();
options.setSource(fileSource);
newImage = (com.aspose.imaging.fileformats.png.PngImage) com.aspose.imaging.Image.create(options, newWidth, newHeight);
int stitchedWidth = 0;
// read pdf file to Aspose Document
doc = new com.aspose.pdf.Document("1.pdf");
// setup default resolution to pdf documents 72dpi
// create image device to save document as image with page dimensions and resolution
// process document page to image
// create bounds to nsert small image into large
for (int pageCount = 1; pageCount <= doc.getPages().size(); pageCount++) {
resolution = new com.aspose.pdf.devices.Resolution(72);
imageDevice = new com.aspose.pdf.devices.PngDevice(
doc.getPages().get_Item(pageCount).getPageInfo().getWidth(),
doc.getPages().get_Item(pageCount).getPageInfo().getHeight(),
new com.aspose.pdf.devices.Resolution(72));
String outPath = "test_" + pageCount + ".png";
imageDevice.process(doc.getPages().get_Item(pageCount), outPath);
image = (com.aspose.imaging.RasterImage) com.aspose.imaging.Image.load(outPath);
bounds = new com.aspose.imaging.Rectangle(
stitchedWidth,
0,
image.getWidth(),
image.getHeight());
//combining images into new one
newImage.saveArgb32Pixels(bounds,image.loadArgb32Pixels(image.getBounds()));
stitchedWidth += image.getWidth();
}
// save created image to disk
newImage.save();
Combine PDF to PNG in Java
Convert or combine multiple PDF into single PNG file in Java is not straightforward task without using 3rd party library. This page shows how to merge multiple PDF files into a single PNG document using Aspose.PDF for Java. The example is written in Python but the API can be used in other programming languages. PDF files are merged such that the first one is joined at the end of the other document.
Repository
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
Dependency
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>version of aspose-pdf API</version>
<classifier>jdk17</classifier>
</dependency>