Java/PDF RTF/Destinations

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

Destinations Demo

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfDestination;
import com.lowagie.text.pdf.PdfOutline;
import com.lowagie.text.pdf.PdfWriter;
public class DestinationsPDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("DestinationsPDF.pdf"));
      writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
      document.open();
      PdfContentByte cb = writer.getDirectContent();
      PdfDestination d1 = new PdfDestination(PdfDestination.XYZ, 300, 800, 0);
      PdfDestination d2 = new PdfDestination(PdfDestination.FITH, 500);
      PdfDestination d3 = new PdfDestination(PdfDestination.FITR, 200, 300, 400, 500);
      PdfDestination d4 = new PdfDestination(PdfDestination.FITBV, 100);
      PdfDestination d5 = new PdfDestination(PdfDestination.FIT);
      PdfOutline out1 = new PdfOutline(cb.getRootOutline(), d1, "root");
      PdfOutline out2 = new PdfOutline(out1, d2, "sub 1");
      PdfOutline out3 = new PdfOutline(out1, d3, "sub 2");
      PdfOutline out4 = new PdfOutline(out2, d4, "sub 2.1");
      PdfOutline out5 = new PdfOutline(out2, d5, "sub 2.2");
      document.add(new Paragraph("adasd"));
      document.newPage();
    } catch (Exception de) {
      de.printStackTrace();
    }
    document.close();
  }
}





Setting up Local Destination

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfDestination;
import com.lowagie.text.pdf.PdfWriter;
public class LocalDestinationPDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("LocalDestinationPDF.pdf"));
      document.open();
      document.add(new Paragraph("Page 1"));
      document.newPage();
      document.add(new Paragraph("Page 2"));
      PdfContentByte cb = writer.getDirectContent();
      cb.localDestination("page2", new PdfDestination(PdfDestination.XYZ, -1, 10000, 0));
      writer.setOpenAction("page2");
    } catch (Exception de) {
      de.printStackTrace();
    }
    document.close();
  }
}