As PDF is the most common portable document format used to exchange files, there arises the requirement to convert documents as well as images to PDF format without losing the quality. In this article, we will learn to programmatically convert JPG, PNG, GIF, and other images to PDF format using Java.
The following are the topics discussed briefly below:
- Image Conversion Java API
- Convert JPG Image to PDF
- Convert PNG, GIF, BMP Images to PDF
- Image Conversion to PDF with Options
Image Conversion Java API
For the conversion of images and documents within your Java applications, GroupDocs offers native, specialized GroupDocs.Conversion for Java API. It allows converting whole documents, specific pages, applying rotations, watermarks even on password-protected files. The API has a long list of documents and images supported file formats that can be converted back and forth.
Download and Configure
Get the conversion library from downloads or add the following pom.xml configuration in your Maven-based Java applications. After that, you can try examples of this article and many more available examples on GitHub. For the details, you may visit the API Reference.
<repository> <id>GroupDocsJavaAPI</id> <name>GroupDocs Java API</name> <url>http://repository.groupdocs.com/repo/</url> </repository>
<dependency> <groupId>com.groupdocs</groupId> <artifactId>groupdocs-conversion</artifactId> <version>21.4</version> </dependency>
Convert JPG to PDF in Java
To convert images to PDF format, there is a simple way. Let’s start with a JPG image and follow the steps to convert a JPG image to a PDF document.
- Load the JPG image using the Converter class.
- Convert the provided image to PDF using the convert method.
- Get the converted PDF image from the location it was saved.
The following code sample shows how to convert a JPG image to PDF using Java in just 2 lines of code.
Convert PNG, GIF, BMP Images to PDF in Java
The API is not restricted to just JPG images. It supports a wide range of image formats for their conversion to PDF in the same way. Whether it is PNG to PDF, GIF to PDF, BMP to PDF, or any other conversion, it can be performed likewise.
The following are the steps to convert any image to a PDF document.
- Load any image using the Converter class.
- Convert the provided image to PDF using the convert method.
The following code sample shows how to convert a PNG image to a PDF in the same way.
Image to PDF Conversion in Java with Options
The following are the steps to convert images to a PDF document with some customizations as per requirement. You can adjust margins, height, width, DPI, apply watermark, and some other options while converting the images to PDF format.
- Load the image using the Converter class.
- Initialize the PDF Conversion Options using PdfConvertOptions.
- Set the margins, height, width using respective methods.
- Apply watermark using WatermarkOptions.
- Convert the provided image to PDF with set options using the convert method.
The following code sample shows how to convert a JPG image to a PDF document using Java with options like; setting margins, specific size, apply watermark with rotation and transparency.
Get a Free API License
You can get a free temporary license to use the API without evaluation limitations.
Conclusion
In this article, you have learned how to convert the images to PDF format. Specifically, we discussed the conversion of JPG, PNG, BMP images to PDF using Java. Further, you have seen how to set margins, size, apply watermark while converting images PDF.
To explore more about the Java Conversion API, you can consult the documentation. For any queries, reach us via the forum.