Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Itext: Image object

Image Class is used to add images to PDF Document. Let me try to add some image to PDF document.

Image image1 = Image.getInstance("lion.jpeg");
document.add(image1);

Above statements adds an image to PDF document.
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class TestImage {
public static void main(String args[]) throws DocumentException, MalformedURLException, IOException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("my_images.pdf"));
document.open();

Image image1 = Image.getInstance("lion.jpeg");
document.add(image1);

document.add(new Paragraph("\n\n"));

String imageUrl = "http://2.bp.blogspot.com/-99yjvVtjtD8/U23y9NqTgJI/AAAAAAAAAeg/P7lK4TwEoXM/s1600/Flying+Car.jpg";
Image image2 = Image.getInstance(new URL(imageUrl));
image2.scalePercent(40);
document.add(image2);

document.close();
}
}


Run above application, you can able to see following kind of pdf document.




Previous                                                 Next                                                 Home


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Itext: Image object

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×