Java/PDF RTF/Outline

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

Outline Actions HyperLink

import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfAction;
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 OutlineActionsHyperLinkPDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("OutlineActionsHyperLinkPDF.pdf"));
      writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
      document.open();
      document.add(new Paragraph("Outline action example"));
      PdfContentByte cb = writer.getDirectContent();
      PdfOutline root = cb.getRootOutline();
      PdfOutline links = new PdfOutline(root, new PdfAction("http://www.jexp.ru"), "Useful links");
      links.setColor(new Color(0xFF, 0x00, 0x00));
      links.setStyle(Font.BOLD);
      
    } catch (Exception ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Outline Actions: JavaScript Code

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfAction;
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 OutlineActionsJavaScriptCodePDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("OutlineActionsJavaScriptCodePDF.pdf"));
      writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
      document.open();
      document.add(new Paragraph("Outline action example"));
      PdfContentByte cb = writer.getDirectContent();
      PdfOutline root = cb.getRootOutline();
      PdfOutline links = new PdfOutline(root, new PdfAction("http://www.jexp.ru"), "Useful links");
      PdfOutline other = new PdfOutline(root, new PdfDestination(PdfDestination.FIT), "other actions", false);
      other.setStyle(Font.ITALIC);
      new PdfOutline(other, PdfAction.javaScript("app.alert("Hello");\r", writer), "Say Hello");
      
    } catch (Exception ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Outline Actions Remote

import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfAction;
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 OutlineActionsRemotePDF {
  public static void main(String[] args) {
    Document document = new Document();
    Document remote = new Document();
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("OutlineActionsRemotePDF.pdf"));
      PdfWriter.getInstance(remote, new FileOutputStream("remote.pdf"));
      
      writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
      remote.open();
      document.open();
      document.add(new Paragraph("Outline action example"));
      PdfContentByte cb = writer.getDirectContent();
      PdfOutline root = cb.getRootOutline();
      PdfOutline links = new PdfOutline(root, new PdfAction("http://www.jexp.ru"), "Useful links");
      new PdfOutline(root, new PdfAction("remote.pdf", 1), "Go to yhe first page of a remote file");
      new PdfOutline(root, new PdfAction("remote.pdf", "test"), "Go to a local destination in a remote file");
      remote.add(new Paragraph("Text"));
      remote.newPage();
      Paragraph p = new Paragraph("This paragraph contains a ");
      p.add(new Chunk("local destination").setLocalDestination("test"));
      remote.add(p);
      
    } catch (Exception ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
    remote.close();
  }
}





Outline Actions with Subitems

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
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 OutlineActionsWithSubitemsPDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("OutlineActionsWithSubitemsPDF.pdf"));
      writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
      document.open();
      document.add(new Paragraph("Outline action example"));
      PdfContentByte cb = writer.getDirectContent();
      PdfOutline root = cb.getRootOutline();
      PdfOutline links = new PdfOutline(root, new PdfAction("http://www.jexp.ru"), "Useful links");
      new PdfOutline(links, new PdfAction("http://www.jexp.ru"), "www.jexp.ru");
      new PdfOutline(links, new PdfAction("http://www.jexp.ru/"), "PDF Demo");
      new PdfOutline(links, new PdfAction("http://www.jexp.ru"), "demo code");
      
    } catch (Exception ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Outline with Style

import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfAction;
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 OutlineWithStylePDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("OutlineWithStylePDF.pdf"));
      writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
      document.open();
      document.add(new Paragraph("Outline action example"));
      PdfContentByte cb = writer.getDirectContent();
      PdfOutline root = cb.getRootOutline();
      PdfOutline links = new PdfOutline(root, new PdfAction("http://www.jexp.ru"), "Useful links");
      links.setColor(new Color(0xFF, 0x00, 0x00));
      links.setStyle(Font.BOLD);
      
    } catch (Exception ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}