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

The following examples show how to use org.apache.pdfbox.cos.COSArray#get() . 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: PDInlineImage.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private PDColorSpace createColorSpace(COSBase cs) throws IOException
{
    if (cs instanceof COSName)
    {
        return PDColorSpace.create(toLongName(cs), resources);
    }

    if (cs instanceof COSArray && ((COSArray) cs).size() > 1)
    {
        COSArray srcArray = (COSArray) cs;
        COSBase csType = srcArray.get(0);
        if (COSName.I.equals(csType) || COSName.INDEXED.equals(csType))
        {
            COSArray dstArray = new COSArray();
            dstArray.addAll(srcArray);
            dstArray.set(0, COSName.INDEXED);
            dstArray.set(1, toLongName(srcArray.get(1)));
            return PDColorSpace.create(dstArray, resources);
        }

        throw new IOException("Illegal type of inline image color space: " + csType);
    }

    throw new IOException("Illegal type of object for inline image color space: " + cs);
}
 
Example 2
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 3
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 4
Source File: PDRectlinearMeasureDictionary.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * This will return the distances.
 * 
 * @return distances
 */
public PDNumberFormatDictionary[] getDistances()
{
    COSArray d = (COSArray)this.getCOSObject().getDictionaryObject("D");
    if (d != null)
    {
        PDNumberFormatDictionary[] retval =
            new PDNumberFormatDictionary[d.size()];
        for (int i = 0; i < d.size(); i++)
        {
            COSDictionary dic = (COSDictionary) d.get(i);
            retval[i] = new PDNumberFormatDictionary(dic);
        }
        return retval;
    }
    return null;
}
 
Example 5
Source File: PDRectlinearMeasureDictionary.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * This will return the areas.
 * 
 * @return areas
 */
public PDNumberFormatDictionary[] getAreas()
{
    COSArray a = (COSArray)this.getCOSObject().getDictionaryObject(COSName.A);
    if (a != null)
    {
        PDNumberFormatDictionary[] retval =
            new PDNumberFormatDictionary[a.size()];
        for (int i = 0; i < a.size(); i++)
        {
            COSDictionary dic = (COSDictionary) a.get(i);
            retval[i] = new PDNumberFormatDictionary(dic);
        }
        return retval;
    }
    return null;
}
 
Example 6
Source File: PDRectlinearMeasureDictionary.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * This will return the angles.
 * 
 * @return angles
 */
public PDNumberFormatDictionary[] getAngles()
{
    COSArray t = (COSArray)this.getCOSObject().getDictionaryObject("T");
    if (t != null)
    {
        PDNumberFormatDictionary[] retval =
            new PDNumberFormatDictionary[t.size()];
        for (int i = 0; i < t.size(); i++)
        {
            COSDictionary dic = (COSDictionary) t.get(i);
            retval[i] = new PDNumberFormatDictionary(dic);
        }
        return retval;
    }
    return null;
}
 
Example 7
Source File: PDRectlinearMeasureDictionary.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * This will return the sloaps of a line.
 * 
 * @return the sloaps of a line
 */
public PDNumberFormatDictionary[] getLineSloaps()
{
    COSArray s = (COSArray)this.getCOSObject().getDictionaryObject("S");
    if (s != null)
    {
        PDNumberFormatDictionary[] retval =
            new PDNumberFormatDictionary[s.size()];
        for (int i = 0; i < s.size(); i++)
        {
            COSDictionary dic = (COSDictionary) s.get(i);
            retval[i] = new PDNumberFormatDictionary(dic);
        }
        return retval;
    }
    return null;
}
 
Example 8
Source File: PDColor.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Creates a PDColor containing the given color value.
 * @param array a COS array containing the color value
 * @param colorSpace color space in which the color value is defined
 */
public PDColor(COSArray array, PDColorSpace colorSpace)
{
    if (array.size() > 0 && array.get(array.size() - 1) instanceof COSName)
    {
        // color components (optional), for the color of an uncoloured tiling pattern
        components = new float[array.size() - 1];
        for (int i = 0; i < array.size() - 1; i++)
        {
            components[i] = ((COSNumber)array.get(i)).floatValue();
        }

        // pattern name (required)
        patternName = (COSName)array.get(array.size() - 1);
    }
    else
    {
        // color components only
        components = new float[array.size()];
        for (int i = 0; i < array.size(); i++)
        {
            components[i] = ((COSNumber)array.get(i)).floatValue();
        }
        patternName = null;
    }
    this.colorSpace = colorSpace;
}
 
Example 9
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 10
Source File: PDNumberTreeNode.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Get the highest value for a key in the number map.
 *
 * @return The highest value for a key in the map.
 */
public Integer getUpperLimit()
{
    Integer retval = null;
    COSArray arr = (COSArray)node.getDictionaryObject( COSName.LIMITS );
    if( arr != null && arr.get(0) != null )
    {
        retval = arr.getInt( 1 );
    }
    return retval;
}
 
Example 11
Source File: PDNumberTreeNode.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Get the lowest value for a key in the number map.
 *
 * @return The lowest value for a key in the map.
 */
public Integer getLowerLimit()
{
    Integer retval = null;
    COSArray arr = (COSArray)node.getDictionaryObject( COSName.LIMITS );
    if( arr != null && arr.get(0) != null )
    {
        retval = arr.getInt( 0 );
    }
    return retval;
}
 
Example 12
Source File: FieldUtils.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Return either one of a list which can have two-element arrays entries.
 * <p>
 * Some entries in a dictionary can either be an array of elements
 * or an array of two-element arrays. This method will either return
 * the elements in the array or in case of two-element arrays, the element
 * designated by the pair index
 * </p>
 * <p>
 * An {@link IllegalArgumentException} will be thrown if the items contain
 * two-element arrays and the index is not 0 or 1.
 * </p>
 * 
 * @param items the array of elements or two-element arrays
 * @param pairIdx the index into the two-element array
 * @return a List of single elements
 */
static List<String> getPairableItems(COSBase items, int pairIdx)
{
    if (pairIdx < 0 || pairIdx > 1) 
    {
        throw new IllegalArgumentException("Only 0 and 1 are allowed as an index into two-element arrays");
    }
    
    if (items instanceof COSString)
    {
        List<String> array = new ArrayList<String>();
        array.add(((COSString) items).getString());
        return array;
    }
    else if (items instanceof COSArray)
    {
        List<String> entryList = new ArrayList<String>(); 
        for (COSBase entry : (COSArray) items)
        {
            if (entry instanceof COSString)
            {
                entryList.add(((COSString) entry).getString());
            }
            else if (entry instanceof COSArray)
            {
                COSArray cosArray = (COSArray) entry;
                if (cosArray.size() >= pairIdx +1 && cosArray.get(pairIdx) instanceof COSString)
                {
                    entryList.add(((COSString) cosArray.get(pairIdx)).getString());
                }
            }
        }
        return entryList;     
    }
    return Collections.emptyList();
}
 
Example 13
Source File: ShowTextGlyph.java    From pdfxtk with Apache License 2.0 5 votes vote down vote up
/**
     * TJ Show text, allowing individual glyph positioning.
     * @param operator The operator that is being executed.
     * @param arguments List
     * @throws IOException If there is an error processing this operator.
     */
    public void process(PDFOperator operator, List arguments) throws IOException 
    {
    	((PDFObjectExtractor)context).setNewTextFragment(true);
        COSArray array = (COSArray)arguments.get( 0 );
        float adjustment=0;
        for( int i=0; i<array.size(); i++ )
        {
            COSBase next = array.get( i );
            if( next instanceof COSNumber )
            {
                adjustment = ((COSNumber)next).floatValue();

                Matrix adjMatrix = new Matrix();
                adjustment=(-adjustment/1000)*context.getGraphicsState().getTextState().getFontSize() *
                    (context.getGraphicsState().getTextState().getHorizontalScalingPercent()/100);
                adjMatrix.setValue( 2, 0, adjustment );
                context.setTextMatrix( adjMatrix.multiply( context.getTextMatrix() ) );
            }
            else if( next instanceof COSString )
            {
//            	TEST 27.08.09
//            	((PDFObjectExtractor)context).setNewTextFragment(true);
            	
            	((PDFObjectExtractor)context).showString( ((COSString)next).getBytes() , i);
                //((PDFObjectExtractor)context).setNewTextFragment(false);
                // automatically set in addCharacter method (simplest way possible)
            }
            else
            {
                throw new IOException( "Unknown type in array for TJ operation:" + next );
            }
        }
    }
 
Example 14
Source File: BaseParser.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * This will parse a PDF array object.
 *
 * @return The parsed PDF array.
 *
 * @throws IOException If there is an error parsing the stream.
 */
protected COSArray parseCOSArray() throws IOException
{
    long startPosition = seqSource.getPosition();
    readExpectedChar('[');
    COSArray po = new COSArray();
    COSBase pbo;
    skipSpaces();
    int i;
    while( ((i = seqSource.peek()) > 0) && ((char)i != ']') )
    {
        pbo = parseDirObject();
        if( pbo instanceof COSObject )
        {
            // We have to check if the expected values are there or not PDFBOX-385
            if (po.size() > 0 && po.get(po.size() - 1) instanceof COSInteger)
            {
                COSInteger genNumber = (COSInteger)po.remove( po.size() -1 );
                if (po.size() > 0 && po.get(po.size() - 1) instanceof COSInteger)
                {
                    COSInteger number = (COSInteger)po.remove( po.size() -1 );
                    COSObjectKey key = new COSObjectKey(number.longValue(), genNumber.intValue());
                    pbo = getObjectFromPool(key);
                }
                else
                {
                    // the object reference is somehow wrong
                    pbo = null;
                }
            }
            else
            {
                pbo = null;
            }
        }
        if( pbo != null )
        {
            po.add( pbo );
        }
        else
        {
            //it could be a bad object in the array which is just skipped
            LOG.warn("Corrupt object reference at offset " +
                    seqSource.getPosition() + ", start offset: " + startPosition);

            // This could also be an "endobj" or "endstream" which means we can assume that
            // the array has ended.
            String isThisTheEnd = readString();
            seqSource.unread(isThisTheEnd.getBytes(ISO_8859_1));
            if(ENDOBJ_STRING.equals(isThisTheEnd) || ENDSTREAM_STRING.equals(isThisTheEnd))
            {
                return po;
            }
        }
        skipSpaces();
    }
    // read ']'
    seqSource.read(); 
    skipSpaces();
    return po;
}
 
Example 15
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 16
Source File: PDEncryption.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * returns the COSString contained in the Recipients field at position i.
 *
 * @param i the position in the Recipients field array.
 *
 * @return a COSString object containing information about the recipient number i.
 */
public COSString getRecipientStringAt(int i)
{
    COSArray array = (COSArray) dictionary.getItem(COSName.RECIPIENTS);
    return (COSString)array.get(i);
}