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:
- Create an object of the Document class.
- Call the getPages().add() method to add a page into the document.
- Instantiate an instance of the Image class.
- Load sample JPEG image file by calling the setFile method.
- Invoke the setFixWidth method to set the width of the image.
- Call the setFixHeight method to set the height of the image.
- Initialize the constructor of the TextFragment class and call the setTitle method to set the title of the JPEG image.
- setApplyResolution method sets a boolean value that indicates whether the image use resolution during generation.
- setBlackWhite method sets a boolean value that indicates whether the image is forced to be black-and-white.
- Invoke the getParagraphs().add(image) method to add the JPEG image to the page.
- 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:
Get a Free License
You can avail a free temporary license to try the API without evaluation limitations.
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.
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.