Java Tutorial/PDF/Action — различия между версиями

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

Текущая версия на 05:21, 1 июня 2010

document level JavaScript

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.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"));
    document.open();
    writer.addJavaScript("function saySomething(s) {app.alert(s)}", false);
    writer.setAdditionalAction(PdfWriter.DOCUMENT_CLOSE, PdfAction.javaScript(
        "saySomething("hi");\r", writer));
    document.add(new Paragraph("PDF document with a JavaScript function."));
    document.close();
  }
}





Generates a file with a launch action

import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfAction;
import com.lowagie.text.pdf.PdfWriter;
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 p = new Paragraph(new Chunk("Click to open test.txt in Notepad.")
        .setAction(new PdfAction("c:/windows/notepad.exe", "test.txt", "open", "c:\\")));
    document.add(p);
    document.close();
  }
}





Jump with PdfAction

import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfAction;
import com.lowagie.text.pdf.PdfDestination;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    Document remote = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    PdfWriter.getInstance(remote, new FileOutputStream("remote.pdf"));
    document.open();
    remote.open();
    PdfAction action = PdfAction.gotoLocalPage(2, new PdfDestination(PdfDestination.XYZ, -1, 10000,
        0), writer);
    writer.setOpenAction(action);
    document.add(new Paragraph("A"));
    document.newPage();
    document.add(new Paragraph("B"));
    document.add(new Chunk("go to page 1").setAction(PdfAction.gotoLocalPage(1, new PdfDestination(
        PdfDestination.FITH, 500), writer)));
    document.add(Chunk.NEWLINE);
    document.add(new Chunk("go to another document").setAction(PdfAction.gotoRemotePage(
        "remote.pdf", "test", false, true)));
    remote.add(new Paragraph("another"));
    remote.newPage();
    Paragraph p = new Paragraph("This paragraph contains a ");
    p.add(new Chunk("local destination").setLocalDestination("test"));
    remote.add(p);
    document.close();
    remote.close();
  }
}





named actions

import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfAction;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    PdfPTable table = new PdfPTable(4);
    table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
    table
        .addCell(new Phrase(new Chunk("First Page").setAction(new PdfAction(PdfAction.FIRSTPAGE))));
    table.addCell(new Phrase(new Chunk("Prev Page").setAction(new PdfAction(PdfAction.PREVPAGE))));
    table.addCell(new Phrase(new Chunk("Next Page").setAction(new PdfAction(PdfAction.NEXTPAGE))));
    table.addCell(new Phrase(new Chunk("Last Page").setAction(new PdfAction(PdfAction.LASTPAGE))));
    Paragraph p = new Paragraph(new Chunk("Click to print").setAction(new PdfAction(
        PdfAction.PRINTDIALOG)));
    for (int k = 1; k <= 30; ++k) {
      document.add(new Paragraph("This is page " + k));
      document.add(Chunk.NEWLINE);
      document.add(table);
      document.add(p);
      document.newPage();
    }
    document.close();
  }
}