How to Convert GIF to PDF in Java

How to Convert GIF to PDF in Java

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 Java library to programmatically build a GIF to PDF converter. Automating the process of GIF to PDF conversion will give your business application a competitive edge.

You will go through the following points:

GIF to PDF Conversion - Java 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 in Java.

You may follow the steps given below to convert GIF to PDF in Java:

  1. Instantiate an instance of the Document class.
  2. Initializes an object of the DocumentBuilder class and binds the Document object.
  3. Now, load the source GIF file and create an image stream by calling the createImageInputStream method that takes input from the File Object.
  4. Create an object of the ImageReader class, and 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.
  5. Invoke the setInput method that sets the input source to use to the given ImageInputStream or other object.
  6. Get the number of frames in the image by calling the getNumImages method.
  7. Loop through all frames.
  8. Insert a section break before each new page, in case of a multi-frame image by calling the insertBreak method.
  9. Select the active frame by calling the read method.
  10. Get the current page setup by calling the getPageSetup method and assigning it to the PageSetup class object.
  11. Invoke the setPageWidth method to set the page width.
  12. setPageHeight method will save the page height.
  13. Call the insertImage method to insert the image into the document and position it at the top left corner of the page.
  14. Save the file in PDF file format by invoking the save method.

Copy & paste the following code into your main file to convert GIF to PDF in Java:

The output can be seen in the image below:

GIF to PDF converter

Get a Free License

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

Summing up

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 in Java. Moreover, you can visit the documentation to learn 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.

FAQs

How do I convert a GIF to PDF in Java?

You can perform GIF to PDF conversion programmatically using this Java library. Further, please visit this link to get the cope snippet.

See Also