Example usage for com.lowagie.text.pdf PdfIndirectObject getIndirectReference

List of usage examples for com.lowagie.text.pdf PdfIndirectObject getIndirectReference

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfIndirectObject getIndirectReference.

Prototype


public PdfIndirectReference getIndirectReference() 

Source Link

Document

Returns a PdfIndirectReference to this PdfIndirectObject.

Usage

From source file:com.aripd.clms.service.ContractServiceBean.java

@Override
public void generatePdf(ContractEntity contract) {
    String baseFontUrl = "/fonts/Quivira.otf";
    FontFactory.register(baseFontUrl);//from   w  ww  . jav a2  s  . c om

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    try {
        BaseFont bf = BaseFont.createFont(baseFontUrl, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        Font font18n = new Font(bf, 18, Font.NORMAL);
        Font font12n = new Font(bf, 12, Font.NORMAL);
        Font font8n = new Font(bf, 8, Font.NORMAL);
        Font font8nbu = new Font(bf, 8, Font.BOLD | Font.UNDERLINE);
        Font font8ng = new Font(bf, 8, Font.NORMAL, Color.DARK_GRAY);
        Font font6n = new Font(bf, 6, Font.NORMAL);

        Document document = new Document(PageSize.A4);
        PdfWriter writer = PdfWriter.getInstance(document, output);
        document.open();
        addMetaData(document);
        addTitlePage(document, contract);
        Image imgBlue = Image.getInstance(1, 1, 3, 8, new byte[] { (byte) 0, (byte) 0, (byte) 255, });
        imgBlue.scaleAbsolute(document.getPageSize().getWidth(), 10);
        imgBlue.setAbsolutePosition(0, document.getPageSize().getHeight() - imgBlue.getScaledHeight());
        PdfImage stream = new PdfImage(imgBlue, "", null);
        stream.put(new PdfName("ITXT_SpecialId"), new PdfName("123456789"));
        PdfIndirectObject ref = writer.addToBody(stream);
        imgBlue.setDirectReference(ref.getIndirectReference());
        document.add(imgBlue);

        PdfPTable table = new PdfPTable(2);
        table.setWidthPercentage(100);

        PdfPCell cell = new PdfPCell(new Paragraph(contract.getName(), font18n));
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setColspan(2);
        cell.setPadding(5);
        table.addCell(cell);
        cell = new PdfPCell(new Paragraph("Version: " + contract.getVersion(), font8n));
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setColspan(2);
        cell.setPadding(5);
        table.addCell(cell);
        cell = new PdfPCell(new Paragraph("Review: " + contract.getReview(), font8n));
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setColspan(2);
        cell.setPadding(5);
        table.addCell(cell);
        cell = new PdfPCell(new Paragraph(contract.getRemark(), font12n));
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setColspan(2);
        cell.setPadding(5);
        table.addCell(cell);
        document.add(table);
        // Start a new page
        document.newPage();

        HTMLWorker htmlWorker = new HTMLWorker(document);
        htmlWorker.parse(new StringReader(contract.getRemark()));
        // Start a new page
        document.newPage();

        document.add(new Paragraph("Review Board", font18n));
        document.add(new LineSeparator(0.5f, 100, null, 0, -5));

        table = new PdfPTable(3);
        table.setWidthPercentage(100);

        cell = new PdfPCell(new Paragraph("Review Board", font18n));
        cell.setColspan(3);
        table.addCell(cell);
        cell = new PdfPCell(new Paragraph("Version", font12n));
        table.addCell(cell);
        cell = new PdfPCell(new Paragraph("Date", font12n));
        table.addCell(cell);
        cell = new PdfPCell(new Paragraph("Review", font12n));
        table.addCell(cell);
        for (HistoryContractEntity history : historyContractService.listing(contract)) {
            cell = new PdfPCell(new Paragraph(history.getVersion().toString(), font8n));
            table.addCell(cell);
            cell = new PdfPCell(new Paragraph(history.getStartdate().toString(), font8n));
            table.addCell(cell);
            cell = new PdfPCell(new Paragraph(history.getReview(), font8n));
            table.addCell(cell);
        }
        document.add(table);

        document.close();

        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
        response.reset();
        response.addHeader("Content-Type", "application/force-download");
        String filename = URLEncoder.encode(contract.getName() + ".pdf", "UTF-8");
        //            response.addHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
        response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + filename);
        response.getOutputStream().write(output.toByteArray());
        response.getOutputStream().flush();
        context.responseComplete();
        context.renderResponse();

    } catch (BadPdfFormatException | IOException ex) {
        Logger.getLogger(ContractServiceBean.class.getName()).log(Level.SEVERE, null, ex);
    } catch (DocumentException ex) {
        Logger.getLogger(ContractServiceBean.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:de.unigoettingen.sub.commons.contentlib.pdflib.PDFManager.java

License:Apache License

/**
 * Creates the pdf writer./*  w w w  . j av a  2 s.com*/
 * 
 * @param out the out
 * @param writer the writer
 * @param pdfdoc the pdfdoc
 * 
 * @return the pdf writer
 * 
 * @throws PDFManagerException the PDF manager exception
 */
private PdfWriter createPDFWriter(OutputStream out, Document pdfdoc) throws PDFManagerException {
    PdfWriter writer = null;
    try {
        // open the pdfwriter using the outstream
        writer = PdfWriter.getInstance(pdfdoc, out);
        LOGGER.debug("PDFWriter intstantiated");

        // register Fonts
        int numoffonts = FontFactory.registerDirectories();

        LOGGER.debug(numoffonts + " fonts found and registered!");

        if ((pdfa) && (iccprofile != null)) {
            // we want to write PDFA, we have to set the PDFX conformance
            // before we open the writer
            writer.setPDFXConformance(PdfWriter.PDFA1B);
        }

        // open the pdf document to add pages and other content
        try {
            pdfdoc.open();
            LOGGER.debug("PDFDocument opened");
        } catch (Exception e) {
            throw new PDFManagerException("PdfWriter was opened, but the pdf document couldn't be opened", e);
        }

        if ((pdfa) && (iccprofile != null)) {

            // set the required PDFDictionary which
            // contains the appropriate ICC profile
            PdfDictionary pdfdict_out = new PdfDictionary(PdfName.OUTPUTINTENT);

            // set identifier for ICC profile
            pdfdict_out.put(PdfName.OUTPUTCONDITIONIDENTIFIER, new PdfString("sRGBIEC61966-2.1"));
            pdfdict_out.put(PdfName.INFO, new PdfString("sRGB IEC61966-2.1"));
            pdfdict_out.put(PdfName.S, PdfName.GTS_PDFA1);

            // PdfICCBased ib = new PdfICCBased(iccprofile);
            // writer.setOutputIntents("Custom", "PDF/A sRGB", null, "PDF/A
            // sRGB ICC Profile, sRGB_IEC61966-2-1_withBPC.icc",
            // colorProfileData);

            // read icc profile
            // ICC_Profile icc = ICC_Profile.getInstance(new
            // FileInputStream("c:\\srgb.profile"));
            PdfICCBased ib = new PdfICCBased(iccprofile);
            ib.remove(PdfName.ALTERNATE);

            PdfIndirectObject pio = writer.addToBody(ib);
            pdfdict_out.put(PdfName.DESTOUTPUTPROFILE, pio.getIndirectReference());
            writer.getExtraCatalog().put(PdfName.OUTPUTINTENTS, new PdfArray(pdfdict_out));

            // create MarkInfo elements
            // not sure this is necessary; maybe just needed for tagged PDFs
            // (PDF/A 1a)
            PdfDictionary markInfo = new PdfDictionary(PdfName.MARKINFO);
            markInfo.put(PdfName.MARKED, new PdfBoolean("false"));
            writer.getExtraCatalog().put(PdfName.MARKINFO, markInfo);

            // write XMP
            this.writeXMPMetadata(writer);
        }
    } catch (Exception e) {
        LOGGER.error("Can't open the PdfWriter object\n" + e.toString() + "\n" + e.getMessage());
        throw new PDFManagerException("Can't open the PdfWriter object", e);
    }
    return writer;
}

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  w w  . j ava  2s . 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 ww w  .j  av a2 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 ww.j av  a2  s .  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:questions.images.PostCardExtra.java

public static void main(String[] args) {
    // step 1: creation of a document-object
    Document document = new Document(PageSize.POSTCARD);
    try {/*from w w w. j  av a2 s  .  c  o m*/
        // step 2:
        // we create a writer
        PdfWriter writer = PdfWriter.getInstance(
                // that listens to the document
                document,
                // and directs a PDF-stream to a file
                new FileOutputStream(RESULT));
        // step 3: we open the document
        document.open();
        // step 4: we add a paragraph to the document
        Image img = Image.getInstance(RESOURCE);
        img.scaleToFit(PageSize.POSTCARD.getWidth(), 10000);
        img.setAbsolutePosition(0, 0);
        PdfImage stream = new PdfImage(img, "", null);
        stream.put(new PdfName("MySpecialId"), new PdfName("123456789"));
        PdfIndirectObject ref = writer.addToBody(stream);
        img.setDirectReference(ref.getIndirectReference());
        document.add(img);
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }

    // step 5: we close the document
    document.close();
}