Search in sources :

Example 16 with PdfAction

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

the class TableEvents2Test 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) {
        widths = width[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)

Example 17 with PdfAction

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

the class PdfAnnotationsImp method convertAnnotation.

public static PdfAnnotation convertAnnotation(PdfWriter writer, Annotation annot, Rectangle defaultRect) throws IOException {
    switch(annot.annotationType()) {
        case Annotation.URL_NET:
            return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((URL) annot.attributes().get(Annotation.URL)));
        case Annotation.URL_AS_STRING:
            return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.attributes().get(Annotation.FILE)));
        case Annotation.FILE_DEST:
            return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.attributes().get(Annotation.FILE), (String) annot.attributes().get(Annotation.DESTINATION)));
        case Annotation.SCREEN:
            boolean[] sparams = (boolean[]) annot.attributes().get(Annotation.PARAMETERS);
            String fname = (String) annot.attributes().get(Annotation.FILE);
            String mimetype = (String) annot.attributes().get(Annotation.MIMETYPE);
            PdfFileSpecification fs;
            if (sparams[0])
                fs = PdfFileSpecification.fileEmbedded(writer, fname, fname, null);
            else
                fs = PdfFileSpecification.fileExtern(writer, fname);
            PdfAnnotation ann = PdfAnnotation.createScreen(writer, new Rectangle(annot.llx(), annot.lly(), annot.urx(), annot.ury()), fname, fs, mimetype, sparams[1]);
            return ann;
        case Annotation.FILE_PAGE:
            return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.attributes().get(Annotation.FILE), ((Integer) annot.attributes().get(Annotation.PAGE)).intValue()));
        case Annotation.NAMED_DEST:
            return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction(((Integer) annot.attributes().get(Annotation.NAMED)).intValue()));
        case Annotation.LAUNCH:
            return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.attributes().get(Annotation.APPLICATION), (String) annot.attributes().get(Annotation.PARAMETERS), (String) annot.attributes().get(Annotation.OPERATION), (String) annot.attributes().get(Annotation.DEFAULTDIR)));
        default:
            return new PdfAnnotation(writer, defaultRect.getLeft(), defaultRect.getBottom(), defaultRect.getRight(), defaultRect.getTop(), new PdfString(annot.title(), PdfObject.TEXT_UNICODE), new PdfString(annot.content(), PdfObject.TEXT_UNICODE));
    }
}
Also used : PdfAction(com.lowagie.text.pdf.PdfAction) PdfAnnotation(com.lowagie.text.pdf.PdfAnnotation) PdfFileSpecification(com.lowagie.text.pdf.PdfFileSpecification) Rectangle(com.lowagie.text.Rectangle) PdfRectangle(com.lowagie.text.pdf.PdfRectangle) PdfString(com.lowagie.text.pdf.PdfString) PdfString(com.lowagie.text.pdf.PdfString) URL(java.net.URL)

Example 18 with PdfAction

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

the class OptionalContentTest method main.

/**
 * Demonstrates the use of layers.
 *
 * @param args
 *            no arguments needed
 */
@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: creation of the writer
    PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("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(PdfTestBase.RESOURCES_DIR + "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);
    ArrayList<Object> state = new ArrayList<Object>();
    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();
}
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) TextField(com.lowagie.text.pdf.TextField) PdfContentByte(com.lowagie.text.pdf.PdfContentByte) PdfLayerMembership(com.lowagie.text.pdf.PdfLayerMembership) Test(org.junit.Test)

Example 19 with PdfAction

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

the class OpenApplicationTest method main.

/**
 * Creates a document with Named Actions.
 *
 * @param args The file to open
 */
public void main(String... args) 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("OpenApplication.pdf"));
    // step 3: we open the document
    document.open();
    // step 4: we add some content
    String application = args[0];
    Paragraph p = new Paragraph(new Chunk("Click to open " + application).setAction(new PdfAction(application, null, null, null)));
    document.add(p);
    // step 5: we close the document
    document.close();
}
Also used : PdfAction(com.lowagie.text.pdf.PdfAction) Document(com.lowagie.text.Document) Chunk(com.lowagie.text.Chunk) Paragraph(com.lowagie.text.Paragraph)

Example 20 with PdfAction

use of com.lowagie.text.pdf.PdfAction in project qcadoo by qcadoo.

the class ReportPdfView method buildTestedPdfDocumentToFile.

public void buildTestedPdfDocumentToFile(final Map<String, Object> model, final String filePath) {
    try {
        Document document = newDocument();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filePath));
        writer.setViewerPreferences(getViewerPreferences());
        setPageEvent(writer);
        document.open();
        PdfAction ac = PdfAction.gotoLocalPage(1, new PdfDestination(PdfDestination.XYZ, -1, -1, 1f), writer);
        writer.setOpenAction(ac);
        addContent(document, model, LocaleContextHolder.getLocale(), writer);
        document.close();
    } catch (DocumentException | IOException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}
Also used : PdfAction(com.lowagie.text.pdf.PdfAction) PdfWriter(com.lowagie.text.pdf.PdfWriter) FileOutputStream(java.io.FileOutputStream) DocumentException(com.lowagie.text.DocumentException) PdfDestination(com.lowagie.text.pdf.PdfDestination) IOException(java.io.IOException) Document(com.lowagie.text.Document)

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