Do you want to convert image file formats to PDF file formats programmatically? In this blog post, we will learn how to convert BMP to PDF using Java. For this purpose, we will use an enterprise-level Java PDF library that offers a rich stack of features. In addition, we will write a code snippet that automates BMP to PDF conversion programmatically. Therefore, please ensure you have set up Java on your local machine before proceeding.
We will cover the following points:
BMP to PDF Conversion - Java API installation
The installation process of this Java PDF library takes a few seconds. This library is lightweight and provides methods to meet your business requirements. Therefore, 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 BMP Image to PDF using Java
Once the installation is completed, you may start writing the code to convert BMP to PDF programmatically.
Please follow the steps and the code snippet mentioned below:
- Instantiate an instance of the Document class.
- Invoke the getPages().add() method to add a page into the document.
- Create an object of the Image class.
- Load sample BMP image file by calling the setFile method.
- Call the setFixWidth method to set the width of the image.
- setFixHeight method will set the height of the image.
- Initialize the constructor of the TextFragment class and invoke the setTitle method to set the title of the 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.
- Call getParagraphs().add(image) method to add the image to the page.
- Invoke the save method to save the output as a PDF document
Copy & paste the following code into your main file to render BMP image to PDF document in Java:
// Instantiate an instance of Document class | |
Document document = new Document(); | |
// Invoke the getPages().add() method to add a page into the document | |
Page page = document.getPages().add(); | |
// Create an object of the Image class | |
Image image = new Image(); | |
// Load sample BMP image file by calling the setFile method | |
image.setFile( "Sample.bmp"); | |
// Call the setFixWidth method to set the width of the image | |
image.setFixWidth(100); | |
// setFixHeight method will set the height of the image | |
image.setFixHeight(100); | |
// Initialize the constructor of the TextFragment class and invoke the setTitle method to set the title of the image | |
image.setTitle(new TextFragment("image title")); | |
// setApplyResolution method sets a boolean value that indicates whether the image use resolution during generation | |
image.setApplyResolution(true); | |
// setBlackWhite method sets a boolean value that indicates whether the image is forced to be black-and-white. | |
image.setBlackWhite(false); | |
// Call getParagraphs().add(image) method to add the image to the page | |
page.getParagraphs().add(image); | |
// Invoke the save method to save the output as a PDF document | |
document.save("BMPtoPDF.pdf"); |
You can see the screenshot of BMP to PDF conversion in the image below:
Get a Free License
You can avail a free temporary license to try the API without evaluation limitations.
Summing up
We are ending this blog post here with the hope that you have learned how to convert BMP to PDF using Java programmatically. There are many other prominent methods available that you may find in the documentation. Moreover, this blog post will surely help you if you are looking to automate the BMP to PDF conversion. Moreover, conholdate.com is continuously writing new blog posts. Therefore, please stay in touch for the latest updates.
Ask a question
You can let us know about your questions or queries on our forum.
FAQs
How do I convert a BMP to PDF?
Install this Java PDF library to convert BMP files to PDF files programmatically in Java. In addition, there are many features available that offer file manipulation and conversion programmatically.
How do I convert a BMP Image?
Please follow this link to convert the BMP file to PDF in Java.