Example usage for com.lowagie.text.pdf PdfWriter addAnnotation

List of usage examples for com.lowagie.text.pdf PdfWriter addAnnotation

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfWriter addAnnotation.

Prototype

public void addAnnotation(PdfAnnotation annot) 

Source Link

Usage

From source file:com.geek.tutorial.itext.acroform.RadioCheckBoxForm.java

License:Open Source License

private static void addCheckbox(PdfWriter writer, Rectangle rect, String name)
        throws IOException, DocumentException {
    RadioCheckField check = new RadioCheckField(writer, rect, name, "On");
    check.setCheckType(RadioCheckField.TYPE_CROSS);
    check.setBorderColor(Color.BLACK);
    writer.addAnnotation(check.getCheckField());
}

From source file:com.kahlon.guard.controller.example.SimpleBean.java

public void createPDF() {
    try { //catch better your exceptions, this is just an example
        FacesContext context = FacesContext.getCurrentInstance();
        Document document = new Document();
        String fileName = "PDFFile";
        String TEXT = FacesMessageUtil.getMessage("person.name") + "  --  "
                + "These are the protagonists in 'Hero', a movie by Zhang Yimou:\n"
                + "\u7121\u540d (Nameless), \u6b98\u528d (Broken Sword), "
                + "\u98db\u96ea (Flying Snow), \u5982\u6708 (Moon), "
                + "\u79e6\u738b (the King), and \u9577\u7a7a (Sky).";

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter writer = PdfWriter.getInstance(document, baos);

        if (!document.isOpen()) {
            document.open();//ww w. j  av a  2  s .  c om
        }

        writer.getAcroForm().setNeedAppearances(true);

        TextField text = new TextField(writer, new Rectangle(36, 806, 559, 780), "description");
        text.setOptions(TextField.MULTILINE);
        //text.setOptions(TextField.READ_ONLY);
        text.setText(TEXT);

        writer.addAnnotation(text.getTextField());

        //document.add(new Phrase(TEXT));

        //Keep modifying your pdf file (add pages and more)
        document.close();

        writePDFToResponse(context.getExternalContext(), baos, fileName);

        context.responseComplete();

    } catch (Exception e) {
        //e.printStackTrace();          
    }
}

From source file:net.sf.dynamicreports.examples.genericelement.openflashchart.OpenFlashChartPdfHandler.java

License:Open Source License

@Override
public void exportElement(JRPdfExporterContext exporterContext, JRGenericPrintElement element) {
    try {//w ww  . j  a va  2  s .  co  m
        PdfWriter writer = exporterContext.getPdfWriter();
        PdfIndirectObject swfRef;
        boolean newContext = !existingContexts.containsKey(exporterContext);
        if (newContext) {
            PdfDictionary extensions = new PdfDictionary();
            PdfDictionary adobeExtension = new PdfDictionary();
            adobeExtension.put(new PdfName("BaseVersion"), PdfWriter.PDF_VERSION_1_7);
            adobeExtension.put(new PdfName("ExtensionLevel"), new PdfNumber(3));
            extensions.put(new PdfName("ADBE"), adobeExtension);
            writer.getExtraCatalog().put(new PdfName("Extensions"), extensions);

            byte[] swfData = getChartSwf();
            PdfFileSpecification swfFile = PdfFileSpecification.fileEmbedded(writer, null, "Open Flash Chart",
                    swfData);
            swfRef = writer.addToBody(swfFile);
            existingContexts.put(exporterContext, swfRef);
        } else {
            swfRef = (PdfIndirectObject) existingContexts.get(exporterContext);
        }

        Rectangle rect = new Rectangle(element.getX() + exporterContext.getOffsetX(),
                exporterContext.getExportedReport().getPageHeight() - element.getY()
                        - exporterContext.getOffsetY(),
                element.getX() + exporterContext.getOffsetX() + element.getWidth(),
                exporterContext.getExportedReport().getPageHeight() - element.getY()
                        - exporterContext.getOffsetY() - element.getHeight());
        PdfAnnotation ann = new PdfAnnotation(writer, rect);
        ann.put(PdfName.SUBTYPE, new PdfName("RichMedia"));

        PdfDictionary settings = new PdfDictionary();
        PdfDictionary activation = new PdfDictionary();
        activation.put(new PdfName("Condition"), new PdfName("PV"));
        settings.put(new PdfName("Activation"), activation);
        ann.put(new PdfName("RichMediaSettings"), settings);

        PdfDictionary content = new PdfDictionary();

        HashMap<String, PdfIndirectReference> assets = new HashMap<String, PdfIndirectReference>();
        assets.put("map.swf", swfRef.getIndirectReference());
        PdfDictionary assetsDictionary = PdfNameTree.writeTree(assets, writer);
        content.put(new PdfName("Assets"), assetsDictionary);

        PdfArray configurations = new PdfArray();
        PdfDictionary configuration = new PdfDictionary();

        PdfArray instances = new PdfArray();
        PdfDictionary instance = new PdfDictionary();
        instance.put(new PdfName("Subtype"), new PdfName("Flash"));
        PdfDictionary params = new PdfDictionary();

        String chartData = ((ChartGenerator) element
                .getParameterValue(ChartGenerator.PARAMETER_CHART_GENERATOR)).generateChart();
        String vars = "inline_data=" + chartData;
        params.put(new PdfName("FlashVars"), new PdfString(vars));
        instance.put(new PdfName("Params"), params);
        instance.put(new PdfName("Asset"), swfRef.getIndirectReference());
        PdfIndirectObject instanceRef = writer.addToBody(instance);
        instances.add(instanceRef.getIndirectReference());
        configuration.put(new PdfName("Instances"), instances);

        PdfIndirectObject configurationRef = writer.addToBody(configuration);
        configurations.add(configurationRef.getIndirectReference());
        content.put(new PdfName("Configurations"), configurations);

        ann.put(new PdfName("RichMediaContent"), content);

        writer.addAnnotation(ann);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:net.sf.fspdfs.components.ofc.ChartPdfHandler.java

License:Open Source License

public void exportElement(JRPdfExporterContext exporterContext, JRGenericPrintElement element) {
    try {//from  w w  w  . j  a  v a 2  s .  c  om
        PdfWriter writer = exporterContext.getPdfWriter();
        PdfIndirectObject swfRef;
        boolean newContext = !existingContexts.containsKey(exporterContext);
        if (newContext) {
            // add the Adobe 1.7 extensions catalog dictionary
            PdfDictionary extensions = new PdfDictionary();
            PdfDictionary adobeExtension = new PdfDictionary();
            adobeExtension.put(new PdfName("BaseVersion"), PdfWriter.PDF_VERSION_1_7);
            adobeExtension.put(new PdfName("ExtensionLevel"), new PdfNumber(3));
            extensions.put(new PdfName("ADBE"), adobeExtension);
            writer.getExtraCatalog().put(new PdfName("Extensions"), extensions);

            // add the swf file
            byte[] swfData = readSwf();
            PdfFileSpecification swfFile = PdfFileSpecification.fileEmbedded(writer, null, "Open Flash Chart",
                    swfData);
            swfRef = writer.addToBody(swfFile);
            existingContexts.put(exporterContext, swfRef);
        } else {
            swfRef = (PdfIndirectObject) existingContexts.get(exporterContext);
        }

        Rectangle rect = new Rectangle(element.getX() + exporterContext.getOffsetX(),
                exporterContext.getExportedReport().getPageHeight() - element.getY()
                        - exporterContext.getOffsetY(),
                element.getX() + exporterContext.getOffsetX() + element.getWidth(),
                exporterContext.getExportedReport().getPageHeight() - element.getY()
                        - exporterContext.getOffsetY() - element.getHeight());
        PdfAnnotation ann = new PdfAnnotation(writer, rect);
        ann.put(PdfName.SUBTYPE, new PdfName("RichMedia"));

        PdfDictionary settings = new PdfDictionary();
        PdfDictionary activation = new PdfDictionary();
        activation.put(new PdfName("Condition"), new PdfName("PV"));
        settings.put(new PdfName("Activation"), activation);
        ann.put(new PdfName("RichMediaSettings"), settings);

        PdfDictionary content = new PdfDictionary();

        HashMap assets = new HashMap();
        assets.put("map.swf", swfRef.getIndirectReference());
        PdfDictionary assetsDictionary = PdfNameTree.writeTree(assets, writer);
        content.put(new PdfName("Assets"), assetsDictionary);

        PdfArray configurations = new PdfArray();
        PdfDictionary configuration = new PdfDictionary();

        PdfArray instances = new PdfArray();
        PdfDictionary instance = new PdfDictionary();
        instance.put(new PdfName("Subtype"), new PdfName("Flash"));
        PdfDictionary params = new PdfDictionary();

        String chartData = (String) element.getParameterValue(PARAMETER_CHART_DATA);
        String vars = "inline_data=" + chartData;
        params.put(new PdfName("FlashVars"), new PdfString(vars));
        instance.put(new PdfName("Params"), params);
        instance.put(new PdfName("Asset"), swfRef.getIndirectReference());
        PdfIndirectObject instanceRef = writer.addToBody(instance);
        instances.add(instanceRef.getIndirectReference());
        configuration.put(new PdfName("Instances"), instances);

        PdfIndirectObject configurationRef = writer.addToBody(configuration);
        configurations.add(configurationRef.getIndirectReference());
        content.put(new PdfName("Configurations"), configurations);

        ann.put(new PdfName("RichMediaContent"), content);

        writer.addAnnotation(ann);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:net.sf.jasperreports.components.ofc.ChartPdfHandler.java

License:Open Source License

public void exportElement(JRPdfExporterContext exporterContext, JRGenericPrintElement element) {
    try {/*from   w  w  w . ja v  a 2s  .  co  m*/
        PdfWriter writer = exporterContext.getPdfWriter();
        PdfIndirectObject swfRef;
        boolean newContext = !existingContexts.containsKey(exporterContext);
        if (newContext) {
            // add the Adobe 1.7 extensions catalog dictionary
            PdfDictionary extensions = new PdfDictionary();
            PdfDictionary adobeExtension = new PdfDictionary();
            adobeExtension.put(new PdfName("BaseVersion"), PdfWriter.PDF_VERSION_1_7);
            adobeExtension.put(new PdfName("ExtensionLevel"), new PdfNumber(3));
            extensions.put(new PdfName("ADBE"), adobeExtension);
            writer.getExtraCatalog().put(new PdfName("Extensions"), extensions);

            // add the swf file
            byte[] swfData = readSwf();
            PdfFileSpecification swfFile = PdfFileSpecification.fileEmbedded(writer, null, "Open Flash Chart",
                    swfData);
            swfRef = writer.addToBody(swfFile);
            existingContexts.put(exporterContext, swfRef);
        } else {
            swfRef = (PdfIndirectObject) existingContexts.get(exporterContext);
        }

        Rectangle rect = new Rectangle(element.getX() + exporterContext.getOffsetX(),
                exporterContext.getExportedReport().getPageHeight() - element.getY()
                        - exporterContext.getOffsetY(),
                element.getX() + exporterContext.getOffsetX() + element.getWidth(),
                exporterContext.getExportedReport().getPageHeight() - element.getY()
                        - exporterContext.getOffsetY() - element.getHeight());
        PdfAnnotation ann = new PdfAnnotation(writer, rect);
        ann.put(PdfName.SUBTYPE, new PdfName("RichMedia"));

        PdfDictionary settings = new PdfDictionary();
        PdfDictionary activation = new PdfDictionary();
        activation.put(new PdfName("Condition"), new PdfName("PV"));
        settings.put(new PdfName("Activation"), activation);
        ann.put(new PdfName("RichMediaSettings"), settings);

        PdfDictionary content = new PdfDictionary();

        HashMap<String, PdfIndirectReference> assets = new HashMap<String, PdfIndirectReference>();
        assets.put("map.swf", swfRef.getIndirectReference());
        PdfDictionary assetsDictionary = PdfNameTree.writeTree(assets, writer);
        content.put(new PdfName("Assets"), assetsDictionary);

        PdfArray configurations = new PdfArray();
        PdfDictionary configuration = new PdfDictionary();

        PdfArray instances = new PdfArray();
        PdfDictionary instance = new PdfDictionary();
        instance.put(new PdfName("Subtype"), new PdfName("Flash"));
        PdfDictionary params = new PdfDictionary();

        String chartData = (String) element.getParameterValue(PARAMETER_CHART_DATA);
        String vars = "inline_data=" + chartData;
        params.put(new PdfName("FlashVars"), new PdfString(vars));
        instance.put(new PdfName("Params"), params);
        instance.put(new PdfName("Asset"), swfRef.getIndirectReference());
        PdfIndirectObject instanceRef = writer.addToBody(instance);
        instances.add(instanceRef.getIndirectReference());
        configuration.put(new PdfName("Instances"), instances);

        PdfIndirectObject configurationRef = writer.addToBody(configuration);
        configurations.add(configurationRef.getIndirectReference());
        content.put(new PdfName("Configurations"), configurations);

        ann.put(new PdfName("RichMediaContent"), content);

        writer.addAnnotation(ann);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:net.sf.jasperreports.forms.textinput.TextInputElementPdfHandler.java

License:Open Source License

public void exportElement(JRPdfExporterContext exporterContext, JRGenericPrintElement element) {
    PdfWriter writer = exporterContext.getPdfWriter();
    JasperPrint jasperPrint = exporterContext.getExportedReport();

    JRPrintText printText = (JRPrintText) element
            .getParameterValue(TextInputElement.PARAMETER_PRINT_TEXT_ELEMENT);
    if (printText == null) //FIXMEINPUT deal with xml serialization
    {/*from   ww w . ja v  a2 s  . c  o  m*/
        return;
    }

    Rectangle rectangle = new Rectangle(element.getX() + exporterContext.getOffsetX(),
            jasperPrint.getPageHeight() - element.getY() - exporterContext.getOffsetY(),
            element.getX() + exporterContext.getOffsetX() + element.getWidth(),
            jasperPrint.getPageHeight() - element.getY() - exporterContext.getOffsetY() - element.getHeight());
    TextField text = new TextField(writer, rectangle, getFieldName(element));
    text.setBackgroundColor(printText.getBackcolor());
    text.setTextColor(printText.getForecolor());
    text.setText(printText.getFullText());
    text.setDefaultText("default:" + printText.getFullText());
    //      text.setBackgroundColor(element.getBackcolor());
    //      text.setTextColor(element.getForecolor());
    //      text.setText(getText(element));
    //      text.setDefaultText(getDefaultText(element));

    //FIXME: dynamic settings below:

    //      text.setAlignment(Element.ALIGN_LEFT);
    //      text.setBorderColor(Color.BLACK);
    //      text.setBorderWidth(TextField.BORDER_WIDTH_THIN);
    //      text.setBorderStyle(PdfBorderDictionary.STYLE_INSET);

    //      text.setChoiceExports(null);
    //      text.setChoices(null);
    //      text.setChoiceSelection(0);
    //      text.setExtensionFont(null);
    //      text.setExtraMargin(0, 0);
    //      try{
    //         text.setFont(BaseFont.createFont(BaseFont.COURIER, BaseFont.CP1250, true));
    //      }catch(Exception e){
    //         throw new JRRuntimeException(e);
    //      }
    text.setFontSize(printText.getFontsize());
    if (Boolean.TRUE.equals(element.getParameterValue(TextInputElement.PARAMETER_MULTI_LINE))) {
        text.setOptions(TextField.MULTILINE);
    }
    //      text.setRotation(90);
    //      text.setVisibility(TextField.VISIBLE);

    try {
        PdfFormField field = text.getTextField();
        writer.addAnnotation(field);
    } catch (Exception e) {
        throw new JRRuntimeException(e);
    }

}

From source file:org.mapfish.print.PDFCustomBlocks.java

License:Open Source License

private void addErrors(PdfWriter writer) {
    if (errors.size() > 0) {
        StringBuilder errorTxt = new StringBuilder();
        for (int i = 0; i < errors.size(); i++) {
            Exception exception = errors.get(i);
            errorTxt.append(exception).append("\n");
        }/*from   ww w .j  a  v a 2 s  .  c  o m*/
        errors.clear();

        final Rectangle rect = new Rectangle(20f, 40f, 40f, 60f);

        final PdfAnnotation annotation = PdfAnnotation.createText(writer, rect, "Error", errorTxt.toString(),
                false, "Note");
        writer.addAnnotation(annotation);

        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Added an annotation for errors");
        }
    }
}

From source file:org.xhtmlrenderer.pdf.CheckboxFormField.java

License:Open Source License

public void paint(RenderingContext c, ITextOutputDevice outputDevice, BlockBox box) {
    PdfContentByte cb = outputDevice.getCurrentPage();

    PdfWriter writer = outputDevice.getWriter();
    Element elm = box.getElement();

    Rectangle targetArea = outputDevice.createLocalTargetArea(c, box);
    String onValue = getValue(elm);

    RadioCheckField field = new RadioCheckField(writer, targetArea, getFieldName(outputDevice, elm), onValue);

    field.setChecked(isChecked(elm));/*from   www.  ja v a2s. c o  m*/
    field.setCheckType(RadioCheckField.TYPE_CHECK);
    field.setBorderStyle(PdfBorderDictionary.STYLE_SOLID);
    //TODO Consider if we can get some more correct color
    field.setBorderColor(Color.black);

    field.setBorderWidth(BaseField.BORDER_WIDTH_THIN);

    try {
        PdfFormField formField = field.getCheckField();
        if (isReadOnly(elm)) {
            formField.setFieldFlags(PdfFormField.FF_READ_ONLY);
        }
        writer.addAnnotation(formField);
    } catch (IOException ioe) {
        System.out.println(ioe);
    } catch (DocumentException de) {
        System.out.println(de);
    }

}

From source file:org.xhtmlrenderer.pdf.RadioButtonFormField.java

License:Open Source License

public void paint(RenderingContext c, ITextOutputDevice outputDevice, BlockBox box) {
    String fieldName = getFieldName(outputDevice, box.getElement());
    List radioBoxes = _factory.getRadioButtons(fieldName);

    // iText wants all radio buttons in a group added at once across all pages

    if (radioBoxes == null) {
        // Already added to document
        return;//from   w ww .  jav  a 2s  .co m
    }

    PdfContentByte cb = outputDevice.getCurrentPage();
    PdfWriter writer = outputDevice.getWriter();

    PdfFormField group = PdfFormField.createRadioButton(writer, true);
    group.setFieldName(fieldName);

    RadioButtonFormField checked = getChecked(radioBoxes);
    if (checked != null) {
        group.setValueAsString(getValue(checked.getBox().getElement()));
    }

    for (Iterator i = radioBoxes.iterator(); i.hasNext();) {
        RadioButtonFormField fieldElem = (RadioButtonFormField) i.next();

        createField(c, outputDevice, cb, writer, group, fieldElem, checked);
    }

    writer.addAnnotation(group);

    _factory.remove(fieldName);
}

From source file:org.xhtmlrenderer.pdf.SelectFormField.java

License:Open Source License

public void paint(RenderingContext c, ITextOutputDevice outputDevice, BlockBox box) {
    PdfWriter writer = outputDevice.getWriter();

    String[][] options = getPDFOptions();
    int selectedIndex = getSelectedIndex();

    PdfFormField field;/*from  w w w.j a v a 2  s. c o  m*/

    /*
     * Comment out for now.  We need to draw an appropriate appearance for
     * this to work correctly.
     */
    /*
    if (isMultiple(box.getElement())) {
    field = PdfFormField.createList(writer, options, selectedIndex);  
    } else {
    field = PdfFormField.createCombo(writer, false, options, selectedIndex);    
    }
    */

    field = PdfFormField.createCombo(writer, false, options, selectedIndex);

    field.setWidget(outputDevice.createLocalTargetArea(c, box), PdfAnnotation.HIGHLIGHT_INVERT);
    field.setFieldName(getFieldName(outputDevice, box.getElement()));
    if (options.length > 0) {
        field.setValueAsString(options[selectedIndex][0]);
    }

    createAppearance(c, outputDevice, box, field);

    if (isReadOnly(box.getElement())) {
        field.setFieldFlags(PdfFormField.FF_READ_ONLY);
    }

    /*
    if (isMultiple(box.getElement())) {
    field.setFieldFlags(PdfFormField.FF_MULTISELECT);
    }
    */

    writer.addAnnotation(field);
}