MainClass.java Source code

Java tutorial

Introduction

Here is the source code for MainClass.java

Source

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();
    }

}