Java Code Examples for org.apache.pdfbox.cos.COSArray#getObject()

The following examples show how to use org.apache.pdfbox.cos.COSArray#getObject() . 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: PDActionGoTo.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * This will set the destination to jump to.
 *
 * @param d The destination.
 * 
 * @throws IllegalArgumentException if the destination is not a page dictionary object.
 */
public void setDestination( PDDestination d )
{
    if (d instanceof PDPageDestination)
    {
        PDPageDestination pageDest = (PDPageDestination) d;
        COSArray destArray = pageDest.getCOSObject();
        if (destArray.size() >= 1)
        {
            COSBase page = destArray.getObject(0);
            if (!(page instanceof COSDictionary))
            {
                throw new IllegalArgumentException("Destination of a GoTo action must be "
                        + "a page dictionary object");
            }
        }
    }
    getCOSObject().setItem(COSName.D, d);
}
 
Example 2
Source File: PDPage.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * This will return a list of the annotations for this page.
 *
 * @param annotationFilter the annotation filter provided allowing to filter out specific annotations
 * @return List of the PDAnnotation objects, never null. The returned list is backed by the
 * annotations COSArray, so any adding or deleting in this list will change the document too.
 * 
 * @throws IOException If there is an error while creating the annotation list.
 */
public List<PDAnnotation> getAnnotations(AnnotationFilter annotationFilter) throws IOException
{
    COSBase base = page.getDictionaryObject(COSName.ANNOTS);
    if (base instanceof COSArray)
    {
        COSArray annots = (COSArray) base;
        List<PDAnnotation> actuals = new ArrayList<PDAnnotation>();
        for (int i = 0; i < annots.size(); i++)
        {
            COSBase item = annots.getObject(i);
            if (item == null)
            {
                continue;
            }
            PDAnnotation createdAnnotation = PDAnnotation.createAnnotation(item);
            if (annotationFilter.accept(createdAnnotation))
            {
                actuals.add(createdAnnotation);
            }
        }
        return new COSArrayList<PDAnnotation>(actuals, annots);
    }
    return new COSArrayList<PDAnnotation>(page, COSName.ANNOTS);
}
 
Example 3
Source File: NPEfix18_eigthteen_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * This will return all of the documents root fields.
 *
 * A field might have children that are fields (non-terminal field) or does not
 * have children which are fields (terminal fields).
 *
 * The fields within an AcroForm are organized in a tree structure. The documents root fields
 * might either be terminal fields, non-terminal fields or a mixture of both. Non-terminal fields
 * mark branches which contents can be retrieved using {@link PDNonTerminalField#getChildren()}.
 *
 * @return A list of the documents root fields.
 *
 */
public List<PDField> getFields()
{
    COSArray cosFields = (COSArray) dictionary.getDictionaryObject(COSName.FIELDS);
    if (cosFields == null)
    {
        return Collections.emptyList();
    }
    List<PDField> pdFields = new ArrayList<PDField>();
    for (int i = 0; i < cosFields.size(); i++)
    {
        COSDictionary element = (COSDictionary) cosFields.getObject(i);
        if (element != null)
        {
            PDField field = PDField.fromDictionary(this, element, null);
            if (field != null)
            {
                pdFields.add(field);
            }
        }
    }
    return new COSArrayList<PDField>(pdFields, cosFields);
}
 
Example 4
Source File: Matrix.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Convenience method to be used when creating a matrix from unverified data. If the parameter
 * is a COSArray with at least six numbers, a Matrix object is created from the first six
 * numbers and returned. If not, then the identity Matrix is returned.
 *
 * @param base a COS object, preferably a COSArray with six numbers.
 *
 * @return a Matrix object.
 */
public static Matrix createMatrix(COSBase base)
{
    if (!(base instanceof COSArray))
    {
        return new Matrix();
    }
    COSArray array = (COSArray) base;
    if (array.size() < 6)
    {
        return new Matrix();
    }
    for (int i = 0; i < 6; ++i)
    {
        if (!(array.getObject(i) instanceof COSNumber))
        {
            return new Matrix();
        }
    }
    return new Matrix(array);
}
 
Example 5
Source File: PDNumberTreeNode.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * This will return a map of numbers.  The key will be a java.lang.Integer, the value will
 * depend on where this class is being used.
 *
 * @return A map of COS objects.
 *
 * @throws IOException If there is a problem creating the values.
 */
public Map<Integer,COSObjectable> getNumbers()  throws IOException
{
    Map<Integer, COSObjectable> indices = null;
    COSBase numBase = node.getDictionaryObject(COSName.NUMS);
    if (numBase instanceof COSArray)
    {
        COSArray numbersArray = (COSArray) numBase;
        indices = new HashMap<Integer, COSObjectable>();
        for (int i = 0; i < numbersArray.size(); i += 2)
        {
            COSBase base = numbersArray.getObject(i);
            if (!(base instanceof COSInteger))
            {
                LOG.error("page labels ignored, index " + i + " should be a number, but is " + base);
                return null;
            }
            COSInteger key = (COSInteger) base;
            COSBase cosValue = numbersArray.getObject(i + 1);
            indices.put(key.intValue(), cosValue == null ? null : convertCOSToPD(cosValue));
        }
        indices = Collections.unmodifiableMap(indices);
    }
    return indices;
}
 
Example 6
Source File: PDTerminalField.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Returns the widget annotations associated with this field.
 *
 * @return The list of widget annotations. Be aware that this list is <i>not</i> backed by the
 * actual widget collection of the field, so adding or deleting has no effect on the PDF
 * document until you call {@link #setWidgets(java.util.List) setWidgets()} with the modified
 * list.
 */
@Override
public List<PDAnnotationWidget> getWidgets()
{
    List<PDAnnotationWidget> widgets = new ArrayList<PDAnnotationWidget>();
    COSArray kids = (COSArray)getCOSObject().getDictionaryObject(COSName.KIDS);
    if (kids == null)
    {
        // the field itself is a widget
        widgets.add(new PDAnnotationWidget(getCOSObject()));
    }
    else if (kids.size() > 0)
    {
        // there are multiple widgets
        for (int i = 0; i < kids.size(); i++)
        {
            COSBase kid = kids.getObject(i);
            if (kid instanceof COSDictionary)
            {
                widgets.add(new PDAnnotationWidget((COSDictionary)kid));
            }
        }
    }
    return widgets;
}
 
Example 7
Source File: StandardSecurityHandler.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private byte[] getDocumentIDBytes(COSArray documentIDArray)
{
    //some documents may not have document id, see
    //test\encryption\encrypted_doc_no_id.pdf
    byte[] documentIDBytes;
    if( documentIDArray != null && documentIDArray.size() >= 1 )
    {
        COSString id = (COSString)documentIDArray.getObject( 0 );
        documentIDBytes = id.getBytes();
    }
    else
    {
        documentIDBytes = new byte[0];
    }
    return documentIDBytes;
}
 
Example 8
Source File: FDFField.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * This will return a list of options for a choice field. The value in the list will be 1 of 2 types.
 * java.lang.String or FDFOptionElement.
 *
 * @return A list of all options.
 */
public List<Object> getOptions()
{
    List<Object> retval = null;
    COSArray array = (COSArray) field.getDictionaryObject(COSName.OPT);
    if (array != null)
    {
        List<Object> objects = new ArrayList<Object>();
        for (int i = 0; i < array.size(); i++)
        {
            COSBase next = array.getObject(i);
            if (next instanceof COSString)
            {
                objects.add(((COSString) next).getString());
            }
            else
            {
                COSArray value = (COSArray) next;
                objects.add(new FDFOptionElement(value));
            }
        }
        retval = new COSArrayList<Object>(objects, array);
    }
    return retval;
}
 
Example 9
Source File: PDActionEmbeddedGoTo.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * This will set the destination to jump to.
 *
 * @param d The destination.
 *
 * @throws IllegalArgumentException if the destination is not a page dictionary object.
 */
public void setDestination(PDDestination d)
{
    if (d instanceof PDPageDestination)
    {
        PDPageDestination pageDest = (PDPageDestination) d;
        COSArray destArray = pageDest.getCOSObject();
        if (destArray.size() >= 1)
        {
            COSBase page = destArray.getObject(0);
            if (!(page instanceof COSDictionary))
            {
                throw new IllegalArgumentException("Destination of a GoToE action must be "
                        + "a page dictionary object");
            }
        }
    }
    getCOSObject().setItem(COSName.D, d);
}
 
Example 10
Source File: PDPageTree.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Insert a page before another page within a page tree.
 *
 * @param newPage the page to be inserted.
 * @param nextPage the page that is to be after the new page.
 * @throws IllegalArgumentException if one attempts to insert a page that isn't part of a page
 * tree.
 */
public void insertBefore(PDPage newPage, PDPage nextPage)
{
    COSDictionary nextPageDict = nextPage.getCOSObject();
    COSDictionary parentDict = (COSDictionary) nextPageDict.getDictionaryObject(COSName.PARENT);
    COSArray kids = (COSArray) parentDict.getDictionaryObject(COSName.KIDS);
    boolean found = false;
    for (int i = 0; i < kids.size(); ++i)
    {
        COSDictionary pageDict = (COSDictionary) kids.getObject(i);
        if (pageDict.equals(nextPage.getCOSObject()))
        {
            kids.add(i, newPage.getCOSObject());
            newPage.getCOSObject().setItem(COSName.PARENT, parentDict);
            found = true;
            break;
        }
    }
    if (!found)
    {
        throw new IllegalArgumentException("attempted to insert before orphan page");
    }
    increaseParents(parentDict);
}
 
Example 11
Source File: PDField.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * This will find one of the child elements. The name array are the components of the name to search down the tree
 * of names. The nameIndex is where to start in that array. This method is called recursively until it finds the end
 * point based on the name array.
 * 
 * @param name An array that picks the path to the field.
 * @param nameIndex The index into the array.
 * @return The field at the endpoint or null if none is found.
 */
PDField findKid(String[] name, int nameIndex)
{
    PDField retval = null;
    COSArray kids = (COSArray) dictionary.getDictionaryObject(COSName.KIDS);
    if (kids != null)
    {
        for (int i = 0; retval == null && i < kids.size(); i++)
        {
            COSDictionary kidDictionary = (COSDictionary) kids.getObject(i);
            if (name[nameIndex].equals(kidDictionary.getString(COSName.T)))
            {
                retval = PDField.fromDictionary(acroForm, kidDictionary,
                                                (PDNonTerminalField)this);
                if (retval != null && name.length > nameIndex + 1)
                {
                    retval = retval.findKid(name, nameIndex + 1);
                }
            }
        }
    }
    return retval;
}
 
Example 12
Source File: NPEfix17_seventeen_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * This will return all of the documents root fields.
 *
 * A field might have children that are fields (non-terminal field) or does not
 * have children which are fields (terminal fields).
 *
 * The fields within an AcroForm are organized in a tree structure. The documents root fields
 * might either be terminal fields, non-terminal fields or a mixture of both. Non-terminal fields
 * mark branches which contents can be retrieved using {@link PDNonTerminalField#getChildren()}.
 *
 * @return A list of the documents root fields.
 *
 */
public List<PDField> getFields()
{
    COSArray cosFields = (COSArray) dictionary.getDictionaryObject(COSName.FIELDS);
    if (cosFields == null)
    {
        return Collections.emptyList();
    }
    List<PDField> pdFields = new ArrayList<PDField>();
    for (int i = 0; i < cosFields.size(); i++)
    {
        COSDictionary element = (COSDictionary) cosFields.getObject(i);
        if (element != null)
        {
            PDField field = PDField.fromDictionary(this, element, null);
            if (field != null)
            {
                pdFields.add(field);
            }
        }
    }
    return new COSArrayList<PDField>(pdFields, cosFields);
}
 
Example 13
Source File: DictionaryEncoding.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private void applyDifferences()
{
    // now replace with the differences
    COSBase base = encoding.getDictionaryObject(COSName.DIFFERENCES);
    if (!(base instanceof COSArray))
    {
        return;
    }
    COSArray diffArray = (COSArray) base;
    int currentIndex = -1;
    for (int i = 0; i < diffArray.size(); i++)
    {
        COSBase next = diffArray.getObject(i);
        if( next instanceof COSNumber)
        {
            currentIndex = ((COSNumber)next).intValue();
        }
        else if( next instanceof COSName )
        {
            COSName name = (COSName)next;
            overwrite(currentIndex, name.getName());
            this.differences.put(currentIndex, name.getName());
            currentIndex++;
        }
    }
}
 
Example 14
Source File: PDPolylineAppearanceHandler.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Get the line with of the border.
 * 
 * Get the width of the line used to draw a border around the annotation.
 * This may either be specified by the annotation dictionaries Border
 * setting or by the W entry in the BS border style dictionary. If both are
 * missing the default width is 1.
 * 
 * @return the line width
 */
// TODO: according to the PDF spec the use of the BS entry is annotation
// specific
// so we will leave that to be implemented by individual handlers.
// If at the end all annotations support the BS entry this can be handled
// here and removed from the individual handlers.
float getLineWidth()
{
    PDAnnotationMarkup annotation = (PDAnnotationMarkup) getAnnotation();

    PDBorderStyleDictionary bs = annotation.getBorderStyle();

    if (bs != null)
    {
        return bs.getWidth();
    }

    COSArray borderCharacteristics = annotation.getBorder();
    if (borderCharacteristics.size() >= 3)
    {
        COSBase base = borderCharacteristics.getObject(2);
        if (base instanceof COSNumber)
        {
            return ((COSNumber) base).floatValue();
        }
    }

    return 1;
}
 
Example 15
Source File: PDPageTree.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Helper to get kids from malformed PDFs.
 * @param node page tree node
 * @return list of kids
 */
private List<COSDictionary> getKids(COSDictionary node)
{
    List<COSDictionary> result = new ArrayList<COSDictionary>();

    COSArray kids = node.getCOSArray(COSName.KIDS);
    if (kids == null)
    {
        // probably a malformed PDF
        return result;
    }

    for (int i = 0, size = kids.size(); i < size; i++)
    {
        COSBase base = kids.getObject(i);
        if (base instanceof COSDictionary)
        {
            result.add((COSDictionary) base);
        }
        else
        {
            LOG.warn("COSDictionary expected, but got " +
                    (base == null ? "null" : base.getClass().getSimpleName()));
        }
    }

    return result;
}
 
Example 16
Source File: FDFDictionary.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * This will write this element as an XML document.
 *
 * @param output The stream to write the xml to.
 *
 * @throws IOException If there is an error writing the XML.
 */
public void writeXML(Writer output) throws IOException
{
    PDFileSpecification fs = this.getFile();
    if (fs != null)
    {
        output.write("<f href=\"" + fs.getFile() + "\" />\n");
    }
    COSArray ids = this.getID();
    if (ids != null)
    {
        COSString original = (COSString) ids.getObject(0);
        COSString modified = (COSString) ids.getObject(1);
        output.write("<ids original=\"" + original.toHexString() + "\" ");
        output.write("modified=\"" + modified.toHexString() + "\" />\n");
    }
    List<FDFField> fields = getFields();
    if (fields != null && fields.size() > 0)
    {
        output.write("<fields>\n");
        for (FDFField field : fields)
        {
            field.writeXML(output);
        }
        output.write("</fields>\n");
    }
}
 
Example 17
Source File: PDCircleAppearanceHandler.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Get the line with of the border.
 * 
 * Get the width of the line used to draw a border around the annotation.
 * This may either be specified by the annotation dictionaries Border
 * setting or by the W entry in the BS border style dictionary. If both are
 * missing the default width is 1.
 * 
 * @return the line width
 */
// TODO: according to the PDF spec the use of the BS entry is annotation
// specific
// so we will leave that to be implemented by individual handlers.
// If at the end all annotations support the BS entry this can be handled
// here and removed from the individual handlers.
float getLineWidth()
{
    PDAnnotationMarkup annotation = (PDAnnotationMarkup) getAnnotation();

    PDBorderStyleDictionary bs = annotation.getBorderStyle();

    if (bs != null)
    {
        return bs.getWidth();
    }

    COSArray borderCharacteristics = annotation.getBorder();
    if (borderCharacteristics.size() >= 3)
    {
        COSBase base = borderCharacteristics.getObject(2);
        if (base instanceof COSNumber)
        {
            return ((COSNumber) base).floatValue();
        }
    }

    return 1;
}
 
Example 18
Source File: NPEfix16_sixteen_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Constructor.
 *
 * @param iccArray The ICC stream object.
 */
public PDICCBased( COSArray iccArray )
{
    array = iccArray;
    stream = new PDStream( (COSStream)iccArray.getObject( 1 ) );
}
 
Example 19
Source File: NPEfix16_sixteen_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Constructor.
 *
 * @param iccArray The ICC stream object.
 */
public PDICCBased( COSArray iccArray )
{
    array = iccArray;
    stream = new PDStream( (COSStream)iccArray.getObject( 1 ) );
}
 
Example 20
Source File: NPEfix17_seventeen_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * This will get a field by name, possibly using the cache if setCache is true.
 *
 * @param fullyQualifiedName The name of the field to get.
 * @return The field with that name of null if one was not found.
 * @throws IOException If there is an error getting the field type.
 */
public PDField getField(String fullyQualifiedName)
{
    PDField retval = null;
    if (fieldCache != null)
    {
        retval = fieldCache.get(fullyQualifiedName);
    }
    else
    {
        String[] nameSubSection = fullyQualifiedName.split("\\.");
        COSArray fields = (COSArray) dictionary.getDictionaryObject(COSName.FIELDS);

        for (int i = 0; i < fields.size() && retval == null; i++)
        {
            COSDictionary element = (COSDictionary) fields.getObject(i);
            if (element != null)
            {
                COSString fieldName =
                        (COSString)element.getDictionaryObject(COSName.T);
                if (fieldName.getString().equals(fullyQualifiedName) ||
                        fieldName.getString().equals(nameSubSection[0]))
                {
                    PDField root = PDField.fromDictionary(this, element, null);
                    if (root != null)
                    {
                        if (nameSubSection.length > 1)
                        {
                            PDField kid = root.findKid(nameSubSection, 1);
                            if (kid != null)
                            {
                                retval = kid;
                            }
                            else
                            {
                                retval = root;
                            }
                        }
                        else
                        {
                            retval = root;
                        }
                    }
                }
            }
        }
    }
    return retval;
}