Java Code Examples for org.apache.pdfbox.cos.COSDictionary#getCOSName()

The following examples show how to use org.apache.pdfbox.cos.COSDictionary#getCOSName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PDFontFactory.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Creates a new PDCIDFont instance with the appropriate subclass.
 *
 * @param dictionary descendant font dictionary
 * @return a PDCIDFont instance, based on the SubType entry of the dictionary
 * @throws IOException if something goes wrong
 */
static PDCIDFont createDescendantFont(COSDictionary dictionary, PDType0Font parent)
        throws IOException
{
    COSName type = dictionary.getCOSName(COSName.TYPE, COSName.FONT);
    if (!COSName.FONT.equals(type))
    {
        throw new IllegalArgumentException("Expected 'Font' dictionary but found '" + type.getName() + "'");
    }

    COSName subType = dictionary.getCOSName(COSName.SUBTYPE);
    if (COSName.CID_FONT_TYPE0.equals(subType))
    {
        return new PDCIDFontType0(dictionary, parent);
    }
    else if (COSName.CID_FONT_TYPE2.equals(subType))
    {
        return new PDCIDFontType2(dictionary, parent);
    }
    else
    {
        throw new IOException("Invalid font type: " + type);
    }
}
 
Example 2
Source File: PDPageTree.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private static void sanitizeType(COSDictionary dictionary)
{
    COSName type = dictionary.getCOSName(COSName.TYPE);
    if (type == null)
    {
        dictionary.setItem(COSName.TYPE, COSName.PAGE);
        return;
    }
    if (!COSName.PAGE.equals(type))
    {
        throw new IllegalStateException("Expected 'Page' but found " + type);
    }
}
 
Example 3
Source File: PDPageTree.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Returns true if the node is a page tree node (i.e. and intermediate).
 */
private boolean isPageTreeNode(COSDictionary node )
{
    // some files such as PDFBOX-2250-229205.pdf don't have Pages set as the Type, so we have
    // to check for the presence of Kids too
    return node != null &&
           (node.getCOSName(COSName.TYPE) == COSName.PAGES || node.containsKey(COSName.KIDS));
}
 
Example 4
Source File: COSParser.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
private int checkPagesDictionary(COSDictionary pagesDict, Set<COSObject> set)
{
    // check for kids
    COSBase kids = pagesDict.getDictionaryObject(COSName.KIDS);
    int numberOfPages = 0;
    if (kids instanceof COSArray)
    {
        COSArray kidsArray = (COSArray) kids;
        List<? extends COSBase> kidsList = kidsArray.toList();
        for (COSBase kid : kidsList)
        {
            if (!(kid instanceof COSObject) || set.contains((COSObject) kid))
            {
                kidsArray.remove(kid);
                continue;
            }
            COSObject kidObject = (COSObject) kid;
            COSBase kidBaseobject = kidObject.getObject();
            // object wasn't dereferenced -> remove it
            if (kidBaseobject == null || kidBaseobject.equals(COSNull.NULL))
            {
                LOG.warn("Removed null object " + kid + " from pages dictionary");
                kidsArray.remove(kid);
            }
            else if (kidBaseobject instanceof COSDictionary)
            {
                COSDictionary kidDictionary = (COSDictionary) kidBaseobject;
                COSName type = kidDictionary.getCOSName(COSName.TYPE);
                if (COSName.PAGES.equals(type))
                {
                    // process nested pages dictionaries
                    set.add(kidObject);
                    numberOfPages += checkPagesDictionary(kidDictionary, set);
                }
                else if (COSName.PAGE.equals(type))
                {
                    // count pages
                    numberOfPages++;
                }
            }
        }
    }
    // fix counter
    pagesDict.setInt(COSName.COUNT, numberOfPages);
    return numberOfPages;
}
 
Example 5
Source File: PDSimpleFont.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Reads the Encoding from the Font dictionary or the embedded or substituted font file.
 * Must be called at the end of any subclass constructors.
 *
 * @throws IOException if the font file could not be read
 */
protected void readEncoding() throws IOException
{
    COSBase encoding = dict.getDictionaryObject(COSName.ENCODING);
    if (encoding != null)
    {
        if (encoding instanceof COSName)
        {
            COSName encodingName = (COSName)encoding;
            this.encoding = Encoding.getInstance(encodingName);
            if (this.encoding == null)
            {
                LOG.warn("Unknown encoding: " + encodingName.getName());
                this.encoding = readEncodingFromFont(); // fallback
            }
        }
        else if (encoding instanceof COSDictionary)
        {
            COSDictionary encodingDict = (COSDictionary)encoding;
            Encoding builtIn = null;
            Boolean symbolic = getSymbolicFlag();
            boolean isFlaggedAsSymbolic = symbolic != null && symbolic;

            COSName baseEncoding = encodingDict.getCOSName(COSName.BASE_ENCODING);
            
            boolean hasValidBaseEncoding = baseEncoding != null &&
                        Encoding.getInstance(baseEncoding) != null;
            
            if (!hasValidBaseEncoding && isFlaggedAsSymbolic)
            {
                builtIn = readEncodingFromFont();
            }

            if (symbolic == null)
            {
                symbolic = false;
            }
            this.encoding = new DictionaryEncoding(encodingDict, !symbolic, builtIn);
        }
    }
    else
    {
        this.encoding = readEncodingFromFont();
    }

    // normalise the standard 14 name, e.g "Symbol,Italic" -> "Symbol"
    String standard14Name = Standard14Fonts.getMappedFontName(getName());

    // assign the glyph list based on the font
    if ("ZapfDingbats".equals(standard14Name))
    {
        glyphList = GlyphList.getZapfDingbats();
    }
    else
    {
        // StandardEncoding and Symbol are in the AGL
        glyphList = GlyphList.getAdobeGlyphList();
    }
}
 
Example 6
Source File: CreateSignature.java    From testarea-pdfbox2 with Apache License 2.0 4 votes vote down vote up
/**
 * <p>
 * A minimal signing frame work merely requiring a {@link SignatureInterface}
 * instance signing an existing field and actually locking fields the transform
 * requires to be locked.
 * </p>
 * @see #signExistingFieldWithLock(PDDocument, OutputStream, SignatureInterface)
 */
void signAndLockExistingFieldWithLock(PDDocument document, OutputStream output, SignatureInterface signatureInterface) throws IOException
{
    PDSignatureField signatureField = document.getSignatureFields().get(0);
    PDSignature signature = new PDSignature();
    signatureField.setValue(signature);

    COSBase lock = signatureField.getCOSObject().getDictionaryObject(COS_NAME_LOCK);
    if (lock instanceof COSDictionary)
    {
        COSDictionary lockDict = (COSDictionary) lock;
        COSDictionary transformParams = new COSDictionary(lockDict);
        transformParams.setItem(COSName.TYPE, COSName.getPDFName("TransformParams"));
        transformParams.setItem(COSName.V, COSName.getPDFName("1.2"));
        transformParams.setDirect(true);
        COSDictionary sigRef = new COSDictionary();
        sigRef.setItem(COSName.TYPE, COSName.getPDFName("SigRef"));
        sigRef.setItem(COSName.getPDFName("TransformParams"), transformParams);
        sigRef.setItem(COSName.getPDFName("TransformMethod"), COSName.getPDFName("FieldMDP"));
        sigRef.setItem(COSName.getPDFName("Data"), document.getDocumentCatalog());
        sigRef.setDirect(true);
        COSArray referenceArray = new COSArray();
        referenceArray.add(sigRef);
        signature.getCOSObject().setItem(COSName.getPDFName("Reference"), referenceArray);

        final Predicate<PDField> shallBeLocked;
        final COSArray fields = lockDict.getCOSArray(COSName.FIELDS);
        final List<String> fieldNames = fields == null ? Collections.emptyList() :
            fields.toList().stream().filter(c -> (c instanceof COSString)).map(s -> ((COSString)s).getString()).collect(Collectors.toList());
        final COSName action = lockDict.getCOSName(COSName.getPDFName("Action"));
        if (action.equals(COSName.getPDFName("Include"))) {
            shallBeLocked = f -> fieldNames.contains(f.getFullyQualifiedName());
        } else if (action.equals(COSName.getPDFName("Exclude"))) {
            shallBeLocked = f -> !fieldNames.contains(f.getFullyQualifiedName());
        } else if (action.equals(COSName.getPDFName("All"))) {
            shallBeLocked = f -> true;
        } else { // unknown action, lock nothing
            shallBeLocked = f -> false;
        }
        lockFields(document.getDocumentCatalog().getAcroForm().getFields(), shallBeLocked);
    }

    signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE);
    signature.setSubFilter(PDSignature.SUBFILTER_ADBE_PKCS7_DETACHED);
    signature.setName("blablabla");
    signature.setLocation("blablabla");
    signature.setReason("blablabla");
    signature.setSignDate(Calendar.getInstance());
    document.addSignature(signature);
    ExternalSigningSupport externalSigning =
            document.saveIncrementalForExternalSigning(output);
    // invoke external signature service
    byte[] cmsSignature = signatureInterface.sign(externalSigning.getContent());
    // set signature bytes received from the service
    externalSigning.setSignature(cmsSignature);
}