Friday, 9 October 2020

Convert PDF to Image and Convert Image to PDF in Java

By using Free Spire.PDF for Java, we can easily convert any specific page of PDF document to BMP and Metafile image in JAVA applications, and it also supports converting multiple image formats such as BMP, JPEG, GIF, PNG, TIFF and ICO to PDF. In this article, I’ll show you how to convert PDF to .png image and convert .jpg image to PDF for example.

Add Spire.Pdf.jar as a dependency

Before running codes, the thing you need to do is adding Spire.Pdf.jar to IDEA as a dependency. There are two methods to do it. One is that download Free Spire.PDF for Java package from the E-iceblue website, unzip it and manually add Spire.Pdf.jar in the “lib” folder to IDEA. The other is that installing Spire.PDF for Java from Maven repository. Create a Maven program, write down the following code snippets in your pom.xml file, and then click the button “Import Changes”.

<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <name>e-iceblue</name>
            <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
        </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf.free</artifactId>
        <version>3.9.0</version>
    </dependency>

</dependencies>

Using the code

Convert PDF to Image

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;
import com.spire.pdf.PdfDocument;
import javax.imageio.ImageIO;

public class ToImage {
   
public static void main(String[] args) throws IOException {
       
//load the sample PDF
       
PdfDocument doc = new PdfDocument();
        doc.loadFromFile(
"C:\\Users\\Test1\\Desktop\\Sample.pdf");
       
//save every page of PDF document to .png image
       
BufferedImage image;
       
for (int i = 0; i < doc.getPages().getCount(); i++) {
            image = doc.saveAsImage(i);
            File file =
new File( String.format("output/ToImage-img-%d.png", i));
            ImageIO.write(image,
"PNG", file);
        }
        doc.close();
    }
}

Output


Convert Image to PDF

There are three steps in all to realize the purpose of converting Image to PDF.

Step 1: Create a new PDF file and add a page in it.
Step 2: Load an image in any format, such as jpg, bmp, png, gif, tiff and ico. A .jpg image is loaded and its location
is set in this method.

Step 3: Save the image to PDF file in a specific path.

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.PdfImage;
import java.awt.geom.Rectangle2D;
public class ImageToPDF {
   
public static void main(String[] args) {
       
//Create a PdfDocument instance
       
PdfDocument pdf = new PdfDocument();
       
//Add a page
       
PdfPageBase page = pdf.getPages().add();
        
//Load the image
       
PdfImage image = PdfImage.fromFile("C:\\Users\\Test1\\Desktop\\Image.jpg");
       
//Draw the image to the specific rectangular area of the page
       
double widthFitRate = image.getPhysicalDimension().getWidth() / page.getCanvas().getClientSize().getWidth();
       
double heightFitRate = image.getPhysicalDimension().getHeight() / page.getCanvas().getClientSize().getHeight();
       
double fitRate = Math.max(widthFitRate, heightFitRate);
       
double fitWidth = image.getPhysicalDimension().getWidth() / fitRate;
       
double fitHeight = image.getPhysicalDimension().getHeight() / fitRate;
        page.getCanvas().drawImage(image,
new Rectangle2D.Double(0, 0, fitWidth, fitHeight));
       
//Save the resulting document
       
pdf.saveToFile("output/ConvertImageToPDF.pdf");
    }
}

Output



Conclusion

In addition to converting PDF to Image or Image to PDF, Free Spire.PDF for Java supports converting

PDF to XPS, SVG, Excel, Word, HTML and PDF/A or convert XPS, SVG, HTML to PDF in high

quality. You can find relevant tutorials from the link and please take a note on our Forum if there is any

question.



No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...