Example usage for com.lowagie.text.pdf PdfFileSpecification fileEmbedded

List of usage examples for com.lowagie.text.pdf PdfFileSpecification fileEmbedded

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfFileSpecification fileEmbedded.

Prototype

public static PdfFileSpecification fileEmbedded(PdfWriter writer, String filePath, String fileDisplay,
        byte fileStore[]) throws IOException 

Source Link

Document

Creates a file specification with the file embedded.

Usage

From source file:dev.ztgnrw.htmlconverter.HtmlConverter.java

/**
 * Method for attachment. Adds a File to a PDF as attachment
 * //from  w  w  w.  j  a  v a2  s.c om
 * @param reader
 * @param output
 * @param attachment_uri
 * @throws IOException
 * @throws DocumentException 
 */
private static void addFile(PdfReader reader, String output, String attachment_uri)
        throws IOException, DocumentException {

    File attachment = new File(attachment_uri);

    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(output + ".pdf"));
    // PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(
    //        stamper.getWriter(), null, "test.txt", "Some test".getBytes());

    PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(stamper.getWriter(), null, attachment.getName(),
            Files.readAllBytes(attachment.toPath()));

    stamper.addFileAttachment("Attachment File", fs);
    stamper.close();
}

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

License:Open Source License

@Override
public void exportElement(JRPdfExporterContext exporterContext, JRGenericPrintElement element) {
    try {/*from   w ww  . j a  v  a  2s.c o  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  www.  j a v a2s. 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 {/* w w w. j  a  va2s .  c  o 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:org.eclipse.birt.report.engine.emitter.pdf.PDFPage.java

License:Open Source License

protected void embedFlash(String flashPath, byte[] flashData, float x, float y, float height, float width,
        String helpText, Map params) throws IOException {
    y = transformY(y, height);//  w  w  w.  j  a  v a2s. co m
    contentByte.saveState();
    PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(writer, flashPath, helpText, flashData);
    PdfAnnotation annot = PdfAnnotation.createScreen(writer, new Rectangle(x, y, x + width, y + height),
            helpText, fs, "application/x-shockwave-flash", true);
    writer.addAnnotation(annot);
    if (helpText != null) {
        showHelpText(x, y, width, height, helpText);
    }
    contentByte.restoreState();
}

From source file:questions.stamppages.RemoveAttachmentAnnotations.java

public static void createPdfWithAttachments() throws IOException, DocumentException {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESOURCE));
    document.open();//from www  .  j av a2  s .c  o  m
    document.add(new Paragraph("page 1"));
    writer.addAnnotation(PdfAnnotation.createFileAttachment(writer, new Rectangle(100f, 780f, 120f, 800f),
            "This is some text", "some text".getBytes(), null, "some.txt"));
    writer.addAnnotation(PdfAnnotation.createText(writer, new Rectangle(100f, 750f, 120f, 770f), "Help",
            "This Help annotation was made with 'createText'", false, "Help"));
    document.newPage();
    document.add(new Paragraph("page 2"));
    document.newPage();
    document.add(new Paragraph("page 3"));
    PdfFileSpecification fs1 = PdfFileSpecification.fileEmbedded(writer, TXT, "caesar.txt", null);
    writer.addAnnotation(
            PdfAnnotation.createFileAttachment(writer, new Rectangle(100f, 780f, 120f, 800f), "Caesar", fs1));
    PdfFileSpecification fs2 = PdfFileSpecification.fileEmbedded(writer, IMG, "1t3xt.gif", null);
    writer.addAnnotation(
            PdfAnnotation.createFileAttachment(writer, new Rectangle(100f, 750f, 120f, 770f), "1t3xt", fs2));
    document.close();
}