Copy book mark : Bookmarks « PDF « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » PDF » Bookmarks 
29.33.5.Copy book markPrevious/Next
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[] argsthrows 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, 1null);
    bookmarks.addAll(tmp);
    reader = new PdfReader("HelloWorld3.pdf");
    copy.addPage(copy.getImportedPage(reader, 1));
    tmp = SimpleBookmark.getBookmark(reader);
    SimpleBookmark.shiftPageNumbers(tmp, 2null);
    bookmarks.addAll(tmp);
    copy.setOutlines(bookmarks);
    document.close();
  }

}
29.33.Bookmarks
29.33.1.Add bookmark
29.33.2.Read PDF File to retrieve the bookmarks
29.33.3.Set bookmark title
29.33.4.Pdf SimpleBookmark
29.33.5.Copy book mark
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.