Java Tutorial/PDF/Outline

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

Paragraph Outlines

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfDestination;
import com.lowagie.text.pdf.PdfOutline;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass extends PdfPageEventHelper {
  private int n = 0;
  public void onParagraph(PdfWriter writer, Document document, float position) {
    PdfContentByte cb = writer.getDirectContent();
    new PdfOutline(cb.getRootOutline(), new PdfDestination(PdfDestination.FITH, position), "paragraph " + n++);
  }
  public static void main(String[] args) throws Exception {
    Document document = new Document(PageSize.A6);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(
        "2.pdf"));
    writer.setBoxSize("art", new Rectangle(document.left(), document.bottom(), document.right(),
        document.top()));
    writer.setViewerPreferences(PdfWriter.PageModeUseOutlines | PdfWriter.PageLayoutSinglePage);
    writer.setPageEvent(new MainClass());
    document.open();
    Paragraph p = new Paragraph("line");
    document.add(p);
    document.close();
  }
}





Pdf Outline

import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfAction;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfOutline;
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();
    document.add(new Chunk("asdf").setLocalDestination("Title"));
    PdfContentByte cb = writer.getDirectContent();
    PdfOutline root = cb.getRootOutline();
    PdfOutline links = new PdfOutline(root, new PdfAction(), "Useful links");
    links.setOpen(false);
    new PdfOutline(links, new PdfAction("http://www.jexp.ru"), "jexp.ru");
    PdfAction chained = PdfAction.javaScript("app.alert("alert");\r", writer);
    chained.next(new PdfAction("http://www.jexp.ru"));
    document.close();
  }
}
/**/