Example usage for com.lowagie.text.pdf PdfDictionary PdfDictionary

List of usage examples for com.lowagie.text.pdf PdfDictionary PdfDictionary

Introduction

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

Prototype

public PdfDictionary(PdfName type) 

Source Link

Document

Constructs a PdfDictionary-object of a certain type.

Usage

From source file:com.dlya.facturews.DlyaPdfExporter2.java

License:Open Source License

/**
 *
 *//*from ww  w .ja v  a  2s.c o m*/
protected void exportReportToStream(OutputStream os) throws JRException {
    //ByteArrayOutputStream baos = new ByteArrayOutputStream();

    document = new Document(new Rectangle(jasperPrint.getPageWidth(), jasperPrint.getPageHeight()));

    imageTesterDocument = new Document(new Rectangle(10, //jasperPrint.getPageWidth(),
            10 //jasperPrint.getPageHeight()
    ));

    boolean closeDocuments = true;
    try {
        pdfWriter = PdfWriter.getInstance(document, os);
        pdfWriter.setCloseStream(false);

        if (pdfVersion != null) {
            pdfWriter.setPdfVersion(pdfVersion.charValue());
        }
        if (isCompressed) {
            pdfWriter.setFullCompression();
        }
        if (isEncrypted) {
            pdfWriter.setEncryption(is128BitKey, userPassword, ownerPassword, permissions);
        }

        if (printScaling != null) {
            if (JRPdfExporterParameter.PRINT_SCALING_DEFAULT.equals(printScaling)) {
                //pdfWriter.addViewerPreference(PdfName.PRINTSCALING, PdfName.APPDEFAULT);
            } else if (JRPdfExporterParameter.PRINT_SCALING_NONE.equals(printScaling)) {
                //pdfWriter.addViewerPreference(PdfName.PRINTSCALING, PdfName.NONE);
            }
        }

        // Add meta-data parameters to generated PDF document
        // mtclough@users.sourceforge.net 2005-12-05
        String title = (String) parameters.get(JRPdfExporterParameter.METADATA_TITLE);
        if (title != null) {
            document.addTitle(title);
        }
        String author = (String) parameters.get(JRPdfExporterParameter.METADATA_AUTHOR);
        if (author != null) {
            document.addAuthor(author);
        }
        String subject = (String) parameters.get(JRPdfExporterParameter.METADATA_SUBJECT);
        if (subject != null) {
            document.addSubject(subject);
        }
        String keywords = (String) parameters.get(JRPdfExporterParameter.METADATA_KEYWORDS);
        if (keywords != null) {
            document.addKeywords(keywords);
        }
        String creator = (String) parameters.get(JRPdfExporterParameter.METADATA_CREATOR);
        if (creator != null) {
            document.addCreator(creator);
        } else {
            document.addCreator("JasperReports (" + jasperPrint.getName() + ")");
        }

        // BEGIN: PDF/A support
        String pdfaConformance = getStringParameter(JRPdfExporterParameter.PDFA_CONFORMANCE,
                JRPdfExporterParameter.PROPERTY_PDFA_CONFORMANCE);
        boolean gotPdfa = false;
        if (pdfaConformance != null
                && !JRPdfExporterParameter.PDFA_CONFORMANCE_NONE.equalsIgnoreCase(pdfaConformance)) {
            if (JRPdfExporterParameter.PDFA_CONFORMANCE_1A.equalsIgnoreCase(pdfaConformance)) {
                //pdfWriter.setPDFXConformance(PdfWriter.PDFA1A);
                gotPdfa = true;
            } else if (JRPdfExporterParameter.PDFA_CONFORMANCE_1B.equalsIgnoreCase(pdfaConformance)) {
                //pdfWriter.setPDFXConformance(PdfWriter.PDFA1B);
                gotPdfa = true;
            }
        }

        if (gotPdfa) {

            //pdfWriter.createXmpMetadata();
        } else {
            //pdfWriter.setRgbTransparencyBlending(true);
        }
        // END: PDF/A support

        document.open();

        // BEGIN: PDF/A support
        if (gotPdfa) {
            String iccProfilePath = getStringParameter(JRPdfExporterParameter.PDFA_ICC_PROFILE_PATH,
                    JRPdfExporterParameter.PROPERTY_PDFA_ICC_PROFILE_PATH);
            if (iccProfilePath != null) {
                PdfDictionary pdfDictionary = new PdfDictionary(PdfName.OUTPUTINTENT);
                pdfDictionary.put(PdfName.OUTPUTCONDITIONIDENTIFIER, new PdfString("sRGB IEC61966-2.1"));
                pdfDictionary.put(PdfName.INFO, new PdfString("sRGB IEC61966-2.1"));
                //pdfDictionary.put(PdfName.S, PdfName.GTS_PDFA1);

                InputStream iccIs = RepositoryUtil.getInstance(jasperReportsContext)
                        .getInputStreamFromLocation(iccProfilePath);
                //PdfICCBased pdfICCBased = new PdfICCBased(ICC_Profile.getInstance(iccIs));
                //pdfICCBased.remove(PdfName.ALTERNATE);
                //pdfDictionary.put(PdfName.DESTOUTPUTPROFILE, pdfWriter.addToBody(pdfICCBased).getIndirectReference());

                pdfWriter.getExtraCatalog().put(PdfName.OUTPUTINTENTS, new PdfArray(pdfDictionary));
            } else {
                throw new JRPdfaIccProfileNotFoundException();
            }
        }
        // END: PDF/A support

        if (pdfJavaScript != null) {
            pdfWriter.addJavaScript(pdfJavaScript);
        }

        pdfContentByte = pdfWriter.getDirectContent();

        //tagHelper.init(pdfContentByte);

        initBookmarks();

        PdfWriter imageTesterPdfWriter = PdfWriter.getInstance(imageTesterDocument, new NullOutputStream() // discard the output
        );
        imageTesterDocument.open();
        imageTesterDocument.newPage();
        imageTesterPdfContentByte = imageTesterPdfWriter.getDirectContent();
        imageTesterPdfContentByte.setLiteral("\n");

        for (reportIndex = 0; reportIndex < jasperPrintList.size(); reportIndex++) {
            setJasperPrint(jasperPrintList.get(reportIndex));
            loadedImagesMap = new HashMap<Renderable, com.lowagie.text.Image>();

            setPageSize(null);

            BorderOffset.setLegacy(JRPropertiesUtil.getInstance(jasperReportsContext)
                    .getBooleanProperty(jasperPrint, BorderOffset.PROPERTY_LEGACY_BORDER_OFFSET, false));

            boolean sizePageToContent = JRPropertiesUtil.getInstance(jasperReportsContext).getBooleanProperty(
                    jasperPrint, JRPdfExporterParameter.PROPERTY_SIZE_PAGE_TO_CONTENT, false);

            List<JRPrintPage> pages = jasperPrint.getPages();
            if (pages != null && pages.size() > 0) {
                if (isModeBatch) {
                    document.newPage();

                    if (isCreatingBatchModeBookmarks) {
                        //add a new level to our outline for this report
                        addBookmark(0, jasperPrint.getName(), 0, 0);
                    }

                    startPageIndex = 0;
                    endPageIndex = pages.size() - 1;
                }

                for (int pageIndex = startPageIndex; pageIndex <= endPageIndex; pageIndex++) {
                    if (Thread.interrupted()) {
                        throw new JRException("Current thread interrupted.");
                    }

                    JRPrintPage page = pages.get(pageIndex);

                    if (sizePageToContent) {
                        setPageSize(page);
                    }

                    document.newPage();

                    pdfContentByte = pdfWriter.getDirectContent();

                    pdfContentByte.setLineCap(2);//PdfContentByte.LINE_CAP_PROJECTING_SQUARE since iText 1.02b

                    writePageAnchor(pageIndex);

                    /*   */
                    exportPage(page);
                }
            } else {
                document.newPage();
                pdfContentByte = pdfWriter.getDirectContent();
                pdfContentByte.setLiteral("\n");
            }
        }

        closeDocuments = false;
        document.close();
        imageTesterDocument.close();
    } catch (DocumentException e) {
        throw new JRException("PDF Document error : " + jasperPrint.getName(), e);
    } catch (IOException e) {
        throw new JRException("Error generating PDF report : " + jasperPrint.getName(), e);
    } finally {
        if (closeDocuments) //only on exception
        {
            try {
                document.close();
            } catch (Exception e) {
                // ignore, let the original exception propagate
            }

            try {
                imageTesterDocument.close();
            } catch (Exception e) {
                // ignore, let the original exception propagate
            }
        }
    }

    //return os.toByteArray();
}

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

License:Apache License

/**
 * Creates the pdf writer./*from   ww w .  j  av  a 2 s  .c  om*/
 * 
 * @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:eu.europa.ec.markt.dss.signature.pades.PAdESProfileLTV.java

License:Open Source License

@Override
public Document extendSignatures(Document document, Document originalData, SignatureParameters parameters)
        throws IOException {

    try {/*from   w w  w .j ava 2 s . com*/
        final PdfReader reader = new PdfReader(document.openStream());
        final ByteArrayOutputStream output = new ByteArrayOutputStream();
        final PdfStamper stamper = new PdfStamper(reader, output, '\0', true);

        LTVSignatureValidationCallback callback = new LTVSignatureValidationCallback(stamper);
        pdfSignatureService.validateSignatures(document.openStream(), callback);

        PdfIndirectReference certsRef = stamper.getWriter().getPdfIndirectReference();
        stamper.getWriter().addToBody(callback.getCertsArray(), certsRef, false);

        PdfDictionary dssDictionary = new PdfDictionary(new PdfName("DSS"));
        PdfDictionary vriDictionary = new PdfDictionary(new PdfName("VRI"));

        PdfDictionary sigVriDictionary = new PdfDictionary();

        integrateCRL(callback, stamper, dssDictionary, sigVriDictionary, sigVriDictionary);

        integrateOCSP(callback, stamper, dssDictionary, sigVriDictionary, sigVriDictionary);

        // Add the signature's VRI dictionary, hashing the signature block from the callback method
        MessageDigest _md = MessageDigest.getInstance(DigestAlgorithm.SHA1.getName());
        String hexHash = Hex.encodeHexString(_md.digest(callback.getSignatureBlock())).toUpperCase();

        PdfIndirectReference sigVriRef = stamper.getWriter().getPdfIndirectReference();
        stamper.getWriter().addToBody(sigVriDictionary, sigVriRef, false);
        vriDictionary.put(new PdfName(hexHash), sigVriRef);
        PdfIndirectReference vriRef = stamper.getWriter().getPdfIndirectReference();
        stamper.getWriter().addToBody(vriDictionary, vriRef, false);

        // Add final objects to DSS dictionary
        dssDictionary.put(new PdfName("VRI"), vriRef);
        dssDictionary.put(new PdfName("Certs"), certsRef);

        PdfIndirectReference dssRef = stamper.getWriter().getPdfIndirectReference();
        stamper.getWriter().addToBody(dssDictionary, dssRef, false);
        reader.getCatalog().put(new PdfName("DSS"), dssRef);

        // /Extensions<</ADBE<</BaseVersion/1.7/ExtensionLevel 5>>>>
        PdfDeveloperExtension etsiExtension = new PdfDeveloperExtension(PdfName.ADBE, new PdfName("1.7"), 5);
        stamper.getWriter().addDeveloperExtension(etsiExtension);
        stamper.getWriter().addToBody(reader.getCatalog(), reader.getCatalog().getIndRef(), false);

        stamper.close();
        output.close();

        Document extendedDocument = new InMemoryDocument(output.toByteArray());

        ByteArrayOutputStream ltvDoc = new ByteArrayOutputStream();

        ITextPDFDocTimeSampService service = new ITextPDFDocTimeSampService();
        byte[] digest = service.digest(extendedDocument.openStream(), parameters);
        TimeStampResponse tsToken = tspSource.getTimeStampResponse(parameters.getDigestAlgorithm(), digest);
        service.sign(extendedDocument.openStream(), tsToken.getTimeStampToken().getEncoded(), ltvDoc,
                parameters);

        return new InMemoryDocument(ltvDoc.toByteArray());

    } catch (DocumentException ex) {
        throw new RuntimeException(ex);
    } catch (SignatureException e) {
        throw new RuntimeException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }

}

From source file:eu.europa.ec.markt.dss.signature.pdf.itext.ITextPdfDict.java

License:Open Source License

ITextPdfDict(String dictionaryType) {
    if (dictionaryType != null) {
        wrapped = new PdfDictionary(new PdfName(dictionaryType));
    } else {/* www.j  a  va  2s .  c o m*/
        wrapped = new PdfDictionary();
    }
}

From source file:net.sf.sze.service.impl.converter.PdfConverterImpl.java

License:GNU General Public License

/**
 * Put PDF-A-Infos to the document.// www .ja v a2s. c  om
 * @param writer the pdf-writer.
 * @throws IOException IOException.
 */
private void addPdfAInfosToDictonary(PdfWriter writer) throws IOException {
    PdfDictionary outi = new PdfDictionary(PdfName.OUTPUTINTENT);
    outi.put(PdfName.OUTPUTCONDITIONIDENTIFIER, new PdfString("sRGB IEC61966-2.1"));
    outi.put(PdfName.INFO, new PdfString("sRGB IEC61966-2.1"));
    outi.put(PdfName.S, PdfName.GTS_PDFA1);

    ICC_Profile icc = ICC_Profile.getInstance(ColorSpace.CS_sRGB);
    PdfICCBased ib = new PdfICCBased(icc);
    ib.remove(PdfName.ALTERNATE);
    outi.put(PdfName.DESTOUTPUTPROFILE, writer.addToBody(ib).getIndirectReference());
    writer.getExtraCatalog().put(PdfName.OUTPUTINTENTS, new PdfArray(outi));
}