Example usage for org.apache.pdfbox.cos COSDictionary COSDictionary

List of usage examples for org.apache.pdfbox.cos COSDictionary COSDictionary

Introduction

In this page you can find the example usage for org.apache.pdfbox.cos COSDictionary COSDictionary.

Prototype

public COSDictionary() 

Source Link

Document

Constructor.

Usage

From source file:eu.europa.esig.dss.pdf.pdfbox.PdfBoxSignatureService.java

License:Open Source License

private COSDictionary buildDSSDictionary(List<DSSDictionaryCallback> callbacks) throws Exception {
    COSDictionary dss = new COSDictionary();

    Map<String, COSStream> streams = new HashMap<String, COSStream>();

    Set<CRLToken> allCrls = new HashSet<CRLToken>();
    Set<OCSPToken> allOcsps = new HashSet<OCSPToken>();
    Set<CertificateToken> allCertificates = new HashSet<CertificateToken>();

    COSDictionary vriDictionary = new COSDictionary();
    for (DSSDictionaryCallback callback : callbacks) {
        COSDictionary sigVriDictionary = new COSDictionary();
        sigVriDictionary.setDirect(true);

        if (CollectionUtils.isNotEmpty(callback.getCertificates())) {
            COSArray vriCertArray = new COSArray();
            for (CertificateToken token : callback.getCertificates()) {
                vriCertArray.add(getStream(streams, token));
                allCertificates.add(token);
            }//from  w ww. j a v a 2 s .  co  m
            sigVriDictionary.setItem("Cert", vriCertArray);
        }

        if (CollectionUtils.isNotEmpty(callback.getOcsps())) {
            COSArray vriOcspArray = new COSArray();
            for (OCSPToken token : callback.getOcsps()) {
                vriOcspArray.add(getStream(streams, token));
                allOcsps.add(token);
            }
            sigVriDictionary.setItem("OCSP", vriOcspArray);
        }

        if (CollectionUtils.isNotEmpty(callback.getCrls())) {
            COSArray vriCrlArray = new COSArray();
            for (CRLToken token : callback.getCrls()) {
                vriCrlArray.add(getStream(streams, token));
                allCrls.add(token);
            }
            sigVriDictionary.setItem("CRL", vriCrlArray);
        }

        PAdESSignature signature = callback.getSignature();
        final byte[] digest = DSSUtils.digest(DigestAlgorithm.SHA1,
                signature.getCAdESSignature().getCmsSignedData().getEncoded());
        String hexHash = Hex.encodeHexString(digest).toUpperCase();

        vriDictionary.setItem(hexHash, sigVriDictionary);
    }
    dss.setItem("VRI", vriDictionary);

    if (CollectionUtils.isNotEmpty(allCertificates)) {
        COSArray arrayAllCerts = new COSArray();
        for (CertificateToken token : allCertificates) {
            arrayAllCerts.add(getStream(streams, token));
        }
        dss.setItem("Certs", arrayAllCerts);
    }

    if (CollectionUtils.isNotEmpty(allOcsps)) {
        COSArray arrayAllOcsps = new COSArray();
        for (OCSPToken token : allOcsps) {
            arrayAllOcsps.add(getStream(streams, token));
        }
        dss.setItem("OCSPs", arrayAllOcsps);
    }

    if (CollectionUtils.isNotEmpty(allCrls)) {
        COSArray arrayAllCrls = new COSArray();
        for (CRLToken token : allCrls) {
            arrayAllCrls.add(getStream(streams, token));
        }
        dss.setItem("CRLs", arrayAllCrls);
    }

    return dss;
}

From source file:fi.nls.oskari.printout.printing.PDPageContentStream.java

License:Apache License

private void writeColorSpace(PDColorSpace colorSpace) throws IOException {
    COSName key = null;/*from ww w  . j  a  va2 s  .  com*/
    if (colorSpace instanceof PDDeviceGray || colorSpace instanceof PDDeviceRGB
            || colorSpace instanceof PDDeviceCMYK) {
        key = COSName.getPDFName(colorSpace.getName());
    } else {
        COSDictionary colorSpaces = (COSDictionary) resources.getCOSDictionary()
                .getDictionaryObject(COSName.COLORSPACE);
        if (colorSpaces == null) {
            colorSpaces = new COSDictionary();
            resources.getCOSDictionary().setItem(COSName.COLORSPACE, colorSpaces);
        }
        key = colorSpaces.getKeyForValue(colorSpace.getCOSObject());

        if (key == null) {
            int counter = 0;
            String csName = "CS";
            while (colorSpaces.containsValue(csName + counter)) {
                counter++;
            }
            key = COSName.getPDFName(csName + counter);
            colorSpaces.setItem(key, colorSpace);
        }
    }
    key.writePDF(output);
    appendRawCommands(SPACE);
}

From source file:fixture.pdfboxeg.CreateSignatureBase.java

License:Apache License

public void setMDPPermission(PDDocument doc, PDSignature signature, int accessPermissions) {
    COSDictionary sigDict = signature.getCOSObject();

    // DocMDP specific stuff
    COSDictionary transformParameters = new COSDictionary();
    transformParameters.setItem(COSName.TYPE, COSName.getPDFName("TransformParams"));
    transformParameters.setInt(COSName.P, accessPermissions);
    transformParameters.setName(COSName.V, "1.2");
    transformParameters.setNeedToBeUpdated(true);

    COSDictionary referenceDict = new COSDictionary();
    referenceDict.setItem(COSName.TYPE, COSName.getPDFName("SigRef"));
    referenceDict.setItem("TransformMethod", COSName.getPDFName("DocMDP"));
    referenceDict.setItem("DigestMethod", COSName.getPDFName("SHA1"));
    referenceDict.setItem("TransformParams", transformParameters);
    referenceDict.setNeedToBeUpdated(true);

    COSArray referenceArray = new COSArray();
    referenceArray.add(referenceDict);/*  w  ww .  ja  v  a 2 s  .  co m*/
    sigDict.setItem("Reference", referenceArray);
    referenceArray.setNeedToBeUpdated(true);

    // Catalog
    COSDictionary catalogDict = doc.getDocumentCatalog().getCOSObject();
    COSDictionary permsDict = new COSDictionary();
    catalogDict.setItem(COSName.PERMS, permsDict);
    permsDict.setItem(COSName.DOCMDP, signature);
    catalogDict.setNeedToBeUpdated(true);
    permsDict.setNeedToBeUpdated(true);
}

From source file:net.padaf.preflight.utils.TestCOSUtils.java

License:Apache License

@Test
public void testIsDictionary() {
    try {/*from  w  w w  .j  av a 2 s  .  c  om*/
        COSObject co = new COSObject(new COSDictionary());
        co.setGenerationNumber(new COSInteger(0));
        co.setObjectNumber(new COSInteger(10));

        assertFalse(COSUtils.isDictionary(co, new IOCOSDocument()));

        COSDocument doc = new COSDocument();
        doc.setXRef(new COSObjectKey(co), 1000);
        COSUtils.isDictionary(co, doc);
        doc.close();
    } catch (IOException e) {
        fail(e.getMessage());
    }
}

From source file:org.apache.fop.render.pdf.DocumentRootModifierTestCase.java

License:Apache License

@Test
public void testStructTreeRootEntriesToCopy() throws IOException {
    Rectangle2D r = new Rectangle2D.Double();
    PDFDocument pdfDoc = new PDFDocument("");
    PDFPage page = new PDFPage(new PDFResources(pdfDoc), 0, r, r, r, r);
    page.setObjectNumber(1);//from   w  ww  .  ja v a 2s.  com
    page.setDocument(pdfDoc);
    pdfDoc.makeStructTreeRoot(null);
    PDFStructTreeRoot structTreeRoot = pdfDoc.getRoot().getStructTreeRoot();
    PDFDictionary rootBaseRoleMap = new PDFDictionary();
    PDFBoxAdapter adapter = new PDFBoxAdapter(page, new HashMap(), new HashMap<Integer, PDFArray>());
    DocumentRootModifier modifier = new DocumentRootModifier(adapter, pdfDoc);
    COSDictionary root = new COSDictionary();
    COSDictionary mapRole = new COSDictionary();
    mapRole.setName("Icon", "Figure");
    root.setItem(COSName.ROLE_MAP, mapRole);
    modifier.structTreeRootEntriesToCopy(root);
    structTreeRoot = pdfDoc.getRoot().getStructTreeRoot();
    PDFDictionary baseRoot = (PDFDictionary) structTreeRoot.get("RoleMap");
    String test = baseRoot.get("Icon").toString();
    String expected = "/Figure";
    Assert.assertEquals(test, expected);

    PDFName para = new PDFName("P");
    rootBaseRoleMap.put("MyPara", para);
    structTreeRoot.put("RoleMap", rootBaseRoleMap);
    modifier.structTreeRootEntriesToCopy(root);
    structTreeRoot = pdfDoc.getRoot().getStructTreeRoot();
    PDFDictionary baseRoot2 = (PDFDictionary) structTreeRoot.get("RoleMap");
    PDFName nameIcon = (PDFName) baseRoot2.get("Icon");
    PDFName myPara = (PDFName) baseRoot2.get("MyPara");
    test = nameIcon.getName();
    expected = "Figure";
    Assert.assertEquals(test, expected);
    test = myPara.getName();
    expected = "P";
    Assert.assertEquals(test, expected);

    PDDocument doc = PDDocument.load(new File(getClass().getResource(CLASSMAP).getFile()));
    COSDictionary temp = (COSDictionary) doc.getDocumentCatalog().getStructureTreeRoot().getCOSObject();
    PDFDictionary classMap = new PDFDictionary();
    PDFDictionary inner = new PDFDictionary();
    inner.put("StartIndent", 0);
    classMap.put("Normal2", inner);
    structTreeRoot.put("ClassMap", classMap);
    modifier.structTreeRootEntriesToCopy(temp);
    structTreeRoot = pdfDoc.getRoot().getStructTreeRoot();
    PDFDictionary testDict = (PDFDictionary) structTreeRoot.get("ClassMap");
    Assert.assertNotNull(testDict.get("Normal2"));
}

From source file:org.apache.fop.render.pdf.StructureTreeMergerTestCase.java

License:Apache License

@Test
public void testDirectDescedants() throws IOException {
    PDFStructElem elem = new PDFStructElem();
    elem.setObjectNumber(100);//  w  ww  .java 2  s .  c o m
    setUp();
    adapter = new PDFBoxAdapter(pdfPage, new HashMap(), new HashMap<Integer, PDFArray>());
    PDFLogicalStructureHandler handler = setUpPDFLogicalStructureHandler();
    PDPage srcPage = new PDPage();
    StructureTreeMerger merger = new StructureTreeMerger(elem, handler, adapter, srcPage);
    COSArray array = new COSArray();
    COSDictionary dict = new COSDictionary();
    dict.setItem(COSName.S, COSName.P);
    COSObject obj = new COSObject(dict);
    obj.setObjectNumber(200);
    obj.setGenerationNumber(0);
    array.add(0, obj);
    merger.createDirectDescendants(array, elem);
    List<PDFObject> list = elem.getKids();
    PDFStructElem kid = (PDFStructElem) list.get(0);
    PDFName name = (PDFName) kid.get("S");
    String test = name.getName();
    Assert.assertEquals(test, "P");
}

From source file:org.apache.fop.render.pdf.StructureTreeMergerUtilTestCase.java

License:Apache License

@Test
public void testFindRoleMapKeyByValue() {

    COSDictionary rolemap = new COSDictionary();
    COSName key1 = COSName.getPDFName("Para");
    COSName value1 = COSName.getPDFName("P");
    COSName key2 = COSName.getPDFName("Icon");
    COSName value2 = COSName.getPDFName("Image");
    rolemap.setItem(key1, value1);/*from   w w w  .  java2s. c o m*/
    rolemap.setItem(key2, value2);
    String type = "Image";
    List<String> result = StructureTreeMergerUtil.findRoleMapKeyByValue(type, rolemap);
    String test = result.get(0);
    String expected = "Icon";
    Assert.assertEquals(test, expected);
}

From source file:org.apache.padaf.preflight.utils.TestCOSUtils.java

License:Apache License

@Test
public void testIsDictionary() {
    try {//from   ww w. j  av a 2  s  . c  o  m
        COSObject co = new COSObject(new COSDictionary());
        co.setGenerationNumber(COSInteger.ZERO);
        co.setObjectNumber(new COSInteger(10));

        assertFalse(COSUtils.isDictionary(co, new IOCOSDocument()));

        COSDocument doc = new COSDocument();
        addToXref(doc, new COSObjectKey(co), 1000);
        COSUtils.isDictionary(co, doc);
        doc.close();
    } catch (IOException e) {
        fail(e.getMessage());
    }
}

From source file:org.lockss.pdf.pdfbox.PdfBoxDocument.java

License:Open Source License

@Override
public Map<String, PdfToken> getTrailer() {
    COSDictionary trailer = pdDocument.getDocument().getTrailer();
    if (trailer == null) {
        trailer = new COSDictionary();
    }//  ww w  . j a v a2s. com
    return PdfBoxTokens.getDictionary(trailer);
}

From source file:org.lockss.pdf.pdfbox.PdfBoxTokens.java

License:Open Source License

/**
 * <p>//from   ww w  .j a  va 2s  .  c  o  m
 * Converts from a map from strings to PDF tokens to a 
 * {@link COSDictionary}.
 * </p>
 * @param mapping A map from strings (PDF names) to PDF tokens.
 * @return A {@link COSDictionary} instance.
 * @since 1.56
 */
protected static COSDictionary asCOSDictionary(Map<String, PdfToken> mapping) {
    COSDictionary cosDictionary = new COSDictionary();
    for (Map.Entry<String, PdfToken> entry : mapping.entrySet()) {
        cosDictionary.setItem(asCOSName(entry.getKey()), asCOSBase(entry.getValue()));
    }
    return cosDictionary;
}