Add an image into PDF document in iText
The following example demonstrate how to add an image into a PDF document using the iText library. Image is created using the com.itextpdf.text.Image class. To create an instance of image we can use the Image.getInstance() method. Below we create an image from an image file name an a URL that point to an image resource.
package org.brudvik.example.itextpdf;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileOutputStream;
import java.io.IOException;
public class ImageDemo {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document,
new FileOutputStream("ImageDemo.pdf"));
document.open();
//
// Creating image by file name
//
String filename = "other-sample/src/main/resources/java.gif";
Image image = Image.getInstance(filename);
document.add(image);
//
// Creating image from a URL
//
String url = "http://localhost/xampp/img/xampp-logo-new.gif";
image = Image.getInstance(url);
document.add(image);
} catch (DocumentException | IOException e) {
e.printStackTrace();
} finally {
document.close();
}
}
}
Do you know better ways to do this?

Anbefalte lenker
Aktivitet
Siste kommentarer