Example usage for org.apache.pdfbox.cos COSStream getItem

List of usage examples for org.apache.pdfbox.cos COSStream getItem

Introduction

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

Prototype

public COSBase getItem(COSName key) 

Source Link

Document

This will do a lookup into the dictionary.

Usage

From source file:net.padaf.preflight.font.CompositeFontValidator.java

License:Apache License

/**
 * Standard information of a stream element will be checked by the
 * StreamHelper./*www  .  j  a  v a2  s  .c o  m*/
 * 
 * This method checks mandatory fields of the CMap stream. This method checks
 * too if the CMap stream is damaged using the CMapParser of the fontbox api.
 * 
 * @param aCMap
 * @return
 */
private boolean processCMapAsStream(COSStream aCMap) {
    COSDocument cDoc = handler.getDocument().getDocument();

    String type = aCMap.getNameAsString(COSName.getPDFName(DICTIONARY_KEY_TYPE));
    String cmapName = aCMap.getNameAsString(COSName.getPDFName(FONT_DICTIONARY_KEY_CMAP_NAME));
    COSBase sysinfo = aCMap.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_CID_SYSINFO));
    int wmode = aCMap.getInt(COSName.getPDFName(FONT_DICTIONARY_KEY_CMAP_WMODE));
    COSBase cmapUsed = aCMap.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_CMAP_USECMAP));

    if (!FONT_DICTIONARY_VALUE_TYPE_CMAP.equals(type)) {
        // ---- CMap type is invalid
        this.fontContainer.addError(
                new ValidationError(ERROR_FONTS_CIDKEYED_CMAP_INVALID_OR_MISSING, "The CMap type is invalid"));
        return false;
    }

    // ---- check the content of the CIDSystemInfo
    if (!checkCIDSystemInfo(sysinfo, cDoc)) {
        return false;
    }

    if (cmapName == null || "".equals(cmapName) || wmode > 1) {
        this.fontContainer.addError(new ValidationError(ERROR_FONTS_CIDKEYED_CMAP_INVALID_OR_MISSING,
                "Some elements in the CMap dictionary are missing or invalid"));
        return false;
    }

    try {

        CMap fontboxCMap = new CMapParser().parse(null, aCMap.getUnfilteredStream());
        int wmValue = fontboxCMap.getWMode();
        String cmnValue = fontboxCMap.getName(); //getCmapEntry("CMapName");

        if (wmValue != wmode) {

            this.fontContainer.addError(
                    new ValidationError(ERROR_FONTS_CIDKEYED_CMAP_INVALID_OR_MISSING, "WMode is inconsistent"));
            return false;
        }

        if (!cmnValue.equals(cmapName)) {

            this.fontContainer.addError(new ValidationError(ERROR_FONTS_CIDKEYED_CMAP_INVALID_OR_MISSING,
                    "CMapName is inconsistent"));
            return false;
        }

    } catch (IOException e) {
        this.fontContainer
                .addError(new ValidationError(ERROR_FONTS_CID_CMAP_DAMAGED, "The CMap type is damaged"));
        return false;
    }

    if (cmapUsed != null) {
        return checkCMap(cmapUsed);
    }

    return true;
}

From source file:net.padaf.preflight.helpers.StreamValidationHelper.java

License:Apache License

/**
 * This method checks if one of declared Filter is LZWdecode. If LZW is found,
 * the result list is updated with an error code.
 * /*  w  w w.  j  av a  2  s .  com*/
 * @param stream
 * @param handler
 * @param result
 */
protected void checkFilters(COSStream stream, DocumentHandler handler, List<ValidationError> result) {
    COSDocument cDoc = handler.getDocument().getDocument();
    COSBase bFilter = stream.getItem(COSName.getPDFName(STREAM_DICTIONARY_KEY_FILTER));
    if (bFilter != null) {
        if (COSUtils.isArray(bFilter, cDoc)) {
            COSArray afName = (COSArray) bFilter;
            for (int i = 0; i < afName.size(); ++i) {
                if (!FilterHelper.isAuthorizedFilter(afName.getString(i), result)) {
                    return;
                }
            }
        } else if (bFilter instanceof COSName) {
            String fName = ((COSName) bFilter).getName();
            if (!FilterHelper.isAuthorizedFilter(fName, result)) {
                return;
            }
        } else {
            // ---- The filter type is invalid
            result.add(new ValidationError(ERROR_SYNTAX_STREAM_INVALID_FILTER,
                    "Filter should be a Name or an Array"));
        }
    }
    //  else Filter entry is optional
}

From source file:org.apache.padaf.preflight.font.CompositeFontValidator.java

License:Apache License

/**
 * Standard information of a stream element will be checked by the
 * StreamHelper.//from   w w  w  . ja  va 2s .  c o m
 * 
 * This method checks mandatory fields of the CMap stream. This method checks
 * too if the CMap stream is damaged using the CMapParser of the fontbox api.
 * 
 * @param aCMap
 * @return
 */
private boolean processCMapAsStream(COSStream aCMap) {
    COSDocument cDoc = handler.getDocument().getDocument();

    String type = aCMap.getNameAsString(COSName.getPDFName(DICTIONARY_KEY_TYPE));
    String cmapName = aCMap.getNameAsString(COSName.getPDFName(FONT_DICTIONARY_KEY_CMAP_NAME));
    COSBase sysinfo = aCMap.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_CID_SYSINFO));
    int wmode = aCMap.getInt(COSName.getPDFName(FONT_DICTIONARY_KEY_CMAP_WMODE));
    if (wmode == -1) {
        /*
         * According to the getInt javadoc, -1 is returned if there are no result.
         * In the PDF Reference v1.7 p449, we can read that Default value is 0.
         */
        wmode = FONT_DICTIONARY_DEFAULT_CMAP_WMODE;
    }
    COSBase cmapUsed = aCMap.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_CMAP_USECMAP));

    if (!FONT_DICTIONARY_VALUE_TYPE_CMAP.equals(type)) {
        // ---- CMap type is invalid
        this.fontContainer.addError(
                new ValidationError(ERROR_FONTS_CIDKEYED_CMAP_INVALID_OR_MISSING, "The CMap type is invalid"));
        return false;
    }

    // ---- check the content of the CIDSystemInfo
    if (!checkCIDSystemInfo(sysinfo, cDoc)) {
        return false;
    }

    if (cmapName == null || "".equals(cmapName) || wmode > 1) {
        this.fontContainer.addError(new ValidationError(ERROR_FONTS_CIDKEYED_CMAP_INVALID_OR_MISSING,
                "Some elements in the CMap dictionary are missing or invalid"));
        return false;
    }

    try {

        CMap fontboxCMap = new CMapParser().parse(null, aCMap.getUnfilteredStream());
        int wmValue = fontboxCMap.getWMode();
        String cmnValue = fontboxCMap.getName();

        if (wmValue != wmode) {

            this.fontContainer.addError(
                    new ValidationError(ERROR_FONTS_CIDKEYED_CMAP_INVALID_OR_MISSING, "WMode is inconsistent"));
            return false;
        }

        if (!cmnValue.equals(cmapName)) {

            this.fontContainer.addError(new ValidationError(ERROR_FONTS_CIDKEYED_CMAP_INVALID_OR_MISSING,
                    "CMapName is inconsistent"));
            return false;
        }

    } catch (IOException e) {
        this.fontContainer
                .addError(new ValidationError(ERROR_FONTS_CID_CMAP_DAMAGED, "The CMap type is damaged"));
        return false;
    }

    if (cmapUsed != null) {
        return checkCMap(cmapUsed);
    }

    return true;
}