Search in sources :

Example 1 with PdfAction

use of com.lowagie.text.pdf.PdfAction in project itext2 by albfernandez.

the class OutlineActionsTest method main.

/**
 * Demonstrates some PageLabel functionality.
 */
@Test
public void main() throws Exception {
    // step 1: creation of a document-object
    Document document = new Document();
    Document remote = new Document();
    // step 2:
    PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("OutlineActions.pdf"));
    PdfWriter.getInstance(remote, PdfTestBase.getOutputStream("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("http://www.lowagie.com/iText/links.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);
    // 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) PdfContentByte(com.lowagie.text.pdf.PdfContentByte) Document(com.lowagie.text.Document) Chunk(com.lowagie.text.Chunk) Paragraph(com.lowagie.text.Paragraph) PdfOutline(com.lowagie.text.pdf.PdfOutline) Test(org.junit.Test)

Example 2 with PdfAction

use of com.lowagie.text.pdf.PdfAction in project itext2 by albfernandez.

the class ActionsTest method main.

/**
 * Creates a document with some goto actions.
 */
@Test
public void main() throws Exception {
    // step 1: creation of a document-object
    Document document = new Document();
    Document remote = new Document();
    // step 2:
    PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("Actions.pdf"));
    PdfWriter.getInstance(remote, PdfTestBase.getOutputStream("remote.pdf"));
    // step 3:
    document.open();
    remote.open();
    // step 4: we add some content
    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"));
    document.add(new Chunk("goto page 1").setAction(PdfAction.gotoLocalPage(1, new PdfDestination(PdfDestination.FITH, 500), writer)));
    document.add(Chunk.NEWLINE);
    document.add(new Chunk("goto another document").setAction(PdfAction.gotoRemotePage("remote.pdf", "test", false, true)));
    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);
    // step 5: we close the document
    document.close();
    remote.close();
}
Also used : PdfAction(com.lowagie.text.pdf.PdfAction) PdfWriter(com.lowagie.text.pdf.PdfWriter) PdfDestination(com.lowagie.text.pdf.PdfDestination) Document(com.lowagie.text.Document) Chunk(com.lowagie.text.Chunk) Paragraph(com.lowagie.text.Paragraph) Test(org.junit.Test)

Example 3 with PdfAction

use of com.lowagie.text.pdf.PdfAction in project itext2 by albfernandez.

the class ChainedActionsTest method main.

/**
 * Creates a document with chained Actions.
 */
@Test
public void main() throws Exception {
    // step 1: creation of a document-object
    Document document = new Document();
    // step 2:
    PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("ChainedActions.pdf"));
    // step 3: we add Javascript as Metadata and we open the document
    document.open();
    // step 4: we add some content
    PdfAction action = PdfAction.javaScript("app.alert('Welcome at my site');\r", writer);
    action.next(new PdfAction("http://www.lowagie.com/iText/"));
    Paragraph p = new Paragraph(new Chunk("Click to go to Bruno's site").setAction(action));
    document.add(p);
    // step 5: we close the document
    document.close();
}
Also used : PdfAction(com.lowagie.text.pdf.PdfAction) PdfWriter(com.lowagie.text.pdf.PdfWriter) Document(com.lowagie.text.Document) Chunk(com.lowagie.text.Chunk) Paragraph(com.lowagie.text.Paragraph) Test(org.junit.Test)

Example 4 with PdfAction

use of com.lowagie.text.pdf.PdfAction in project itext2 by albfernandez.

the class NamedActionsTest method main.

/**
 * Creates a document with Named Actions.
 */
@Test
public void main() throws Exception {
    // step 1: creation of a document-object
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    // step 2: we create a writer that listens to the document
    PdfWriter.getInstance(document, PdfTestBase.getOutputStream("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();
    }
    // step 5: we close the document
    document.close();
}
Also used : PdfAction(com.lowagie.text.pdf.PdfAction) PdfPTable(com.lowagie.text.pdf.PdfPTable) Phrase(com.lowagie.text.Phrase) Document(com.lowagie.text.Document) Chunk(com.lowagie.text.Chunk) Paragraph(com.lowagie.text.Paragraph) Test(org.junit.Test)

Example 5 with PdfAction

use of com.lowagie.text.pdf.PdfAction in project itext2 by albfernandez.

the class TableEvents1Test method tableLayout.

/**
 * @see com.lowagie.text.pdf.PdfPTableEvent#tableLayout(com.lowagie.text.pdf.PdfPTable,
 *      float[][], float[], int, int, com.lowagie.text.pdf.PdfContentByte[])
 */
public void tableLayout(PdfPTable table, float[][] width, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) {
    // widths of the different cells of the first row
    float[] widths = width[0];
    PdfContentByte cb = canvases[PdfPTable.TEXTCANVAS];
    cb.saveState();
    // border for the complete table
    cb.setLineWidth(2);
    cb.setRGBColorStroke(255, 0, 0);
    cb.rectangle(widths[0], heights[heights.length - 1], widths[widths.length - 1] - widths[0], heights[0] - heights[heights.length - 1]);
    cb.stroke();
    // border for the header rows
    if (headerRows > 0) {
        cb.setRGBColorStroke(0, 0, 255);
        cb.rectangle(widths[0], heights[headerRows], widths[widths.length - 1] - widths[0], heights[0] - heights[headerRows]);
        cb.stroke();
    }
    cb.restoreState();
    cb = canvases[PdfPTable.BASECANVAS];
    cb.saveState();
    // border for the cells
    cb.setLineWidth(.5f);
    // loop over the rows
    for (int line = 0; line < heights.length - 1; ++line) {
        // loop over the columns
        for (int col = 0; col < widths.length - 1; ++col) {
            if (line == 0 && col == 0)
                cb.setAction(new PdfAction("http://www.lowagie.com/iText/"), widths[col], heights[line + 1], widths[col + 1], heights[line]);
            cb.setRGBColorStrokeF((float) Math.random(), (float) Math.random(), (float) Math.random());
            // horizontal borderline
            cb.moveTo(widths[col], heights[line]);
            cb.lineTo(widths[col + 1], heights[line]);
            cb.stroke();
            // vertical borderline
            cb.setRGBColorStrokeF((float) Math.random(), (float) Math.random(), (float) Math.random());
            cb.moveTo(widths[col], heights[line]);
            cb.lineTo(widths[col], heights[line + 1]);
            cb.stroke();
        }
    }
    cb.restoreState();
}
Also used : PdfAction(com.lowagie.text.pdf.PdfAction) PdfContentByte(com.lowagie.text.pdf.PdfContentByte)

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