Convert JPEG to PDF using Java

Convert JPEG to PDF using Java

JPEG and PDF are the two most widely used file formats at the current point in time. PNG and JPEG are raster image files whereas, PDF is based on vector files. However, you need to convert JPEG to PDF as the business requirements change. Therefore, we will use this enterprise-level PDF generator library to achieve the JPEG to PDF conversion. Moreover, it enables you to programmatically parse, process, and convert popular image file formats to PDF files. Let’s write the code snippet to convert JPEG to PDF using Java API.

We will cover the following sections:

PDF generator library installation

As far as the installation is concerned, you can find a complete installation guide to set up this library in your Java project. Please ensure that you have Java installed on your system. However, there are two ways either you can download the API or install it using the following Maven configurations.

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-pdf</artifactId>
    <classifier>jdk17</classifier>
</dependency>

Convert JPEG to PDF using Java

This PDF generator library allows developers to configure the API calls to meet business requirements.

You may follow the steps and the code snippet mentioned below:

  1. Create an object of the Document class.
  2. Call the getPages().add() method to add a page into the document.
  3. Instantiate an instance of the Image class.
  4. Load sample JPEG image file by calling the setFile method.
  5. Invoke the setFixWidth method to set the width of the image.
  6. Call the setFixHeight method to set the height of the image.
  7. Initialize the constructor of the TextFragment class and call the setTitle method to set the title of the JPEG image.
  8. setApplyResolution method sets a boolean value that indicates whether the image use resolution during generation.
  9. setBlackWhite method sets a boolean value that indicates whether the image is forced to be black-and-white.
  10. Invoke the getParagraphs().add(image) method to add the JPEG image to the page.
  11. Call the save method to save the output as a PDF document.

Copy & paste the following code into your main file:

You can see the output in the image below:

JPEG to PDF

Get a Free License

You can avail a free temporary license to try the API without evaluation limitations.

Summing up

We are ending this tutorial blog post here to hope that you have learned how to convert JPEG to PDF using Java programmatically. In addition, you may visit the documentation of this PDF generator library to learn the other features. This blog post will help you if you are looking to build a JPEG to PDF converter programmatically. Further, please visit conholdate.com for the latest updates.

Ask a question

You can let us know about your questions or queries on our forum.

FAQs

How to convert image to PDF using Java?

Install this PDF generator library to convert popular image formats to the PDF file format programmatically.

Can I convert JPG to PDF?

Please visit this link to know the code snippet that converts JPEG/JPG to the PDF file format in a Java application.

See Also