Java Tutorial/PDF/Bookmarks

Материал из Java эксперт
Перейти к: навигация, поиск

Add bookmark

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.BaseFont;
import com.lowagie.text.pdf.PdfAnnotation;
import com.lowagie.text.pdf.PdfContentByte;
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 {
    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
    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);
    cb.beginText();
    cb.setFontAndSize(bf, 18);
    cb.setTextMatrix(36, 770);
    cb.showText("Inserted Title Page");
    cb.endText();
    List list = SimpleBookmark.getBookmark(reader);
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("Title", "Title Page");
    map.put("Action", "GoTo");
    map.put("Page", "1 FitH 806");
    list.add(0, map);
    stamper.setOutlines(list);
    stamper.close();
  }
}





Copy book mark

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





Pdf SimpleBookmark

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
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.Rectangle;
import com.lowagie.text.Section;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfAnnotation;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
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();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    Paragraph hello = new Paragraph("asdf");
    Chapter universe = new Chapter("A", 1);
    Section section;
    section = universe.addSection("B");
    section.add(hello);
    section = universe.addSection("C");
    section.add(hello);
    document.close();
    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
    PdfReader reader = new PdfReader("2.pdf");
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(
        "HelloWorldManipulateBookmarks.pdf"));
    stamper.insertPage(1, PageSize.A4);
    PdfContentByte cb = stamper.getOverContent(1);
    cb.beginText();
    cb.setFontAndSize(bf, 18);
    cb.setTextMatrix(36, 770);
    cb.showText("Inserted Title Page");
    cb.endText();
    stamper.addAnnotation(PdfAnnotation.createText(stamper.getWriter(), new Rectangle(30f, 750f,
        80f, 800f), "inserted page", "This page is the title page.", true, null), 1);
    List list = SimpleBookmark.getBookmark(reader);
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("Title", "Title Page");
    ArrayList kids = new ArrayList();
    HashMap<String, String> kid1 = new HashMap<String, String>();
    kid1.put("Title", "top");
    kid1.put("Action", "GoTo");
    kid1.put("Page", "1 ");
    kids.add(kid1);
    HashMap<String, String> kid2 = new HashMap<String, String>();
    kid2.put("Title", "bottom");
    kid2.put("Action", "GoTo");
    kid2.put("Page", "6");
    kids.add(kid2);
    map.put("Kids", kids);
    list.add(0, map);
    SimpleBookmark.exportToXML(list, new FileOutputStream("manipulated_bookmarks.xml"),
        "ISO8859-1", true);
    stamper.setOutlines(list);
    stamper.close();
  }
}





Read PDF File to retrieve the bookmarks

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.SimpleBookmark;
public class HelloWorldBookmarks {
  public static void main(String[] args) throws Exception {
    PdfReader reader = new PdfReader("HelloWorldRead.pdf");
    List list = SimpleBookmark.getBookmark(reader);
    for (Iterator i = list.iterator(); i.hasNext();) {
      showBookmark((Map) i.next());
    }
  }
  private static void showBookmark(Map bookmark) {
    System.out.println(bookmark.get("Title"));
    ArrayList kids = (ArrayList) bookmark.get("Kids");
    if (kids == null)
      return;
    for (Iterator i = kids.iterator(); i.hasNext();) {
      showBookmark((Map) i.next());
    }
  }
}





Set bookmark title

import java.io.FileOutputStream;
import com.lowagie.text.ChapterAutoNumber;
import com.lowagie.text.Document;
import com.lowagie.text.Phrase;
import com.lowagie.text.Section;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
    document.open();
    Phrase text = new Phrase("test. ");
    ChapterAutoNumber chapter1 = new ChapterAutoNumber("This is a sample sentence:");
    chapter1.setBookmarkTitle("The fox");
    chapter1.setBookmarkOpen(false);
    Section section1 = chapter1.addSection("Quick");
    section1.add(text);
    document.add(chapter1);
    ChapterAutoNumber chapter2 = new ChapterAutoNumber("Jumps");
    Section section = chapter2.addSection("Over");
    section.add(text);
    document.add(chapter2);
    document.close();
  }
}