MainClass.java Source code

Java tutorial

Introduction

Here is the source code for MainClass.java

Source

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

import com.lowagie.text.Chapter;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Section;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.SimpleBookmark;

public class MainClass {
    public static void main(String[] args) throws Exception {
        Document document = new Document(PageSize.A4);
        Paragraph hello = new Paragraph("(English:) hello, ");
        PdfWriter.getInstance(document, new FileOutputStream("HelloWorld1.pdf"));
        document.open();
        Chapter universe = new Chapter("asdf", 1);
        Section section;
        document.add(universe);

        document.close();
        document = new Document(PageSize.A4);

        PdfWriter.getInstance(document, new FileOutputStream("HelloWorld2.pdf"));
        document.open();
        Chapter people = new Chapter("asdf", 2);
        section = people.addSection("B");
        section.add(hello);
        document.add(people);

        document.close();
        document = new Document(PageSize.A4);

        PdfWriter.getInstance(document, new FileOutputStream("HelloWorld3.pdf"));
        document.open();
        Chapter animals = new Chapter("asdf", 3);
        section = animals.addSection("B");
        section.add(hello);
        document.add(animals);
        document.close();

        ArrayList bookmarks = new ArrayList();
        PdfReader reader = new PdfReader("HelloWorld1.pdf");
        document = new Document(reader.getPageSizeWithRotation(1));
        PdfCopy copy = new PdfCopy(document, new FileOutputStream("HelloWorldCopyBookmarks.pdf"));
        document.open();
        copy.addPage(copy.getImportedPage(reader, 1));
        bookmarks.addAll(SimpleBookmark.getBookmark(reader));
        reader = new PdfReader("HelloWorld2.pdf");
        copy.addPage(copy.getImportedPage(reader, 1));
        List tmp = SimpleBookmark.getBookmark(reader);
        SimpleBookmark.shiftPageNumbers(tmp, 1, null);
        bookmarks.addAll(tmp);
        reader = new PdfReader("HelloWorld3.pdf");
        copy.addPage(copy.getImportedPage(reader, 1));
        tmp = SimpleBookmark.getBookmark(reader);
        SimpleBookmark.shiftPageNumbers(tmp, 2, null);
        bookmarks.addAll(tmp);
        copy.setOutlines(bookmarks);
        document.close();
    }

}