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

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

Introduction

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

Prototype

public COSBase getDictionaryObject(String key) 

Source Link

Document

This will get an object from this dictionary.

Usage

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

License:Apache License

/**
 * visitFromStream method comment.//from  www.jav  a2  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.contentstream.ContentStreamWrapper.java

License:Apache License

/**
 * Process the validation of a Tiling Pattern
 * //  w  w w. ja v a2 s  .  c  om
 * @param pattern
 * @return A list of validation error. This list is empty if the validation
 *         succeed.
 * @throws ValidationException
 */
public List<ValidationError> validPatternContentStream(COSStream pattern) throws ValidationException {
    List<ValidationError> errors = new ArrayList<ValidationError>();

    try {
        COSDictionary res = (COSDictionary) pattern.getDictionaryObject(DICTIONARY_KEY_RESOURCES);
        resetEnginContext();
        processSubStream(null, new PDResources(res), pattern);
    } catch (ContentStreamException e) {
        errors.add(new ValidationError(e.getValidationError(), e.getMessage()));
    } catch (IOException e) {
        throw new ValidationException("Unable to check the ContentStream : " + e.getMessage(), e);
    }

    return errors;
}

From source file:org.apache.fop.render.pdf.pdfbox.PDFBoxAdapter.java

License:Apache License

private void mergeXObj(COSDictionary sourcePageResources, FontInfo fontinfo, UniqueName uniqueName)
        throws IOException {
    COSDictionary xobj = (COSDictionary) sourcePageResources.getDictionaryObject(COSName.XOBJECT);
    if (xobj != null && pdfDoc.isMergeFontsEnabled()) {
        for (Map.Entry<COSName, COSBase> i : xobj.entrySet()) {
            COSObject v = (COSObject) i.getValue();
            COSStream stream = (COSStream) v.getObject();
            COSDictionary res = (COSDictionary) stream.getDictionaryObject(COSName.RESOURCES);
            if (res != null) {
                COSDictionary src = (COSDictionary) res.getDictionaryObject(COSName.FONT);
                if (src != null) {
                    COSDictionary target = (COSDictionary) sourcePageResources
                            .getDictionaryObject(COSName.FONT);
                    if (target == null) {
                        sourcePageResources.setItem(COSName.FONT, src);
                    } else {
                        for (Map.Entry<COSName, COSBase> entry : src.entrySet()) {
                            if (!target.keySet().contains(entry.getKey())) {
                                target.setItem(uniqueName.getName(entry.getKey()), entry.getValue());
                            }//from w w  w. ja  va 2s  .  c o m
                        }
                    }
                    PDFWriter writer = new MergeFontsPDFWriter(src, fontinfo, uniqueName, parentFonts, 0);
                    String c = writer.writeText(new PDStream(stream));
                    if (c != null) {
                        stream.removeItem(COSName.FILTER);
                        newXObj.put(i.getKey(), c);
                        for (Object e : src.keySet().toArray()) {
                            COSName name = (COSName) e;
                            src.setItem(uniqueName.getName(name), src.getItem(name));
                            src.removeItem(name);
                        }
                    }
                }
            }
        }
    }
}