Import pages as an Image : PDF Page « PDF « Java Tutorial






import java.io.FileOutputStream;

import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.GrayColor;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;

public class MainClass {

  public static void main(String[] args) throws Exception {
    Document document = new Document(PageSize.A4);
    PdfReader reader = new PdfReader("HelloWorldToImport.pdf");
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(
        "HelloWorldImportedPages.pdf"));
    document.open();
    document.add(new Paragraph("This is page 1:"));
    PdfImportedPage page = writer.getImportedPage(reader, 1);
    Image image = Image.getInstance(page);
    image.scalePercent(15f);
    image.setBorder(Rectangle.BOX);
    image.setBorderWidth(3f);
    image.setBorderColor(new GrayColor(0.5f));
    document.add(image);
    document.add(new Paragraph("This is page 2:"));
    page = writer.getImportedPage(reader, 2);
    image = Image.getInstance(page);
    image.scalePercent(15f);
    image.setBorder(Rectangle.BOX);
    image.setBorderWidth(3f);
    image.setBorderColor(new GrayColor(0.5f));
    document.add(image);
    document.add(new Paragraph("This is page 3:"));
    page = writer.getImportedPage(reader, 4);
    image = Image.getInstance(page);
    image.scalePercent(15f);
    image.setBorder(Rectangle.BOX);
    image.setBorderWidth(3f);
    image.setBorderColor(new GrayColor(0.5f));
    document.add(image);
    System.out.println("Tampered? " + reader.isTampered());
    document.close();
  }
}








29.9.PDF Page
29.9.1.Add a new page
29.9.2.Import pages as an Image