Merge PNG to PDF via Java
Sample code for concatenation PNG documents into single formats PDF on Java. Programmers can use this example code to placing PNG into PDF within any Web or Desktop Java based Application.
Merge PNG to PDF Using Java
In order to merge PNG to PDF, 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 PNG to PDF via Java
Java developers can easily load & merge PNG files to PDF 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 PDF document
- In loop: add PNG 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 PNG to PDF Java concatenation
// create empty pdf document
doc = new com.aspose.pdf.Document();
// make list of files with images to merge
List<String> images = Arrays.asList(new String[]{"1.png", "2.png"});
// add image to new pdf page
// add new page to pdf
for (String fs : images) {
page = doc.getPages().add();
page.addImage(fs, new com.aspose.pdf.Rectangle(0, 0, 700, 1000);
}
// save result pdf to file
doc.save("Merger_png_pdf.pdf", com.aspose.pdf.SaveFormat.Pdf);
Combine PNG to PDF in Java
Convert or combine multiple PNG into single PDF file in Java is not straightforward task without using 3rd party library. This page shows how to merge multiple PNG files into a single PDF 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>