Convert Images to PDF in Java

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 images to PDF format in Java.

Convert Images to PDF using Java

The following are the topics discussed briefly below:

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, apply 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 as well the many more example available 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 Image to PDF in Java

Mountain JPG Image

Any image can be converted to PDF, whether it is JPG to PDF, PNG to PDF, or WebP to PDF conversion, it can be performed likewise.

The following are the steps to convert any image to a PDF document.

  • Load the image using the Converter class.
  • Convert the provided image to PDF using the convert method.

The following code sample shows how to convert any image to PDF using Java in just 2 lines of code.

Convert Image to PDF with Options in Java

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.

Converted JPG to PDF
  • 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.

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. 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

See Also