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

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

Introduction

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

Prototype

public String getNameAsString(String key) 

Source Link

Document

This is a convenience method that will get the dictionary object that is expected to be a name and convert it to a string.

Usage

From source file:com.aaasec.sigserv.csspsupport.pdfbox.modifications.CsCOSWriter.java

License:Apache License

/**
 * visitFromStream method comment./*from   w ww  .  j  ava2 s . c om*/
 *
 * @param obj The object that is being visited.
 *
 * @throws COSVisitorException If there is an exception while visiting this
 * object.
 *
 * @return null
 */
public Object visitFromStream(COSStream obj) throws COSVisitorException {
    InputStream input = null;
    try {
        if (willEncrypt) {
            document.getSecurityHandler().encryptStream(obj, currentObjectKey.getNumber(),
                    currentObjectKey.getGeneration());
        }

        COSObject lengthObject = null;
        // check if the length object is required to be direct, like in
        // a cross reference stream dictionary
        COSBase lengthEntry = obj.getDictionaryObject(COSName.LENGTH);
        String type = obj.getNameAsString(COSName.TYPE);
        if (lengthEntry != null && lengthEntry.isDirect() || "XRef".equals(type)) {
            // the length might be the non encoded length,
            // set the real one as direct object
            COSInteger cosInteger = COSInteger.get(obj.getFilteredLength());
            cosInteger.setDirect(true);
            obj.setItem(COSName.LENGTH, cosInteger);

        } else {
            // make the length an implicit indirect object
            // set the length of the stream and write stream dictionary
            lengthObject = new COSObject(null);

            obj.setItem(COSName.LENGTH, lengthObject);
        }
        input = obj.getFilteredStream();
        //obj.accept(this);
        // write the stream content
        visitFromDictionary(obj);
        getStandardOutput().write(STREAM);
        getStandardOutput().writeCRLF();
        byte[] buffer = new byte[1024];
        int amountRead = 0;
        int totalAmountWritten = 0;
        while ((amountRead = input.read(buffer, 0, 1024)) != -1) {
            getStandardOutput().write(buffer, 0, amountRead);
            totalAmountWritten += amountRead;
        }
        // set the length as an indirect object
        if (lengthObject != null) {
            lengthObject.setObject(COSInteger.get(totalAmountWritten));
        }
        getStandardOutput().writeCRLF();
        getStandardOutput().write(ENDSTREAM);
        getStandardOutput().writeEOL();
        return null;
    } catch (Exception e) {
        throw new COSVisitorException(e);
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                throw new COSVisitorException(e);
            }
        }
    }
}

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

License:Apache License

/**
 * Standard information of a stream element will be checked by the
 * StreamHelper.//from  www .  j  a va 2 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.font.CompositeFontValidator.java

License:Apache License

/**
 * This method return false and updates the FontContainer if the Composite
 * Font TYPE 0 or TYPE1C is damaged. This method checks the Widths
 * consistency.//from   ww w  .  j  a v a  2s.c  o  m
 * 
 * @param pfDescriptor
 * @return
 */
boolean checkFontFileElement_CIDFontType0(PDFontDescriptorDictionary pfDescriptor) throws ValidationException {
    // ---- FontFile Validation
    PDStream ff1 = pfDescriptor.getFontFile();
    PDStream ff2 = pfDescriptor.getFontFile2();
    PDStream ff3 = pfDescriptor.getFontFile3();

    boolean onlyOne = (ff1 != null && ff2 == null && ff3 == null) || (ff1 == null && ff2 != null && ff3 == null)
            || (ff1 == null && ff2 == null && ff3 != null);

    if ((ff3 == null) || !onlyOne) {
        this.fontContainer.addError(new ValidationResult.ValidationError(ERROR_FONTS_FONT_FILEX_INVALID,
                "The FontFile is invalid"));
        return false;
    }

    // ---- Stream validation should be done by the StreamValidateHelper.
    // ---- Process font specific check
    COSStream stream = ff3.getStream();
    if (stream == null) {
        this.fontContainer.addError(new ValidationResult.ValidationError(ERROR_FONTS_FONT_FILEX_INVALID,
                "The FontFile is missing"));
        this.fontContainer.setFontProgramEmbedded(false);
        return false;
    }

    // ---- Lengthx aren't mandatory for this type of font
    // ---- But the Subtype is a mandatory field with specific values
    String st = stream.getNameAsString(COSName.getPDFName(DICTIONARY_KEY_SUBTYPE));
    if (!(FONT_DICTIONARY_VALUE_TYPE0C.equals(st) || FONT_DICTIONARY_VALUE_TYPE1C.equals(st))) {
        this.fontContainer.addError(new ValidationResult.ValidationError(ERROR_FONTS_FONT_FILEX_INVALID,
                "The FontFile3 stream doesn't have the right Subtype"));
        return false;
    }

    // ---- try to load the font using the java.awt.font object.
    // ---- if the font is invalid, an exception will be thrown
    try {
        CFFParser cffParser = new CFFParser();
        List<CFFFont> lCFonts = cffParser.parse(ff3.getByteArray());

        if (lCFonts == null || lCFonts.isEmpty()) {
            this.fontContainer.addError(new ValidationResult.ValidationError(ERROR_FONTS_CID_DAMAGED,
                    "The FontFile can't be read"));
            return false;
        }

        return checkCIDFontWidths(lCFonts) && checkFontFileMetaData(pfDescriptor, ff3);
    } catch (IOException e) {
        this.fontContainer.addError(
                new ValidationResult.ValidationError(ERROR_FONTS_CID_DAMAGED, "The FontFile can't be read"));
        return false;
    }
}

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

License:Apache License

public List<ValidationError> validateXObject(DocumentHandler handler, COSObject cObj)
        throws ValidationException {
    XObjectValidator xObjVal = null;/*from  w  w  w  . j  a va2  s .c om*/

    // ---- According to the XObject subtype, the validation isn't processed by
    // the same Validator
    COSStream dic = (COSStream) cObj.getObject();
    String subtype = dic.getNameAsString(COSName.getPDFName(DICTIONARY_KEY_SUBTYPE));

    if (XOBJECT_DICTIONARY_VALUE_SUBTYPE_IMG.equals(subtype)) {
        xObjVal = new XObjImageValidator(handler, dic);
    } else if (XOBJECT_DICTIONARY_VALUE_SUBTYPE_FORM.equals(subtype)) {
        xObjVal = new XObjFormValidator(handler, dic);
    } else if (XOBJECT_DICTIONARY_VALUE_SUBTYPE_POSTSCRIPT.equals(subtype)) {
        xObjVal = new XObjPostscriptValidator(handler, dic);
    } else {
        throw new ValidationException("Invalid XObject subtype");
    }

    return xObjVal.validate();
}

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./* w ww.jav a 2  s  .  c om*/
 * 
 * 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;
}