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

The following examples show how to use org.apache.pdfbox.cos.COSArray#size() . 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: PDRectlinearMeasureDictionary.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * This will return the changes along the y-axis.
 * 
 * @return changes along the y-axis
 */
public PDNumberFormatDictionary[] getChangeYs()
{
    COSArray y = (COSArray)this.getCOSObject().getDictionaryObject("Y");
    if (y != null)
    {
        PDNumberFormatDictionary[] retval =
            new PDNumberFormatDictionary[y.size()];
        for (int i = 0; i < y.size(); i++)
        {
            COSDictionary dic = (COSDictionary) y.get(i);
            retval[i] = new PDNumberFormatDictionary(dic);
        }
        return retval;
    }
    return null;
}
 
Example 2
Source File: COSArrayList.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * This will take an array of COSNumbers and return a COSArrayList of
 * java.lang.Float values.
 *
 * @param floatArray The existing float Array.
 *
 * @return The list of Float objects.
 */
public static List<Float> convertFloatCOSArrayToList( COSArray floatArray )
{
    List<Float> retval = null;
    if( floatArray != null )
    {
        List<Float> numbers = new ArrayList<Float>(floatArray.size());
        for( int i=0; i<floatArray.size(); i++ )
        {
            COSBase base = floatArray.getObject(i);
            if (base instanceof COSNumber)
            {
                numbers.add(((COSNumber) base).floatValue());
            }
            else
            {
                numbers.add(null);
            }
        }
        retval = new COSArrayList<Float>( numbers, floatArray );
    }
    return retval;
}
 
Example 3
Source File: FDFDictionary.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * This will get the list of FDF Annotations. This will return a list of FDFAnnotation objects or null if the entry
 * is not set.
 *
 * @return A list of FDF annotations.
 *
 * @throws IOException If there is an error creating the annotation list.
 */
public List<FDFAnnotation> getAnnotations() throws IOException
{
    List<FDFAnnotation> retval = null;
    COSArray annotArray = (COSArray) fdf.getDictionaryObject(COSName.ANNOTS);
    if (annotArray != null)
    {
        List<FDFAnnotation> annots = new ArrayList<FDFAnnotation>();
        for (int i = 0; i < annotArray.size(); i++)
        {
            annots.add(FDFAnnotation.create((COSDictionary) annotArray.getObject(i)));
        }
        retval = new COSArrayList<FDFAnnotation>(annots, annotArray);
    }
    return retval;
}
 
Example 4
Source File: PDAnnotationMarkup.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * PDF 2.0: This will retrieve the arrays that shall represent the alternating horizontal
 * and vertical coordinates for path building.
 *
 * @return An array of float arrays, each supplying the operands for a path building operator
 * (m, l or c). The first array should have 2 elements, the others should have 2 or 6 elements.
 */
public float[][] getPath()
{
    COSBase base = getCOSObject().getDictionaryObject(COSName.PATH);
    if (base instanceof COSArray)
    {
        COSArray array = (COSArray) base;
        float[][] pathArray = new float[array.size()][];
        for (int i = 0; i < array.size(); ++i)
        {
            COSBase base2 = array.getObject(i);
            if (base2 instanceof COSArray)
            {
                pathArray[i] = ((COSArray) array.getObject(i)).toFloatArray();
            }
            else
            {
                pathArray[i] = new float[0];
            }
        }
        return pathArray;
    }
    return null;
}
 
Example 5
Source File: FDFDictionary.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * This will get the list of FDF Pages. This will return a list of FDFPage objects.
 *
 * @return A list of FDF pages.
 */
public List<FDFPage> getPages()
{
    List<FDFPage> retval = null;
    COSArray pageArray = (COSArray) fdf.getDictionaryObject(COSName.PAGES);
    if (pageArray != null)
    {
        List<FDFPage> pages = new ArrayList<FDFPage>();
        for (int i = 0; i < pageArray.size(); i++)
        {
            pages.add(new FDFPage((COSDictionary) pageArray.get(i)));
        }
        retval = new COSArrayList<FDFPage>(pages, pageArray);
    }
    return retval;
}
 
Example 6
Source File: PDPage.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Get the viewports.
 *
 * @return a list of viewports or null if there is no /VP entry.
 */
public List<PDViewportDictionary> getViewports()
{
    COSBase base = page.getDictionaryObject(COSName.VP);
    if (!(base instanceof COSArray))
    {
        return null;
    }
    COSArray array = (COSArray) base;
    List<PDViewportDictionary> viewports = new ArrayList<PDViewportDictionary>();
    for (int i = 0; i < array.size(); ++i)
    {
        COSBase base2 = array.getObject(i);
        if (base2 instanceof COSDictionary)
        {
            viewports.add(new PDViewportDictionary((COSDictionary) base2));
        }
        else
        {
            LOG.warn("Array element " + base2 + " is skipped, must be a (viewport) dictionary");
        }
    }
    return viewports;
}
 
Example 7
Source File: PDPageTree.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Insert a page after another page within a page tree.
 *
 * @param newPage the page to be inserted.
 * @param prevPage the page that is to be before the new page.
 * @throws IllegalArgumentException if one attempts to insert a page that isn't part of a page
 * tree.
 */
public void insertAfter(PDPage newPage, PDPage prevPage)
{
    COSDictionary prevPageDict = prevPage.getCOSObject();
    COSDictionary parentDict = (COSDictionary) prevPageDict.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(prevPage.getCOSObject()))
        {
            kids.add(i + 1, 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 8
Source File: PDRectlinearMeasureDictionary.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * This will return the changes along the x-axis.
 * 
 * @return changes along the x-axis
 */
public PDNumberFormatDictionary[] getChangeXs()
{
    COSArray x = (COSArray)this.getCOSObject().getDictionaryObject("X");
    if (x != null)
    {
        PDNumberFormatDictionary[] retval =
            new PDNumberFormatDictionary[x.size()];
        for (int i = 0; i < x.size(); i++)
        {
            COSDictionary dic = (COSDictionary) x.get(i);
            retval[i] = new PDNumberFormatDictionary(dic);
        }
        return retval;
    }
    return null;
}
 
Example 9
Source File: PDPage.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Returns the content streams which make up this page.
 * 
 * @return content stream iterator
 */
public Iterator<PDStream> getContentStreams()
{
    List<PDStream> streams = new ArrayList<PDStream>();
    COSBase base = page.getDictionaryObject(COSName.CONTENTS);
    if (base instanceof COSStream)
    {
        streams.add(new PDStream((COSStream) base));
    }
    else if (base instanceof COSArray && ((COSArray) base).size() > 0)
    {
        COSArray array = (COSArray)base;
        for (int i = 0; i < array.size(); i++)
        {
            COSStream stream = (COSStream) array.getObject(i);
            streams.add(new PDStream(stream));
        }
    }
    return streams.iterator();
}
 
Example 10
Source File: PDTriangleBasedShadingType.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Get the decode for the input parameter.
 *
 * @param paramNum the function parameter number
 * @return the decode parameter range or null if none is set
 */
public PDRange getDecodeForParameter(int paramNum)
{
    PDRange retval = null;
    COSArray decodeValues = getDecodeValues();
    if (decodeValues != null && decodeValues.size() >= paramNum * 2 + 1)
    {
        retval = new PDRange(decodeValues, paramNum);
    }
    return retval;
}
 
Example 11
Source File: PDLinkAppearanceHandler.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()
{
    PDAnnotationLink annotation = (PDAnnotationLink) 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 12
Source File: BlendMode.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Determines the blend mode from the BM entry in the COS ExtGState.
 *
 * @param cosBlendMode name or array
 * @return blending mode
 */
public static BlendMode getInstance(COSBase cosBlendMode)
{
    BlendMode result = null;
    if (cosBlendMode instanceof COSName)
    {
        result = BLEND_MODES.get(cosBlendMode);
    }
    else if (cosBlendMode instanceof COSArray)
    {
        COSArray cosBlendModeArray = (COSArray) cosBlendMode;
        for (int i = 0; i < cosBlendModeArray.size(); i++)
        {
            result = BLEND_MODES.get(cosBlendModeArray.getObject(i));
            if (result != null)
            {
                break;
            }
        }
    }

    if (result != null)
    {
        return result;
    }
    return BlendMode.NORMAL;
}
 
Example 13
Source File: Filter.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
protected COSDictionary getDecodeParams(COSDictionary dictionary, int index)
{
    COSBase filter = dictionary.getDictionaryObject(COSName.FILTER, COSName.F);
    COSBase obj = dictionary.getDictionaryObject(COSName.DECODE_PARMS, COSName.DP);
    if (filter instanceof COSName && obj instanceof COSDictionary)
    {
        // PDFBOX-3932: The PDF specification requires "If there is only one filter and that 
        // filter has parameters, DecodeParms shall be set to the filter’s parameter dictionary" 
        // but tests show that Adobe means "one filter name object".
        return (COSDictionary)obj;
    }
    else if (filter instanceof COSArray && obj instanceof COSArray)
    {
        COSArray array = (COSArray)obj;
        if (index < array.size())
        {
            COSBase objAtIndex = array.getObject(index);
            if (objAtIndex instanceof COSDictionary)
            {
                return (COSDictionary)array.getObject(index);
            }
        }
    }
    else if (obj != null && !(filter instanceof COSArray || obj instanceof COSArray))
    {
        LOG.error("Expected DecodeParams to be an Array or Dictionary but found " +
                  obj.getClass().getName());
    }
    return new COSDictionary();
}
 
Example 14
Source File: PDOptionalContentProperties.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Lists all optional content group names.
 * @return an array of all names
 */
public String[] getGroupNames()
{
    COSArray ocgs = (COSArray)dict.getDictionaryObject(COSName.OCGS);
    int size = ocgs.size();
    String[] groups = new String[size];
    for (int i = 0; i < size; i++)
    {
        COSBase obj = ocgs.get(i);
        COSDictionary ocg = toDictionary(obj);
        groups[i] = ocg.getString(COSName.NAME);
    }
    return groups;
}
 
Example 15
Source File: COSArrayList.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * This will take an array of COSName and return a COSArrayList of
 * java.lang.String values.
 *
 * @param nameArray The existing name Array.
 *
 * @return The list of String objects.
 */
public static List<String> convertCOSNameCOSArrayToList( COSArray nameArray )
{
    List<String> retval = null;
    if( nameArray != null )
    {
        List<String>names = new ArrayList<String>();
        for( int i=0; i<nameArray.size(); i++ )
        {
            names.add( ((COSName)nameArray.getObject( i )).getName() );
        }
        retval = new COSArrayList<String>( names, nameArray );
    }
    return retval;
}
 
Example 16
Source File: PDPolygonAppearanceHandler.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 17
Source File: PDFieldFactory.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Creates a COSField subclass from the given field.
 *
 * @param form the form that the field is part of
 * @param field the dictionary representing a field element
 * @param parent the parent node of the node to be created 
 * @return the corresponding PDField instance
 */
static PDField createField(PDAcroForm form, COSDictionary field, PDNonTerminalField parent)
{
    String fieldType = findFieldType(field);
    
    // Test if we have a non terminal field first as it might have
    // properties which do apply to other fields
    // A non terminal fields has Kids entries which do have
    // a field name (other than annotations)
    if (field.containsKey(COSName.KIDS))
    {
        COSArray kids = (COSArray) field.getDictionaryObject(COSName.KIDS);
        if (kids != null && kids.size() > 0)
        {
            for (int i = 0; i < kids.size(); i++)
            {
                COSBase kid = kids.getObject(i);
                if (kid instanceof COSDictionary && ((COSDictionary) kid).getString(COSName.T) != null)
                {
                    return new PDNonTerminalField(form, field, parent);
                }
            }
        }
    } 
    
    if (FIELD_TYPE_CHOICE.equals(fieldType))
    {
        return createChoiceSubType(form, field, parent);
    }
    else if (FIELD_TYPE_TEXT.equals(fieldType))
    {
        return new PDTextField(form, field, parent);
    }
    else if (FIELD_TYPE_SIGNATURE.equals(fieldType))
    {
        return new PDSignatureField(form, field, parent);
    }
    else if (FIELD_TYPE_BUTTON.equals(fieldType))
    {
        return createButtonSubType(form, field, parent);
    }
    else
    {
        // an erroneous non-field object, see PDFBOX-2885
        return null;
    }
}
 
Example 18
Source File: PDStructureNode.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Removes a COS base kid.
 * 
 * @param object the COS base
 * @return <code>true</code> if the kid was removed, <code>false</code> otherwise
 */
protected boolean removeKid(COSBase object)
{
    if (object == null)
    {
        return false;
    }
    COSBase k = this.getCOSObject().getDictionaryObject(COSName.K);
    if (k == null)
    {
        // no kids: objectable is not a kid
        return false;
    }
    else if (k instanceof COSArray)
    {
        // currently more than one kid: remove kid from existing array
        COSArray array = (COSArray) k;
        boolean removed = array.removeObject(object);
        // if now only one kid: set remaining kid as kids
        if (array.size() == 1)
        {
            this.getCOSObject().setItem(COSName.K, array.getObject(0));
        }
        return removed;
    }
    else
    {
        // currently one kid: if current kid equals given object, remove kids entry
        boolean onlyKid = k.equals(object);
        if (!onlyKid && (k instanceof COSObject))
        {
            COSBase kObj = ((COSObject) k).getObject();
            onlyKid = kObj.equals(object);
        }
        if (onlyKid)
        {
            this.getCOSObject().setItem(COSName.K, null);
            return true;
        }
        return false;
    }
}
 
Example 19
Source File: SampledImageReader.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
private static float[] getDecodeArray(PDImage pdImage) throws IOException
{
    final COSArray cosDecode = pdImage.getDecode();
    float[] decode = null;

    if (cosDecode != null)
    {
        int numberOfComponents = pdImage.getColorSpace().getNumberOfComponents();
        if (cosDecode.size() != numberOfComponents * 2)
        {
            if (pdImage.isStencil() && cosDecode.size() >= 2
                    && cosDecode.get(0) instanceof COSNumber
                    && cosDecode.get(1) instanceof COSNumber)
            {
                float decode0 = ((COSNumber) cosDecode.get(0)).floatValue();
                float decode1 = ((COSNumber) cosDecode.get(1)).floatValue();
                if (decode0 >= 0 && decode0 <= 1 && decode1 >= 0 && decode1 <= 1)
                {
                    LOG.warn("decode array " + cosDecode
                            + " not compatible with color space, using the first two entries");
                    return new float[]
                    {
                        decode0, decode1
                    };
                }
            }
            LOG.error("decode array " + cosDecode
                    + " not compatible with color space, using default");
        }
        else
        {
            decode = cosDecode.toFloatArray();
        }
    }

    // use color space default
    if (decode == null)
    {
        return pdImage.getColorSpace().getDefaultDecode(pdImage.getBitsPerComponent());
    }

    return decode;
}
 
Example 20
Source File: SecurityHandler.java    From gcs with Mozilla Public License 2.0 3 votes vote down vote up
/**
 * This will decrypt an array.
 *
 * @param array The array to decrypt.
 * @param objNum The object number.
 * @param genNum The object generation number.
 *
 * @throws IOException If there is an error accessing the data.
 */
private void decryptArray(COSArray array, long objNum, long genNum) throws IOException
{
    for (int i = 0; i < array.size(); i++)
    {
        decrypt(array.get(i), objNum, genNum);
    }
}