Java Code Examples for org.apache.pdfbox.cos.COSDictionary#getInt()

The following examples show how to use org.apache.pdfbox.cos.COSDictionary#getInt() . 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: PDAbstractPattern.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Create the correct PD Model pattern based on the COS base pattern.
 * @param dictionary the COS pattern dictionary
 * @return the newly created pattern object
 * @throws IOException If we are unable to create the PDPattern object.
 */
public static PDAbstractPattern create(COSDictionary dictionary) throws IOException
{
    PDAbstractPattern pattern;
    int patternType = dictionary.getInt(COSName.PATTERN_TYPE, 0);
    switch (patternType)
    {
        case TYPE_TILING_PATTERN:
            pattern = new PDTilingPattern(dictionary);
            break;
        case TYPE_SHADING_PATTERN:
            pattern = new PDShadingPattern(dictionary);
            break;
        default:
            throw new IOException("Error: Unknown pattern type " + patternType);
    }
    return pattern;
}
 
Example 2
Source File: CCITTFactory.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Creates a new CCITT Fax compressed image XObject from a TIFF file.
 * 
 * @param document the document to create the image as part of.
 * @param reader the random access TIFF file which contains a suitable CCITT
 * compressed image
 * @param number TIFF image number, starting from 0
 * @return a new Image XObject, or null if no such page
 * @throws IOException if there is an error reading the TIFF data.
 */
private static PDImageXObject createFromRandomAccessImpl(PDDocument document,
                                                         RandomAccess reader,
                                                         int number) throws IOException
{
    COSDictionary decodeParms = new COSDictionary();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    extractFromTiff(reader, bos, decodeParms, number);
    if (bos.size() == 0)
    {
        return null;
    }
    ByteArrayInputStream encodedByteStream = new ByteArrayInputStream(bos.toByteArray());
    PDImageXObject pdImage = new PDImageXObject(document, 
            encodedByteStream, 
            COSName.CCITTFAX_DECODE, 
            decodeParms.getInt(COSName.COLUMNS), 
            decodeParms.getInt(COSName.ROWS),
            1,
            PDDeviceGray.INSTANCE);
    
    COSDictionary dict = pdImage.getCOSObject();
    dict.setItem(COSName.DECODE_PARMS, decodeParms);
    return pdImage;
}
 
Example 3
Source File: PDFunction.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Create the correct PD Model function based on the COS base function.
 *
 * @param function The COS function dictionary.
 *
 * @return The PDModel Function object.
 *
 * @throws IOException If we are unable to create the PDFunction object.
 */
public static PDFunction create( COSBase function ) throws IOException
{
    if (function == COSName.IDENTITY)
    {
        return new PDFunctionTypeIdentity(null);
    }
    
    COSBase base = function;
    if (function instanceof COSObject)
    {
        base = ((COSObject) function).getObject();
    }
    if (!(base instanceof COSDictionary))
    {
        throw new IOException("Error: Function must be a Dictionary, but is " +
                base.getClass().getSimpleName());
    }
    COSDictionary functionDictionary = (COSDictionary) base;
    int functionType = functionDictionary.getInt(COSName.FUNCTION_TYPE);
    switch (functionType)
    {
        case 0:
            return new PDFunctionType0(functionDictionary);
        case 2:
            return new PDFunctionType2(functionDictionary);
        case 3:
            return new PDFunctionType3(functionDictionary);
        case 4:
            return new PDFunctionType4(functionDictionary);
        default:
            throw new IOException("Error: Unknown function type " + functionType);
    }
}
 
Example 4
Source File: PDFieldFactory.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private static PDField createButtonSubType(PDAcroForm form, COSDictionary field,
                                           PDNonTerminalField parent)
{
    int flags = field.getInt(COSName.FF, 0);
    // BJL: I have found that the radio flag bit is not always set
    // and that sometimes there is just a kids dictionary.
    // so, if there is a kids dictionary then it must be a radio button group.
    if ((flags & PDButton.FLAG_RADIO) != 0)
    {
        return new PDRadioButton(form, field, parent);
    }
    else if ((flags & PDButton.FLAG_PUSHBUTTON) != 0)
    {
        return new PDPushButton(form, field, parent);
    }
    else
    {
        return new PDCheckBox(form, field, parent);
    }
}
 
Example 5
Source File: LZWFilter.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public DecodeResult decode(InputStream encoded, OutputStream decoded,
        COSDictionary parameters, int index) throws IOException
{
    COSDictionary decodeParams = getDecodeParams(parameters, index);
    int earlyChange = decodeParams.getInt(COSName.EARLY_CHANGE, 1);

    if (earlyChange != 0 && earlyChange != 1)
    {
        earlyChange = 1;
    }

    doLZWDecode(encoded, Predictor.wrapPredictor(decoded, decodeParams), earlyChange);
    return new DecodeResult(parameters);
}
 
Example 6
Source File: Predictor.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Wraps and <code>OutputStream</code> in a predictor decoding stream as necessary.
 * If no predictor is specified by the parameters, the original stream is returned as is.
 *
 * @param out The stream to which decoded data should be written
 * @param decodeParams Decode parameters for the stream
 * @return An <code>OutputStream</code> is returned, which will write decoded data
 * into the given stream. If no predictor is specified, the original stream is returned.
 */
static OutputStream wrapPredictor(OutputStream out, COSDictionary decodeParams)
{
    int predictor = decodeParams.getInt(COSName.PREDICTOR);
    if (predictor > 1)
    {
        int colors = Math.min(decodeParams.getInt(COSName.COLORS, 1), 32);
        int bitsPerPixel = decodeParams.getInt(COSName.BITS_PER_COMPONENT, 8);
        int columns = decodeParams.getInt(COSName.COLUMNS, 1);

        return new PredictorOutputStream(out, predictor, colors, bitsPerPixel, columns);
    }
    else
    {
        return out;
    }
}
 
Example 7
Source File: PDShading.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Create the correct PD Model shading based on the COS base shading.
 *
 * @param shadingDictionary the COS shading dictionary
 * @return the newly created shading resources object
 * @throws IOException if we are unable to create the PDShading object
 */
public static PDShading create(COSDictionary shadingDictionary) throws IOException
{
    PDShading shading = null;
    int shadingType = shadingDictionary.getInt(COSName.SHADING_TYPE, 0);
    switch (shadingType)
    {
        case SHADING_TYPE1:
            shading = new PDShadingType1(shadingDictionary);
            break;
        case SHADING_TYPE2:
            shading = new PDShadingType2(shadingDictionary);
            break;
        case SHADING_TYPE3:
            shading = new PDShadingType3(shadingDictionary);
            break;
        case SHADING_TYPE4:
            shading = new PDShadingType4(shadingDictionary);
            break;
        case SHADING_TYPE5:
            shading = new PDShadingType5(shadingDictionary);
            break;
        case SHADING_TYPE6:
            shading = new PDShadingType6(shadingDictionary);
            break;
        case SHADING_TYPE7:
            shading = new PDShadingType7(shadingDictionary);
            break;
        default:
            throw new IOException("Error: Unknown shading type " + shadingType);
    }
    return shading;
}
 
Example 8
Source File: PDFieldFactory.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private static PDField createChoiceSubType(PDAcroForm form, COSDictionary field,
                                           PDNonTerminalField parent)
{
    int flags = field.getInt(COSName.FF, 0);
    if ((flags & PDChoice.FLAG_COMBO) != 0)
    {
        return new PDComboBox(form, field, parent);
    }
    else
    {
        return new PDListBox(form, field, parent);
    }
}
 
Example 9
Source File: CCITTFaxFilter.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
protected void encode(InputStream input, OutputStream encoded, COSDictionary parameters)
        throws IOException
{
    int cols = parameters.getInt(COSName.COLUMNS);
    int rows = parameters.getInt(COSName.ROWS);
    CCITTFaxEncoderStream ccittFaxEncoderStream = 
            new CCITTFaxEncoderStream(encoded, cols, rows, TIFFExtension.FILL_LEFT_TO_RIGHT);
    IOUtils.copy(input, ccittFaxEncoderStream);
    input.close();
}
 
Example 10
Source File: PDPageTree.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Returns the given COS page using a depth-first search.
 *
 * @param pageNum 1-based page number
 * @param node page tree node to search
 * @param encountered number of pages encountered so far
 * @return COS dictionary of the Page object
 */
private COSDictionary get(int pageNum, COSDictionary node, int encountered)
{
    if (pageNum < 0)
    {
        throw new IndexOutOfBoundsException("Index out of bounds: " + pageNum);
    }

    if (isPageTreeNode(node))
    {
        int count = node.getInt(COSName.COUNT, 0);
        if (pageNum <= encountered + count)
        {
            // it's a kid of this node
            for (COSDictionary kid : getKids(node))
            {
                // which kid?
                if (isPageTreeNode(kid))
                {
                    int kidCount = kid.getInt(COSName.COUNT, 0);
                    if (pageNum <= encountered + kidCount)
                    {
                        // it's this kid
                        return get(pageNum, kid, encountered);
                    }
                    else
                    {
                        encountered += kidCount;
                    }
                }
                else
                {
                    // single page
                    encountered++;
                    if (pageNum == encountered)
                    {
                        // it's this page
                        return get(pageNum, kid, encountered);
                    }
                }
            }

            throw new IllegalStateException("1-based index not found: " + pageNum);
        }
        else
        {
            throw new IndexOutOfBoundsException("1-based index out of bounds: " + pageNum);
        }
    }
    else
    {
        if (encountered == pageNum)
        {
            return node;
        }
        else
        {
            throw new IllegalStateException("1-based index not found: " + pageNum);
        }
    }
}
 
Example 11
Source File: CCITTFaxFilter.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public DecodeResult decode(InputStream encoded, OutputStream decoded,
                                     COSDictionary parameters, int index) throws IOException
{
    // get decode parameters
    COSDictionary decodeParms = getDecodeParams(parameters, index);

    // parse dimensions
    int cols = decodeParms.getInt(COSName.COLUMNS, 1728);
    int rows = decodeParms.getInt(COSName.ROWS, 0);
    int height = parameters.getInt(COSName.HEIGHT, COSName.H, 0);
    if (rows > 0 && height > 0)
    {
        // PDFBOX-771, PDFBOX-3727: rows in DecodeParms sometimes contains an incorrect value
        rows = height;
    }
    else
    {
        // at least one of the values has to have a valid value
        rows = Math.max(rows, height);
    }

    // decompress data
    int k = decodeParms.getInt(COSName.K, 0);
    boolean encodedByteAlign = decodeParms.getBoolean(COSName.ENCODED_BYTE_ALIGN, false);
    int arraySize = (cols + 7) / 8 * rows;
    // TODO possible options??
    byte[] decompressed = new byte[arraySize];
    CCITTFaxDecoderStream s;
    int type;
    long tiffOptions;
    if (k == 0)
    {
        tiffOptions = encodedByteAlign ? TIFFExtension.GROUP3OPT_BYTEALIGNED : 0;
        type = TIFFExtension.COMPRESSION_CCITT_MODIFIED_HUFFMAN_RLE;
    }
    else
    {
        if (k > 0)
        {
            tiffOptions = encodedByteAlign ? TIFFExtension.GROUP3OPT_BYTEALIGNED : 0;
            tiffOptions |= TIFFExtension.GROUP3OPT_2DENCODING;
            type = TIFFExtension.COMPRESSION_CCITT_T4;
        }
        else
        {
            // k < 0
            tiffOptions = encodedByteAlign ? TIFFExtension.GROUP4OPT_BYTEALIGNED : 0;
            type = TIFFExtension.COMPRESSION_CCITT_T6;
        }
    }
    s = new CCITTFaxDecoderStream(encoded, cols, type, TIFFExtension.FILL_LEFT_TO_RIGHT, tiffOptions);
    readFromDecoderStream(s, decompressed);

    // invert bitmap
    boolean blackIsOne = decodeParms.getBoolean(COSName.BLACK_IS_1, false);
    if (!blackIsOne)
    {
        // Inverting the bitmap
        // Note the previous approach with starting from an IndexColorModel didn't work
        // reliably. In some cases the image wouldn't be painted for some reason.
        // So a safe but slower approach was taken.
        invertBitmap(decompressed);
    }

    decoded.write(decompressed);
    return new DecodeResult(parameters);
}