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

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

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

Chained Actions

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 ChainedActionsPDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("ChainedActionsPDF.pdf"));
      document.open();
      PdfAction action = PdfAction.javaScript("app.alert("Welcome at my site");\r", writer);
      action.next(new PdfAction("http://www.jexp.ru"));
      Paragraph p = new Paragraph(new Chunk("Click to go to a website").setAction(action));
      document.add(p);
    } catch (Exception de) {
      de.printStackTrace();
    }
    document.close();
  }
}





Named Actions in PDF Document

import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfAction;
import com.lowagie.text.pdf.PdfWriter;

public class NamedActionsPDF {
  public static void main(String[] args) {
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    try {
      PdfWriter writer = PdfWriter.getInstance(document,  new FileOutputStream("NamedActionsPDF.pdf"));
      document.open();
      Paragraph p = new Paragraph(new Chunk("Click to print").setAction(new PdfAction(PdfAction.PRINTDIALOG)));
      
      p.add(new Phrase(new Chunk("First Page").setAction(new PdfAction(PdfAction.FIRSTPAGE))));
      p.add(new Phrase(new Chunk("Prev Page").setAction(new PdfAction(PdfAction.PREVPAGE))));
      p.add(new Phrase(new Chunk("Next Page").setAction(new PdfAction(PdfAction.NEXTPAGE))));
      p.add(new Phrase(new Chunk("Last Page").setAction(new PdfAction(PdfAction.LASTPAGE))));
      
      for (int i = 1; i <= 10; ++i) {
        document.add(new Paragraph("This is page " + i));
        document.add(new Paragraph(Chunk.NEWPAGE));
        document.add(p);
        document.newPage();
      }
    } catch (Exception de) {
      de.printStackTrace();
    }
    document.close();
  }
}





PdfAction: Goto Another Pdf File

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 PdfActionGotoAnotherPdfFile {
  public static void main(String[] args) {
    Document document = new Document();
    Document remote = new Document();
    try {
      PdfWriter writer = PdfWriter.getInstance(document,
          new FileOutputStream("PdfActionGotoAnotherPdfFile.pdf"));
      PdfWriter.getInstance(remote, new FileOutputStream("PdfActionGotoAnotherPdfFileremote.pdf"));
      document.open();
      document.add(new Chunk("goto another document").setAction(PdfAction.gotoRemotePage("PdfActionGotoAnotherPdfFileremote.pdf", "test", false, true)));
      
      remote.open();
      remote.add(new Paragraph("Some remote document"));
      remote.newPage();
      Paragraph p = new Paragraph("This paragraph contains a ");
      p.add(new Chunk("local destination").setLocalDestination("test"));
      remote.add(p);
    } catch (Exception de) {
      de.printStackTrace();
    }
    document.close();
    remote.close();
  }
}





PdfAction: Goto Local Page 1

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 PdfActionGotoLocalPage1 {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("PdfActionGotoLocalPage1.pdf"));
      document.open();
      document.add(new Paragraph("Page 1"));
      document.newPage();
      document.add(new Paragraph("Page 2"));
      document.add(new Chunk("goto page 1").setAction(PdfAction.gotoLocalPage(1, new PdfDestination(PdfDestination.FITH,500), writer)));
    } catch (Exception de) {
      de.printStackTrace();
    }
    document.close();
  }
}





PdfAction: Goto Local Page 2

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 PdfActionGotoLocalPage {
  public static void main(String[] args) {
    Document document = new Document();
    Document remote = new Document();
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("PdfActionGotoLocalPage.pdf"));
      document.open();
      PdfAction action = PdfAction.gotoLocalPage(2, new PdfDestination(PdfDestination.XYZ, -1, 10000, 0), writer);
      writer.setOpenAction(action);
      document.add(new Paragraph("Page 1"));
      document.newPage();
      document.add(new Paragraph("Page 2"));
    } catch (Exception de) {
      de.printStackTrace();
    }
    document.close();
    remote.close();
  }
}





Pdf action: jump locally

import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Anchor;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class JumpLocalDestinationPDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("JumpLocalDestinationPDF.pdf"));
      
      document.open();
      Paragraph paragraph = new Paragraph();
      Anchor anchor1 = new Anchor("some text", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.UNDERLINE, new Color(0, 0, 255)));
      anchor1.setName("top");
      paragraph.add(anchor1);
      document.add(paragraph);
      
      Anchor anchor2 = new Anchor("please jump to a local destination", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new Color(0, 0, 255)));
      anchor2.setReference("#top");
      document.add(anchor2);
      
    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
    document.close();
  }
}





Pdf action: open application (notepad.exe)

import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfAction;
import com.lowagie.text.pdf.PdfWriter;
public class OpenApplicationPDF {
  public static void main(String[] args) {
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("OpenApplicationPDF.pdf"));
      document.open();
      String application = "notepad.exe";
      Paragraph p = new Paragraph(new Chunk("Click to open " + application).setAction(new PdfAction(application, null, null, null)));
      document.add(p);
    } catch (Exception de) {
      de.printStackTrace();
    }
    document.close();
  }
}