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.
This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here