AddingAWTImagePDF.java Source code

Java tutorial

Introduction

Here is the source code for AddingAWTImagePDF.java

Source

import java.awt.Toolkit;
import java.io.FileOutputStream;

import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;

public class AddingAWTImagePDF {
    public static void main(String[] args) {
        Document document = new Document();
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("AddingAWTImagePDF.pdf"));
            document.open();
            PdfContentByte cb = writer.getDirectContent();
            java.awt.Image awtImage = Toolkit.getDefaultToolkit().createImage("logo.png");
            Image image = Image.getInstance(awtImage, null);
            image.setAbsolutePosition(100, 500);
            cb.addImage(image);
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        document.close();
    }
}