Embed Image in PDF with iText


How to embed images in PDF using iText Java Library?

✍: FYIcenter.com


Here is a tutorial for creating a PDF document with an embedded image using iText Java Library.

1. Create a Java file, EmbeddedImage.java:

 * An image embedded in text in PDF with iText
import com.itextpdf.kernel.pdf.*;
import com.itextpdf.layout.*;
import com.itextpdf.layout.element.*;
import com.itextpdf.io.image.*;
import java.io.*;

public class EmbeddedImage {
  public static void main(String args[]) throws IOException {
    PdfWriter writer = new PdfWriter("image.pdf");
    PdfDocument pdf = new PdfDocument(writer);
    Document document = new Document(pdf);
    Image fox = new Image(ImageDataFactory.create("fox.bmp"));
    Paragraph p = new Paragraph("The quick brown ")
      .add(" jumps over the lazy dog.");

2. Run the Java program iText 7 Java Library.

fyicenter$ java -cp java -cp kernel-7.1.4.jar:layout-7.1.4.jar: \
  io-7.1.4.jar:slf4j-api-1.7.31.jar \

fyicenter$ ls -l *.pdf 
  11683 image.pdf

3. View the new PDF, image.pdf, in a browser. You see a PDF page with an embedded image.

Embedded Image in PDF with iText Java Library
Embedded Image in PDF with iText Java Library


⇒ Draw Shape in PDF with iText

⇐ Bullet List in PDF with iText

⇑ Using iText Library in Java Programs

⇑⇑ iText for PDF Generation

2021-11-13, 305👍, 0💬