Insert Images in PDF in Java

Image is an important part of PDF documents, and inserting images at appropriate positions on a PDF page can make the document more attractive. This article will share how to insert an image into an existing PDF document using Free Spire.PDF for Java.


This content originally appeared on DEV Community and was authored by carlwils

Image is an important part of PDF documents, and inserting images at appropriate positions on a PDF page can make the document more attractive. This article will share how to insert an image into an existing PDF document using Free Spire.PDF for Java.

Import Jar Dependency (2 Methods)

Method 1: Download the free library and unzip it. Then add the Spire.Pdf.jar file to your Java application as dependency.
Method 2: Directly add the jar dependency to maven project by adding the following configurations to the pom.xml file.

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

Sample Code

Free Spire.PDF for Java provides the PdfPageBase.getCanvas().drawImage() method to add images to the PDFs when creating as well as insert images into existing PDFs. The following code shows how to insert an image into an exixting PDF document, and to avoid covering the text content, I set the transparency to the inserted image.

import com.spire.pdf.*;
import com.spire.pdf.graphics.PdfImage;

public class InsertImage {
    public static void main(String[] args) {

        //Create PDF document
        PdfDocument pdf = new PdfDocument();

        //Load the PDF document from disk
        pdf.loadFromFile("D:\\Files\\input.pdf");

        //Get a specified page
        PdfPageBase page = pdf.getPages().get(0);

        //Load an image
        PdfImage image = PdfImage.fromFile("C:\\Users\\Administrator\\Desktop\\Olympics.jpg");

        //Set the width and height of image
        float width = image.getWidth() * 0.75f;
        float height = image.getHeight() * 0.75f;

        //Define a position to draw image
        double x = (page.getCanvas().getClientSize().getWidth()-width) /2;
        float y = 300f;

        //Set image transparency
        page.getCanvas().setTransparency(0.5f);

        //Draw image on page canvas
        page.getCanvas().drawImage(image, x, y, width, height);

        //Save the document
        pdf.saveToFile("insertImage.pdf", FileFormat.PDF);
        pdf.close();
    }
}

InsertPDFImage


This content originally appeared on DEV Community and was authored by carlwils


Print Share Comment Cite Upload Translate Updates
APA

carlwils | Sciencx (2022-04-27T06:29:16+00:00) Insert Images in PDF in Java. Retrieved from https://www.scien.cx/2022/04/27/insert-images-in-pdf-in-java/

MLA
" » Insert Images in PDF in Java." carlwils | Sciencx - Wednesday April 27, 2022, https://www.scien.cx/2022/04/27/insert-images-in-pdf-in-java/
HARVARD
carlwils | Sciencx Wednesday April 27, 2022 » Insert Images in PDF in Java., viewed ,<https://www.scien.cx/2022/04/27/insert-images-in-pdf-in-java/>
VANCOUVER
carlwils | Sciencx - » Insert Images in PDF in Java. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/04/27/insert-images-in-pdf-in-java/
CHICAGO
" » Insert Images in PDF in Java." carlwils | Sciencx - Accessed . https://www.scien.cx/2022/04/27/insert-images-in-pdf-in-java/
IEEE
" » Insert Images in PDF in Java." carlwils | Sciencx [Online]. Available: https://www.scien.cx/2022/04/27/insert-images-in-pdf-in-java/. [Accessed: ]
rf:citation
» Insert Images in PDF in Java | carlwils | Sciencx | https://www.scien.cx/2022/04/27/insert-images-in-pdf-in-java/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.