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

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

Introduction

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

Prototype

public PdfName(byte bytes[]) 

Source Link

Document

Constructs a PdfName.

Usage

From source file:eu.europa.ec.markt.dss.validation.pades.PDFDocumentValidator.java

License:Open Source License

@Override
public List<AdvancedSignature> getSignatures() {
    final List<AdvancedSignature> list = new ArrayList<AdvancedSignature>();

    try {//from   www  .j  av a2s.  c o m
        PDFSignatureService pdfSignatureService = new ITextPDFSignatureService();
        pdfSignatureService.validateSignatures(this.document.openStream(), new SignatureValidationCallback() {

            @Override
            public void validate(PdfReader reader, PdfDictionary outerCatalog, X509Certificate arg0, Date arg1,
                    Certificate[] arg2, PdfDictionary signatureDictionary, PdfPKCS7 pk) {

                if (arg0 == null) {
                    throw new NotETSICompliantException(MSG.NO_SIGNING_CERTIFICATE);
                }

                if (arg1 == null) {
                    // throw new NotETSICompliantException(MSG.NO_SIGNING_TIME);
                }

                try {
                    if (!signatureDictionary.get(new PdfName("Type")).equals(new PdfName("DocTimeStamp"))) {
                        list.add(new PAdESSignature(reader, outerCatalog, signatureDictionary, pk));
                    }
                } catch (CMSException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
    } catch (SignatureException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    return list;
}

From source file:eu.europa.ec.markt.dss.validation.pades.PDFDocumentValidator.java

License:Open Source License

@Override
protected SignatureLevelBES verifyLevelBES(AdvancedSignature signature, Date referenceTime,
        ValidationContext ctx) {/*  ww w . j  ava 2  s.  c  o m*/
    SignatureLevelBES superchecks = super.verifyLevelBES(signature, referenceTime, ctx);
    PAdESSignature pades = (PAdESSignature) signature;

    PdfName subfilter = pades.getSignatureDictionary().getAsName(PdfName.SUBFILTER);

    if (subfilter == null || (!subfilter.equals(new PdfName("ETSI.CAdES.detached"))
            && !subfilter.equals(new PdfName("ETSI.RFC3161")))) {
        LOG.warning(
                "Invalid or missing SubFilter value in the signature dictionary; should be either ETSI.CAdES.detached or ETSI.RFC3161");
    }

    return superchecks;
}

From source file:eu.europa.ec.markt.dss.validation.pades.PDFDocumentValidator.java

License:Open Source License

private boolean checkVriDict(PdfDictionary vriSigDictionary, boolean _vriVerificationresult,
        PAdESSignature pades, ValidationContext ctx, String hexHash)
        throws CertificateException, IOException, CRLException, OCSPException {

    boolean vriVerificationresult = _vriVerificationresult;

    if (vriSigDictionary == null) {
        LOG.info("Couldn't find the signature VRI identified by " + hexHash + " in the DSS");
        vriVerificationresult = false;//from  w w  w  .ja  v a2  s.co  m
    } else {
        LOG.info("Found the signature VRI identified by " + hexHash + " in the DSS");

        // Verify the certs in the VRI
        PdfArray vricert = vriSigDictionary.getAsArray(new PdfName("Cert"));
        if (vricert != null) {
            CertificateFactory factory = CertificateFactory.getInstance("X509");
            List<X509Certificate> certs = new ArrayList<X509Certificate>();
            for (int i = 0; i < vricert.size(); i++) {
                PdfStream stream = vricert.getAsStream(i);
                certs.add((X509Certificate) factory.generateCertificate(
                        new ByteArrayInputStream(PdfReader.getStreamBytes((PRStream) stream))));
            }
            vriVerificationresult &= everyCertificateValueAreThere(ctx, certs, pades.getSigningCertificate());
        }

        // Verify the CRLs in the VRI
        PdfArray vricrl = vriSigDictionary.getAsArray(new PdfName("CRL"));
        if (vricrl != null) {
            CertificateFactory factory = CertificateFactory.getInstance("X509");
            List<X509CRL> crls = new ArrayList<X509CRL>();
            for (int i = 0; i < vricrl.size(); i++) {
                PdfStream stream = vricrl.getAsStream(i);
                crls.add((X509CRL) factory
                        .generateCRL(new ByteArrayInputStream(PdfReader.getStreamBytes((PRStream) stream))));
            }
            vriVerificationresult &= everyCRLValueOrRefAreThere(ctx, crls);
        }

        // Verify the OCSPs in the VRI
        PdfArray vriocsp = vriSigDictionary.getAsArray(new PdfName("OCSP"));
        if (vriocsp != null) {
            List<BasicOCSPResp> ocsps = new ArrayList<BasicOCSPResp>();
            for (int i = 0; i < vriocsp.size(); i++) {
                PdfStream stream = vriocsp.getAsStream(i);
                ocsps.add((BasicOCSPResp) new OCSPResp(PdfReader.getStreamBytes((PRStream) stream))
                        .getResponseObject());
            }
            vriVerificationresult &= everyOCSPValueOrRefAreThere(ctx, ocsps);
        }

    }

    return vriVerificationresult;
}

From source file:eu.europa.ec.markt.dss.validation.pades.PDFDocumentValidator.java

License:Open Source License

@Override
protected SignatureLevelLTV verifyLevelLTV(AdvancedSignature signature, Date referenceTime,
        ValidationContext ctx) {//  w w  w  .java2s . c o m
    try {
        PAdESSignature pades = (PAdESSignature) signature;
        LOG.info("Starting LTV validation of signature: " + pades.getPdfPkcs7().getSignName() + " / "
                + PdfPKCS7.getSubjectFields(pades.getPdfPkcs7().getSigningCertificate()));

        PdfDictionary catalog = pades.getOuterCatalog();
        if (catalog == null) {
            catalog = pades.getPdfReader().getCatalog();
        }

        PdfDictionary dss = catalog.getAsDict(new PdfName("DSS"));

        if (dss == null) {
            LOG.info("No DSS dictionary!");
            return new SignatureLevelLTV(new Result(ResultStatus.INVALID, "no.dss.dictionary"), null, null);
        }

        LOG.info("DSS dictionary found");

        PdfName sigType = pades.getSignatureDictionary().getAsName(PdfName.TYPE);
        // PdfName subfilter = pades.getSignatureDictionary().getAsName(PdfName.SUBFILTER);

        TimestampVerificationResult docTimestampCheck = null;

        boolean dssCertsVerificationResult = everyCertificateValueAreThere(ctx,
                pades.getExtendedCertificateSource().getCertificates(), pades.getSigningCertificate());
        boolean dssRevocationVerificationResult = true;
        dssRevocationVerificationResult &= everyCRLValueOrRefAreThere(ctx, pades.getCRLs());
        dssRevocationVerificationResult &= everyOCSPValueOrRefAreThere(ctx, pades.getOCSPs());
        boolean vriVerificationresult = true;

        if (sigType != null) {
            if (sigType.equals(new PdfName("Sig"))) {
                // Standard signature

                PdfDictionary vri = dss.getAsDict(new PdfName("VRI"));

                if (vri == null) {
                    LOG.info("No VRI dictionary, this is optional but required by Adobe Acrobat");
                    return new SignatureLevelLTV(new Result(ResultStatus.INVALID, "no.vri.dictionary"), null,
                            null);
                }

                // Verify the VRI
                MessageDigest _md = MessageDigest.getInstance("SHA1");
                String hexHash = Hex
                        .encodeHexString(
                                _md.digest(pades.getSignatureDictionary().get(PdfName.CONTENTS).getBytes()))
                        .toUpperCase();

            } else if (sigType.equals(new PdfName("DocTimeStamp"))) {

            } else {
                throw new RuntimeException("Unknown signature dictionary type");
            }
        }

        Result levelReached = null;
        if (dssCertsVerificationResult && dssRevocationVerificationResult) {
            levelReached = new Result(ResultStatus.VALID, null);
        } else {
            levelReached = new Result();
            if (!dssCertsVerificationResult) {
                levelReached.setStatus(ResultStatus.INVALID, "dss.certs.verification.result.error");
            } else if (!dssRevocationVerificationResult) {
                levelReached.setStatus(ResultStatus.INVALID, "dss.revocation.verification.result.error");
            } else if (!vriVerificationresult) {
                levelReached.setStatus(ResultStatus.INVALID, "vri.verification.result.error");
            }
        }

        return new SignatureLevelLTV(levelReached,
                new Result((dssCertsVerificationResult) ? ResultStatus.VALID : ResultStatus.INVALID, null),
                new Result((dssRevocationVerificationResult) ? ResultStatus.VALID : ResultStatus.INVALID,
                        null));

    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}

From source file:ispyb.common.util.PDFFormFiller.java

License:Open Source License

/**
 * Render and finalize the PDF form filled with data
 * /*from   w ww.j a  v  a  2  s  . c o m*/
 * @throws Exception
 */
public void render() throws Exception {
    // This is mandatory not to have the "Expected a dict object"
    // message when the result ouput is openned!!!
    PdfDictionary pdfDictionary = (PdfDictionary) PdfReader
            .getPdfObject(this.reader.getCatalog().get(PdfName.ACROFORM));
    pdfDictionary.remove(new PdfName("XFA"));
    // ///////////////////////////////////////////////////////////

    if (this.stamper != null) {
        this.stamper.setFormFlattening(true);
        this.stamper.close();
        this.stamper = null;
    }
    if (this.reader != null) {
        this.reader.close();
        this.reader = null;
    }
    if (this.writer != null) {
        if (!this.writer.isCloseStream())
            this.writer.close();
        this.writer = null;
    }
    this.formFields = null;
}

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  ava 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  va 2  s.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 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  . j  ava 2 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:net.sf.jasperreports.engine.export.JRPdfExporterTagHelper.java

License:Open Source License

/**
 *
 *///from w w w  .j  a va 2  s .  c  om
protected void init(PdfContentByte pdfContentByte) {
    this.pdfContentByte = pdfContentByte;

    if (isTagged) {
        PdfStructureTreeRoot root = pdfWriter.getStructureTreeRoot();
        allTag = new PdfStructureElement(root, new PdfName("All"));
        root.mapRole(new PdfName("All"), new PdfName("Sect"));
        if (pdfWriter.getPDFXConformance() == PdfWriter.PDFA1A) {
            root.mapRole(new PdfName("Anchor"), PdfName.NONSTRUCT);
            root.mapRole(PdfName.TEXT, PdfName.SPAN);
            root.mapRole(PdfName.IMAGE, PdfName.FIGURE);
        } else {
            root.mapRole(new PdfName("Anchor"), PdfName.TEXT);
        }

        if (language != null) {
            allTag.put(PdfName.LANG, new PdfString(language));
        }
        tagStack = new Stack<PdfStructureElement>();
        tagStack.push(allTag);
    }
}

From source file:net.sf.jasperreports.engine.export.JRPdfExporterTagHelper.java

License:Open Source License

protected void startPageAnchor() {
    if (isTagged) {
        PdfStructureElement textTag = new PdfStructureElement(allTag, new PdfName("Anchor"));
        pdfContentByte.beginMarkedContentSequence(textTag);
    }/*from w  w w  . j  a v  a2  s. com*/
}