Search in sources :

Example 1 with PdfFileSpecification

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

the class AnnotationsTest method main.

/**
 * Creates a document with some PdfAnnotations.
 */
@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("Annotations.pdf"));
    // step 3:
    writer.setPdfVersion(PdfWriter.VERSION_1_5);
    document.open();
    // step 4:
    PdfContentByte cb = writer.getDirectContent();
    // page 1
    PdfFileSpecification fs = PdfFileSpecification.fileExtern(writer, PdfTestBase.RESOURCES_DIR + "cards.mpg");
    writer.addAnnotation(PdfAnnotation.createScreen(writer, new Rectangle(200f, 700f, 300f, 800f), "cards.mpg", fs, "video/mpeg", true));
    PdfAnnotation a = new PdfAnnotation(writer, 200f, 550f, 300f, 650f, PdfAction.javaScript("app.alert('Hello');\r", writer));
    document.add(new Chunk("click to trigger javascript").setAnnotation(a).setLocalDestination("top"));
    writer.addAnnotation(a);
    writer.addAnnotation(PdfAnnotation.createFileAttachment(writer, new Rectangle(100f, 650f, 150f, 700f), "This is some text", "some text".getBytes(), null, "some.txt"));
    writer.addAnnotation(PdfAnnotation.createText(writer, new Rectangle(200f, 400f, 300f, 500f), "Help", "This Help annotation was made with 'createText'", false, "Help"));
    writer.addAnnotation(PdfAnnotation.createText(writer, new Rectangle(200f, 250f, 300f, 350f), "Help", "This Comment annotation was made with 'createText'", true, "Comment"));
    cb.rectangle(200, 700, 100, 100);
    cb.rectangle(200, 550, 100, 100);
    cb.rectangle(200, 400, 100, 100);
    cb.rectangle(200, 250, 100, 100);
    cb.stroke();
    document.newPage();
    // page 2
    writer.addAnnotation(PdfAnnotation.createLink(writer, new Rectangle(200f, 700f, 300f, 800f), PdfAnnotation.HIGHLIGHT_TOGGLE, PdfAction.javaScript("app.alert('Hello');\r", writer)));
    writer.addAnnotation(PdfAnnotation.createLink(writer, new Rectangle(200f, 550f, 300f, 650f), PdfAnnotation.HIGHLIGHT_OUTLINE, "top"));
    writer.addAnnotation(PdfAnnotation.createLink(writer, new Rectangle(200f, 400f, 300f, 500f), PdfAnnotation.HIGHLIGHT_PUSH, 1, new PdfDestination(PdfDestination.FIT)));
    writer.addAnnotation(PdfAnnotation.createSquareCircle(writer, new Rectangle(200f, 250f, 300f, 350f), "This Comment annotation was made with 'createSquareCircle'", false));
    document.newPage();
    // page 3
    PdfContentByte pcb = new PdfContentByte(writer);
    pcb.setColorFill(new Color(0xFF, 0x00, 0x00));
    writer.addAnnotation(PdfAnnotation.createFreeText(writer, new Rectangle(200f, 700f, 300f, 800f), "This is some free text, blah blah blah", pcb));
    writer.addAnnotation(PdfAnnotation.createLine(writer, new Rectangle(200f, 550f, 300f, 650f), "this is a line", 200, 550, 300, 650));
    writer.addAnnotation(PdfAnnotation.createStamp(writer, new Rectangle(200f, 400f, 300f, 500f), "This is a stamp", "Stamp"));
    writer.addAnnotation(PdfAnnotation.createPopup(writer, new Rectangle(200f, 250f, 300f, 350f), "Hello, I'm a popup!", true));
    cb.rectangle(200, 700, 100, 100);
    cb.rectangle(200, 550, 100, 100);
    cb.rectangle(200, 250, 100, 100);
    cb.stroke();
    // step 5: we close the document
    document.close();
}
Also used : PdfWriter(com.lowagie.text.pdf.PdfWriter) PdfAnnotation(com.lowagie.text.pdf.PdfAnnotation) PdfFileSpecification(com.lowagie.text.pdf.PdfFileSpecification) Color(java.awt.Color) Rectangle(com.lowagie.text.Rectangle) PdfDestination(com.lowagie.text.pdf.PdfDestination) PdfContentByte(com.lowagie.text.pdf.PdfContentByte) Document(com.lowagie.text.Document) Chunk(com.lowagie.text.Chunk) Test(org.junit.Test)

Example 2 with PdfFileSpecification

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

the class Annotations method main.

/**
 * Creates a document with some PdfAnnotations.
 *
 * @param args
 *            no arguments needed
 */
public static void main(String[] args) {
    System.out.println("PdfAnnotations");
    // step 1: creation of a document-object
    Document document = new Document();
    try {
        // step 2:
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("Annotations.pdf"));
        // step 3:
        writer.setPdfVersion(PdfWriter.VERSION_1_5);
        document.open();
        // step 4:
        PdfContentByte cb = writer.getDirectContent();
        // page 1
        PdfFileSpecification fs = PdfFileSpecification.fileExtern(writer, "cards.mpg");
        writer.addAnnotation(PdfAnnotation.createScreen(writer, new Rectangle(200f, 700f, 300f, 800f), "cards.mpg", fs, "video/mpeg", true));
        PdfAnnotation a = new PdfAnnotation(writer, 200f, 550f, 300f, 650f, PdfAction.javaScript("app.alert('Hello');\r", writer));
        document.add(new Chunk("click to trigger javascript").setAnnotation(a).setLocalDestination("top"));
        writer.addAnnotation(a);
        writer.addAnnotation(PdfAnnotation.createFileAttachment(writer, new Rectangle(100f, 650f, 150f, 700f), "This is some text", "some text".getBytes(), null, "some.txt"));
        writer.addAnnotation(PdfAnnotation.createText(writer, new Rectangle(200f, 400f, 300f, 500f), "Help", "This Help annotation was made with 'createText'", false, "Help"));
        writer.addAnnotation(PdfAnnotation.createText(writer, new Rectangle(200f, 250f, 300f, 350f), "Help", "This Comment annotation was made with 'createText'", true, "Comment"));
        cb.rectangle(200, 700, 100, 100);
        cb.rectangle(200, 550, 100, 100);
        cb.rectangle(200, 400, 100, 100);
        cb.rectangle(200, 250, 100, 100);
        cb.stroke();
        document.newPage();
        // page 2
        writer.addAnnotation(PdfAnnotation.createLink(writer, new Rectangle(200f, 700f, 300f, 800f), PdfAnnotation.HIGHLIGHT_TOGGLE, PdfAction.javaScript("app.alert('Hello');\r", writer)));
        writer.addAnnotation(PdfAnnotation.createLink(writer, new Rectangle(200f, 550f, 300f, 650f), PdfAnnotation.HIGHLIGHT_OUTLINE, "top"));
        writer.addAnnotation(PdfAnnotation.createLink(writer, new Rectangle(200f, 400f, 300f, 500f), PdfAnnotation.HIGHLIGHT_PUSH, 1, new PdfDestination(PdfDestination.FIT)));
        writer.addAnnotation(PdfAnnotation.createSquareCircle(writer, new Rectangle(200f, 250f, 300f, 350f), "This Comment annotation was made with 'createSquareCircle'", false));
        document.newPage();
        // page 3
        PdfContentByte pcb = new PdfContentByte(writer);
        pcb.setColorFill(new Color(0xFF, 0x00, 0x00));
        writer.addAnnotation(PdfAnnotation.createFreeText(writer, new Rectangle(200f, 700f, 300f, 800f), "This is some free text, blah blah blah", pcb));
        writer.addAnnotation(PdfAnnotation.createLine(writer, new Rectangle(200f, 550f, 300f, 650f), "this is a line", 200, 550, 300, 650));
        writer.addAnnotation(PdfAnnotation.createStamp(writer, new Rectangle(200f, 400f, 300f, 500f), "This is a stamp", "Stamp"));
        writer.addAnnotation(PdfAnnotation.createPopup(writer, new Rectangle(200f, 250f, 300f, 350f), "Hello, I'm a popup!", true));
        cb.rectangle(200, 700, 100, 100);
        cb.rectangle(200, 550, 100, 100);
        cb.rectangle(200, 250, 100, 100);
        cb.stroke();
    } catch (Exception de) {
        de.printStackTrace();
    }
    // step 5: we close the document
    document.close();
}
Also used : PdfWriter(com.lowagie.text.pdf.PdfWriter) PdfAnnotation(com.lowagie.text.pdf.PdfAnnotation) FileOutputStream(java.io.FileOutputStream) PdfFileSpecification(com.lowagie.text.pdf.PdfFileSpecification) Color(java.awt.Color) Rectangle(com.lowagie.text.Rectangle) PdfDestination(com.lowagie.text.pdf.PdfDestination) PdfContentByte(com.lowagie.text.pdf.PdfContentByte) Document(com.lowagie.text.Document) Chunk(com.lowagie.text.Chunk)

Example 3 with PdfFileSpecification

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

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.getAttributes().get(Annotation.URL)));
        case Annotation.URL_AS_STRING:
            return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.getAttributes().get(Annotation.FILE)));
        case Annotation.FILE_DEST:
            return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.getAttributes().get(Annotation.FILE), (String) annot.getAttributes().get(Annotation.DESTINATION)));
        case Annotation.SCREEN:
            boolean[] sparams = (boolean[]) annot.getAttributes().get(Annotation.PARAMETERS);
            String fname = (String) annot.getAttributes().get(Annotation.FILE);
            String mimetype = (String) annot.getAttributes().get(Annotation.MIMETYPE);
            PdfFileSpecification fs;
            if (sparams[0]) {
                fs = PdfFileSpecification.fileEmbedded(writer, fname, fname, null);
            } else {
                fs = PdfFileSpecification.fileExtern(writer, fname);
            }
            return PdfAnnotation.createScreen(writer, new Rectangle(annot.llx(), annot.lly(), annot.urx(), annot.ury()), fname, fs, mimetype, sparams[1]);
        case Annotation.FILE_PAGE:
            return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.getAttributes().get(Annotation.FILE), (Integer) annot.getAttributes().get(Annotation.PAGE)));
        case Annotation.NAMED_DEST:
            return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((Integer) annot.getAttributes().get(Annotation.NAMED)));
        case Annotation.LAUNCH:
            return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.getAttributes().get(Annotation.APPLICATION), (String) annot.getAttributes().get(Annotation.PARAMETERS), (String) annot.getAttributes().get(Annotation.OPERATION), (String) annot.getAttributes().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 4 with PdfFileSpecification

use of com.lowagie.text.pdf.PdfFileSpecification in project gdmatrix by gdmatrix.

the class P7MTransformer method transformCMS.

private DataHandler transformCMS(Document document, String transformationName, Map options) throws Exception {
    String sigId = DocumentUtils.getPropertyValue(document, "sigId");
    DataHandler data = document.getContent().getData();
    P7MDocument cms = new P7MDocument(data.getInputStream());
    // add timestamps
    String tsUrl = properties.get(TSURL_PROPERTY);
    if (tsUrl != null && options != null && options.containsKey(TIMESTAMP_OPTION)) {
        cms.addTimeStamps(tsUrl);
    }
    if (P7M.equals(transformationName)) {
        byte[] bytes = cms.getEncoded();
        ByteArrayDataSource ds = new ByteArrayDataSource(bytes, "application/pkcs7-mime");
        return new DataHandler(ds);
    } else if (SIG.equals(transformationName)) {
        String signatureInfo = readSignatureInfo(document, options, cms, sigId, HTML_TEMPLATEURL_PROPERTY);
        byte[] bytes = signatureInfo.getBytes("UTF-8");
        return new DataHandler(new ByteArrayDataSource(bytes, "text/html"));
    } else if (PDF.equals(transformationName)) {
        // assuming content data is a MS Word document
        File file = File.createTempFile("transform", ".doc");
        System.out.println(">>> msword file: " + file);
        FileOutputStream os = new FileOutputStream(file);
        byte[] bytes = cms.getSignedContent();
        IOUtils.write(bytes, os);
        os.close();
        String signatureInfo = readSignatureInfo(document, options, cms, sigId, TEXT_TEMPLATEURL_PROPERTY);
        String pdfConverter = properties.get(PDFCONVERTER_PROPERTY);
        Template template = Template.create(pdfConverter);
        HashMap vars = new HashMap();
        vars.put("file", file.getAbsolutePath());
        vars.put("signatureInfo", signatureInfo);
        vars.putAll(options);
        Runtime runtime = Runtime.getRuntime();
        String command = template.merge(vars);
        System.out.println("Convert command: " + command);
        runtime.exec(command);
        String filename = file.getName();
        File pdfFile = new File(file.getParent(), filename.substring(0, filename.length() - 3) + "pdf");
        System.out.println(">>> pdf file: " + pdfFile);
        waitForFile(pdfFile);
        if (pdfFile.exists()) {
            System.out.println("Adding file and signature info...");
            File finalPdfFile = File.createTempFile("final", ".pdf");
            // read pdf file
            FileInputStream fis = new FileInputStream(pdfFile);
            PdfReader reader = new PdfReader(fis);
            fis.close();
            // create new pdf document
            FileOutputStream fos = new FileOutputStream(finalPdfFile);
            com.lowagie.text.Document pdfDoc = new com.lowagie.text.Document();
            PdfWriter writer = PdfWriter.getInstance(pdfDoc, fos);
            pdfDoc.open();
            pdfDoc.setPageSize(reader.getPageSize(1));
            PdfContentByte cb = writer.getDirectContent();
            for (int i = 1; i <= reader.getNumberOfPages(); i++) {
                pdfDoc.newPage();
                PdfImportedPage page = writer.getImportedPage(reader, i);
                cb.addTemplate(page, 0, 0);
            }
            // add signature info page
            if (options.containsKey(SIGN_INFO_PAGE_OPTION)) {
                String signatureInfo2 = readSignatureInfo(document, options, cms, sigId, HTML_TEMPLATEURL_PROPERTY);
                addSignatureInfo(pdfDoc, signatureInfo2);
            }
            // add original file
            if (options.containsKey(EMBED_P7M)) {
                String p7mFilename = cms.getSignatures().get(0).getFilename();
                PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(writer, null, p7mFilename + ".p7m", cms.getEncoded());
                writer.addFileAttachment(fs);
            }
            pdfDoc.close();
            fos.close();
            file.delete();
            pdfFile.delete();
            return new DataHandler(new TemporaryDataSource(finalPdfFile));
        } else
            throw new TransformationException("Internal error");
    } else // DOC
    {
        // Assuming p7m content is a MS Word document
        byte[] bytes = cms.getSignedContent();
        ByteArrayDataSource ds = new ByteArrayDataSource(bytes, "application/msword");
        return new DataHandler(ds);
    }
}
Also used : PdfWriter(com.lowagie.text.pdf.PdfWriter) HashMap(java.util.HashMap) P7MDocument(org.santfeliu.doc.fmt.p7m.P7MDocument) PdfString(com.lowagie.text.pdf.PdfString) DataHandler(javax.activation.DataHandler) PdfReader(com.lowagie.text.pdf.PdfReader) Document(org.matrix.doc.Document) P7MDocument(org.santfeliu.doc.fmt.p7m.P7MDocument) FileInputStream(java.io.FileInputStream) Template(org.santfeliu.util.template.Template) PdfImportedPage(com.lowagie.text.pdf.PdfImportedPage) TemporaryDataSource(org.santfeliu.util.TemporaryDataSource) FileOutputStream(java.io.FileOutputStream) PdfFileSpecification(com.lowagie.text.pdf.PdfFileSpecification) PdfContentByte(com.lowagie.text.pdf.PdfContentByte) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) File(java.io.File)

Example 5 with PdfFileSpecification

use of com.lowagie.text.pdf.PdfFileSpecification 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)

Aggregations

PdfFileSpecification (com.lowagie.text.pdf.PdfFileSpecification)5 Rectangle (com.lowagie.text.Rectangle)4 PdfAnnotation (com.lowagie.text.pdf.PdfAnnotation)4 PdfContentByte (com.lowagie.text.pdf.PdfContentByte)3 PdfString (com.lowagie.text.pdf.PdfString)3 PdfWriter (com.lowagie.text.pdf.PdfWriter)3 Chunk (com.lowagie.text.Chunk)2 Document (com.lowagie.text.Document)2 PdfAction (com.lowagie.text.pdf.PdfAction)2 PdfDestination (com.lowagie.text.pdf.PdfDestination)2 PdfRectangle (com.lowagie.text.pdf.PdfRectangle)2 Color (java.awt.Color)2 FileOutputStream (java.io.FileOutputStream)2 URL (java.net.URL)2 PdfImportedPage (com.lowagie.text.pdf.PdfImportedPage)1 PdfReader (com.lowagie.text.pdf.PdfReader)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 HashMap (java.util.HashMap)1 DataHandler (javax.activation.DataHandler)1