Create PdfImportedPage : PDF Stamper « PDF « Java Tutorial






import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;

import com.lowagie.text.PageSize;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfAnnotation;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.SimpleBookmark;

public class MainClass {
  public static void main(String[] args) throws Exception {
    PdfReader reader = new PdfReader("test.pdf");
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("my.pdf"));
    stamper.insertPage(1, PageSize.A4);
    PdfContentByte cb = stamper.getOverContent(1);
    PdfImportedPage p = stamper.getImportedPage(reader, 2);
    cb.addTemplate(p, 0.4f, 0f, 0f, 0.4f, 36f, 450);
    p = stamper.getImportedPage(reader, 3);
    cb.addTemplate(p, 0.4f, 0f, 0f, 0.4f, 300f, 450);
    p = stamper.getImportedPage(reader, 4);
    cb.addTemplate(p, 0.4f, 0f, 0f, 0.4f, 36f, 100);
    stamper.addAnnotation(PdfAnnotation.createText(stamper.getWriter(), new Rectangle(300f, 350f,
        500f, 450f), "inserted page", "This page contains a copy of the next three pages.", true,
        null), 1);
    List list = SimpleBookmark.getBookmark(reader);
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("Title", "title content");
    map.put("Action", "GoTo");
    map.put("Page", "1 FitH 806");
    list.add(0, map);
    stamper.setOutlines(list);
    stamper.close();
  }

}








29.3.PDF Stamper
29.3.1.Stamping PDF file
29.3.2.Import page
29.3.3.Copy a selection of pages to new PDF files
29.3.4.Create PdfImportedPage