The PDF file format is one of the most popular vector-based files. On the other hand, GIF is based on raster images and offers a great deal when it comes to showing images as an animation. In this blog post, you will learn how to convert GIF to PDF in Java application. Therefore, you will install this document processing library to programmatically build a GIF to PDF converter. Automating the process of GIF to PDF conversion will definitely give your business application a competitive edge.
You will go through the following points:
GIF to PDF conversion - API installation
This document processing library is very simple to install in your Java project. However, you can download the JAR file or install this library using the following Maven configurations.
<repository> <id>AsposeJavaAPI</id> <name>Aspose Java API</name> <url>https://releases.aspose.com/java/repo/</url> </repository> <dependency> <groupId>com.aspose</groupId> <artifactId>aspose-words</artifactId> <version>22.10</version> <classifier>jdk17</classifier> </dependency>
How to Convert GIF to PDF in Java
Once the library is installed, you can start writing the source code to build a GIF to PDF converter programmatically.
You may follow the steps and the code snippet given below:
- Instantiate an instance of the Document class.
- Initializes an object of the DocumentBuilder class and bind the Document object.
- Now, load the source GIF file and create an image stream by calling the createImageInputStream method that takes input from the File Object.
- Create an object of the ImageReader class, access the method getImageReaders that returns an Iterator containing all currently registered ImageReaders that claim to be able to decode the supplied Object, typically an ImageInputStream.
- Invoke the setInput method that sets the input source to use to the given ImageInputStream or other object.
- Get the number of frames in the image by calling the getNumImages method.
- Loop through all frames.
- Insert a section break before each new page, in case of a multi-frame image by calling the insertBreak method.
- Select the active frame by calling the read method.
- Get the current page setup by calling the getPageSetup method and assigning it to the PageSetup class object.
- Invoke the setPageWidth method to set the page width.
- setPageHeight method will save the page height.
- Call the insertImage method to insert the image into the document and position it at the top left corner of the page.
- Save the file in PDF file format by invoking the save method.
Copy & paste the following code into your main file:
The output can be seen 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 blog post here. you have gone through how to convert GIF to PDF in Java using a few lines of source code. In addition, you may install this document processing library to build a GIF to PDF converter programmatically. Moreover, you can visit the documentation to know other interesting features.
Moreover, we suggest you follow our Getting Started guide.
Finally, conholdate.com is writing new blog posts. So, please stay in touch for regular updates.
Ask a question
You can let us know about your questions or queries on our forum.
How do I convert a GIF to PDF?