Search in sources :

Example 11 with PdfAction

use of com.lowagie.text.pdf.PdfAction in project OpenPDF by LibrePDF.

the class Add3D method AddButton.

public static void AddButton(float x, float y, String fname, String js, String image, PdfWriter wr) {
    try {
        // URL url=Add3D.class.getResource(
        // image);
        // PdfFileSpecification fs=PdfFileSpecification.fileEmbedded(wr,image,image,null);
        // wr.addAnnotation(PdfAnnotation.createScreen(wr,new Rectangle(x, y, x + img.plainWidth(),
        // y + img.plainHeight())));
        Image img = Image.getInstance(image);
        PushbuttonField bt = new PushbuttonField(wr, new Rectangle(x, y, x + img.getPlainWidth(), y + img.getPlainHeight()), fname);
        bt.setLayout(PushbuttonField.LAYOUT_ICON_ONLY);
        bt.setImage(img);
        PdfFormField ff = bt.getField();
        PdfAction ac = PdfAction.javaScript(js, wr);
        ff.setAction(ac);
        wr.addAnnotation(ff);
    } catch (IOException | DocumentException ignored) {
    // ignored
    }
}
Also used : PdfAction(com.lowagie.text.pdf.PdfAction) PdfFormField(com.lowagie.text.pdf.PdfFormField) DocumentException(com.lowagie.text.DocumentException) Rectangle(com.lowagie.text.Rectangle) IOException(java.io.IOException) Image(com.lowagie.text.Image) PushbuttonField(com.lowagie.text.pdf.PushbuttonField)

Example 12 with PdfAction

use of com.lowagie.text.pdf.PdfAction in project OpenPDF by LibrePDF.

the class OptionalContent method main.

/**
 * Demonstrates the use of layers.
 * @param args no arguments needed
 */
public static void main(String[] args) {
    try {
        System.out.println("Optional content");
        // step 1: creation of a document-object
        Document document = new Document(PageSize.A4, 50, 50, 50, 50);
        // step 2: creation of the writer
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("optionalcontent.pdf"));
        writer.setPdfVersion(PdfWriter.VERSION_1_5);
        writer.setViewerPreferences(PdfWriter.PageModeUseOC);
        // step 3: opening the document
        document.open();
        // step 4: content
        PdfContentByte cb = writer.getDirectContent();
        Phrase explanation = new Phrase("Automatic layers, form fields, images, templates and actions", new Font(Font.HELVETICA, 18, Font.BOLD, Color.red));
        ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, explanation, 50, 650, 0);
        PdfLayer l1 = new PdfLayer("Layer 1", writer);
        PdfLayer l2 = new PdfLayer("Layer 2", writer);
        PdfLayer l3 = new PdfLayer("Layer 3", writer);
        PdfLayer l4 = new PdfLayer("Form and XObject Layer", writer);
        PdfLayerMembership m1 = new PdfLayerMembership(writer);
        m1.addMember(l2);
        m1.addMember(l3);
        Phrase p1 = new Phrase("Text in layer 1");
        Phrase p2 = new Phrase("Text in layer 2 or layer 3");
        Phrase p3 = new Phrase("Text in layer 3");
        cb.beginLayer(l1);
        ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p1, 50, 600, 0f);
        cb.endLayer();
        cb.beginLayer(m1);
        ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p2, 50, 550, 0);
        cb.endLayer();
        cb.beginLayer(l3);
        ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p3, 50, 500, 0);
        cb.endLayer();
        TextField ff = new TextField(writer, new Rectangle(200, 600, 300, 620), "field1");
        ff.setBorderColor(Color.blue);
        ff.setBorderStyle(PdfBorderDictionary.STYLE_SOLID);
        ff.setBorderWidth(TextField.BORDER_WIDTH_THIN);
        ff.setText("I'm a form field");
        PdfFormField form = ff.getTextField();
        form.setLayer(l4);
        writer.addAnnotation(form);
        Image img = Image.getInstance("pngnow.png");
        img.setLayer(l4);
        img.setAbsolutePosition(200, 550);
        cb.addImage(img);
        PdfTemplate tp = cb.createTemplate(100, 20);
        Phrase pt = new Phrase("I'm a template", new Font(Font.HELVETICA, 12, Font.NORMAL, Color.magenta));
        ColumnText.showTextAligned(tp, Element.ALIGN_LEFT, pt, 0, 0, 0);
        tp.setLayer(l4);
        tp.setBoundingBox(new Rectangle(0, -10, 100, 20));
        cb.addTemplate(tp, 200, 500);
        List<Object> state = new ArrayList<>();
        state.add("toggle");
        state.add(l1);
        state.add(l2);
        state.add(l3);
        state.add(l4);
        PdfAction action = PdfAction.setOCGstate(state, true);
        Chunk ck = new Chunk("Click here to toggle the layers", new Font(Font.HELVETICA, 18, Font.NORMAL, Color.yellow)).setBackground(Color.blue).setAction(action);
        ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, new Phrase(ck), 250, 400, 0);
        cb.sanityCheck();
        // step 5: closing the document
        document.close();
    } catch (Exception de) {
        de.printStackTrace();
    }
}
Also used : PdfAction(com.lowagie.text.pdf.PdfAction) PdfFormField(com.lowagie.text.pdf.PdfFormField) PdfWriter(com.lowagie.text.pdf.PdfWriter) PdfLayer(com.lowagie.text.pdf.PdfLayer) Rectangle(com.lowagie.text.Rectangle) ArrayList(java.util.ArrayList) Phrase(com.lowagie.text.Phrase) Document(com.lowagie.text.Document) Image(com.lowagie.text.Image) Chunk(com.lowagie.text.Chunk) PdfTemplate(com.lowagie.text.pdf.PdfTemplate) Font(com.lowagie.text.Font) FileOutputStream(java.io.FileOutputStream) TextField(com.lowagie.text.pdf.TextField) PdfContentByte(com.lowagie.text.pdf.PdfContentByte) PdfLayerMembership(com.lowagie.text.pdf.PdfLayerMembership)

Example 13 with PdfAction

use of com.lowagie.text.pdf.PdfAction in project OpenPDF by LibrePDF.

the class OutlineActions method main.

/**
 * Demonstrates some PageLabel functionality.
 *
 * @param args no arguments needed here
 */
public static void main(String[] args) {
    System.out.println("Outlines with actions");
    // step 1: creation of a document-object
    Document document = new Document();
    Document remote = new Document();
    try {
        // step 2:
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("OutlineActions.pdf"));
        PdfWriter.getInstance(remote, new FileOutputStream("remote.pdf"));
        // step 3:
        writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
        document.open();
        remote.open();
        // step 4:
        // we add some content
        document.add(new Paragraph("Outline action example"));
        // we add the outline
        PdfContentByte cb = writer.getDirectContent();
        PdfOutline root = cb.getRootOutline();
        PdfOutline links = new PdfOutline(root, new PdfAction("https://github.com/LibrePDF/OpenPDFlinks.html"), "Useful links");
        links.setColor(new Color(0x00, 0x80, 0x80));
        links.setStyle(Font.BOLD);
        new PdfOutline(links, new PdfAction("http://www.lowagie.com/iText"), "Bruno's iText site");
        new PdfOutline(links, new PdfAction("http://itextpdf.sourceforge.net/"), "Paulo's iText site");
        new PdfOutline(links, new PdfAction("http://sourceforge.net/projects/itext/"), "iText @ SourceForge");
        PdfOutline other = new PdfOutline(root, new PdfDestination(PdfDestination.FIT), "other actions", false);
        other.setStyle(Font.ITALIC);
        new PdfOutline(other, new PdfAction("remote.pdf", 1), "Go to yhe first page of a remote file");
        new PdfOutline(other, new PdfAction("remote.pdf", "test"), "Go to a local destination in a remote file");
        new PdfOutline(other, PdfAction.javaScript("app.alert('Hello');\r", writer), "Say Hello");
        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 (DocumentException | IOException de) {
        System.err.println(de.getMessage());
    }
    // step 5: we close the document
    document.close();
    remote.close();
}
Also used : PdfAction(com.lowagie.text.pdf.PdfAction) PdfWriter(com.lowagie.text.pdf.PdfWriter) Color(java.awt.Color) PdfDestination(com.lowagie.text.pdf.PdfDestination) IOException(java.io.IOException) Document(com.lowagie.text.Document) Chunk(com.lowagie.text.Chunk) Paragraph(com.lowagie.text.Paragraph) FileOutputStream(java.io.FileOutputStream) DocumentException(com.lowagie.text.DocumentException) PdfContentByte(com.lowagie.text.pdf.PdfContentByte) PdfOutline(com.lowagie.text.pdf.PdfOutline)

Example 14 with PdfAction

use of com.lowagie.text.pdf.PdfAction in project OpenPDF by LibrePDF.

the class NamedActions method main.

/**
 * Creates a document with Named Actions.
 *
 * @param args
 *            no arguments needed
 */
public static void main(String[] args) {
    System.out.println("Named Actions");
    // step 1: creation of a document-object
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    try {
        // step 2: we create a writer that listens to the document
        PdfWriter.getInstance(document, new FileOutputStream("NamedActions.pdf"));
        // step 3: we open the document
        document.open();
        // step 4: we add some content
        Paragraph p = new Paragraph(new Chunk("Click to print").setAction(new PdfAction(PdfAction.PRINTDIALOG)));
        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))));
        for (int k = 1; k <= 10; ++k) {
            document.add(new Paragraph("This is page " + k));
            document.add(Chunk.NEWLINE);
            document.add(table);
            document.add(p);
            document.newPage();
        }
    } catch (Exception de) {
        de.printStackTrace();
    }
    // step 5: we close the document
    document.close();
}
Also used : PdfAction(com.lowagie.text.pdf.PdfAction) PdfPTable(com.lowagie.text.pdf.PdfPTable) FileOutputStream(java.io.FileOutputStream) Phrase(com.lowagie.text.Phrase) Document(com.lowagie.text.Document) Chunk(com.lowagie.text.Chunk) Paragraph(com.lowagie.text.Paragraph)

Example 15 with PdfAction

use of com.lowagie.text.pdf.PdfAction in project OpenPDF by LibrePDF.

the class OpenApplication method main.

/**
 * Creates a document with Named Actions.
 *
 * @param args
 *            the system root (for instance "C:\windows\")
 */
public static void main(String[] args) {
    System.out.println("Open Application");
    // step 1: creation of a document-object
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    try {
        // step 2: we create a writer that listens to the document
        PdfWriter.getInstance(document, new FileOutputStream("OpenApplication.pdf"));
        // step 3: we open the document
        document.open();
        // step 4: we add some content
        String application = args[0] + "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();
    }
    // step 5: we close the document
    document.close();
}
Also used : PdfAction(com.lowagie.text.pdf.PdfAction) FileOutputStream(java.io.FileOutputStream) Document(com.lowagie.text.Document) Chunk(com.lowagie.text.Chunk) Paragraph(com.lowagie.text.Paragraph)

Aggregations

PdfAction (com.lowagie.text.pdf.PdfAction)21 Document (com.lowagie.text.Document)13 Chunk (com.lowagie.text.Chunk)12 Paragraph (com.lowagie.text.Paragraph)10 PdfWriter (com.lowagie.text.pdf.PdfWriter)9 PdfContentByte (com.lowagie.text.pdf.PdfContentByte)8 FileOutputStream (java.io.FileOutputStream)7 PdfDestination (com.lowagie.text.pdf.PdfDestination)6 Rectangle (com.lowagie.text.Rectangle)5 Test (org.junit.Test)5 DocumentException (com.lowagie.text.DocumentException)4 Phrase (com.lowagie.text.Phrase)4 IOException (java.io.IOException)4 Image (com.lowagie.text.Image)3 PdfFormField (com.lowagie.text.pdf.PdfFormField)3 Font (com.lowagie.text.Font)2 PdfAnnotation (com.lowagie.text.pdf.PdfAnnotation)2 PdfFileSpecification (com.lowagie.text.pdf.PdfFileSpecification)2 PdfLayer (com.lowagie.text.pdf.PdfLayer)2 PdfLayerMembership (com.lowagie.text.pdf.PdfLayerMembership)2