Java Code Examples for org.apache.pdfbox.cos.COSStream#createInputStream()

The following examples show how to use org.apache.pdfbox.cos.COSStream#createInputStream() . 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: PDCIDFont.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
final int[] readCIDToGIDMap() throws IOException
{
    int[] cid2gid = null;
    COSBase map = dict.getDictionaryObject(COSName.CID_TO_GID_MAP);
    if (map instanceof COSStream)
    {
        COSStream stream = (COSStream) map;

        InputStream is = stream.createInputStream();
        byte[] mapAsBytes = IOUtils.toByteArray(is);
        IOUtils.closeQuietly(is);
        int numberOfInts = mapAsBytes.length / 2;
        cid2gid = new int[numberOfInts];
        int offset = 0;
        for (int index = 0; index < numberOfInts; index++)
        {
            int gid = (mapAsBytes[offset] & 0xff) << 8 | mapAsBytes[offset + 1] & 0xff;
            cid2gid[index] = gid;
            offset += 2;
        }
    }
    return cid2gid;
}
 
Example 2
Source File: Overlay.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private COSStream createCombinedContentStream(COSBase contents) throws IOException
{
    List<COSStream> contentStreams = createContentStreamList(contents);
    // concatenate streams
    COSStream concatStream = inputPDFDocument.getDocument().createCOSStream();
    OutputStream out = concatStream.createOutputStream(COSName.FLATE_DECODE);
    for (COSStream contentStream : contentStreams)
    {
        InputStream in = contentStream.createInputStream();
        IOUtils.copy(in, out);
        out.flush();
        in.close();
    }
    out.close();
    return concatStream;
}
 
Example 3
Source File: Type5ShadingContext.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
private List<ShadedTriangle> collectTriangles(PDShadingType5 latticeTriangleShadingType,
        AffineTransform xform, Matrix matrix) throws IOException
{
    COSDictionary dict = latticeTriangleShadingType.getCOSObject();
    if (!(dict instanceof COSStream))
    {
        return Collections.emptyList();
    }
    PDRange rangeX = latticeTriangleShadingType.getDecodeForParameter(0);
    PDRange rangeY = latticeTriangleShadingType.getDecodeForParameter(1);
    if (Float.compare(rangeX.getMin(), rangeX.getMax()) == 0 ||
        Float.compare(rangeY.getMin(), rangeY.getMax()) == 0)
    {
        return Collections.emptyList();
    }
    int numPerRow = latticeTriangleShadingType.getVerticesPerRow();
    PDRange[] colRange = new PDRange[numberOfColorComponents];
    for (int i = 0; i < numberOfColorComponents; ++i)
    {
        colRange[i] = latticeTriangleShadingType.getDecodeForParameter(2 + i);
    }
    List<Vertex> vlist = new ArrayList<Vertex>();
    long maxSrcCoord = (long) Math.pow(2, bitsPerCoordinate) - 1;
    long maxSrcColor = (long) Math.pow(2, bitsPerColorComponent) - 1;
    COSStream cosStream = (COSStream) dict;

    ImageInputStream mciis = new MemoryCacheImageInputStream(cosStream.createInputStream());
    try
    {
        boolean eof = false;
        while (!eof)
        {
            Vertex p;
            try
            {
                p = readVertex(mciis, maxSrcCoord, maxSrcColor, rangeX, rangeY, colRange, matrix, xform);
                vlist.add(p);
            }
            catch (EOFException ex)
            {
                eof = true;
            }
        }
    }
    finally
    {
        mciis.close();
    }
    int rowNum = vlist.size() / numPerRow;
    Vertex[][] latticeArray = new Vertex[rowNum][numPerRow];
    List<ShadedTriangle> list = new ArrayList<ShadedTriangle>();
    if (rowNum < 2)
    {
        // must have at least two rows; if not, return empty list
        return list;
    }
    for (int i = 0; i < rowNum; i++)
    {
        for (int j = 0; j < numPerRow; j++)
        {
            latticeArray[i][j] = vlist.get(i * numPerRow + j);
        }
    }

    for (int i = 0; i < rowNum - 1; i++)
    {
        for (int j = 0; j < numPerRow - 1; j++)
        {
            Point2D[] ps = new Point2D[] {
                latticeArray[i][j].point,
                latticeArray[i][j + 1].point,
                latticeArray[i + 1][j].point  };

            float[][] cs = new float[][] {
                latticeArray[i][j].color,
                latticeArray[i][j + 1].color,
                latticeArray[i + 1][j].color };

            list.add(new ShadedTriangle(ps, cs));

            ps = new Point2D[] {
                latticeArray[i][j + 1].point,
                latticeArray[i + 1][j].point,
                latticeArray[i + 1][j + 1].point };

            cs = new float[][]{
                latticeArray[i][j + 1].color,
                latticeArray[i + 1][j].color,
                latticeArray[i + 1][j + 1].color };

            list.add(new ShadedTriangle(ps, cs));
        }
    }
    return list;
}
 
Example 4
Source File: PDFObjectStreamParser.java    From gcs with Mozilla Public License 2.0 3 votes vote down vote up
/**
 * Constructor.
 *
 * @param stream The stream to parse.
 * @param document The document for the current parsing.
 * @throws IOException If there is an error initializing the stream.
 */
public PDFObjectStreamParser(COSStream stream, COSDocument document) throws IOException
{
    super(new InputStreamSource(stream.createInputStream()));
    this.stream = stream;
    this.document = document;
}
 
Example 5
Source File: PDFXrefStreamParser.java    From gcs with Mozilla Public License 2.0 3 votes vote down vote up
/**
 * Constructor.
 *
 * @param stream The stream to parse.
 * @param document The document for the current parsing.
 * @param resolver resolver to read the xref/trailer information
 *
 * @throws IOException If there is an error initializing the stream.
 */
public PDFXrefStreamParser(COSStream stream, COSDocument document, XrefTrailerResolver resolver)
        throws IOException
{
    super(new InputStreamSource(stream.createInputStream()));
    this.stream = stream;
    this.document = document;
    this.xrefTrailerResolver = resolver;
}
 
Example 6
Source File: PDFStreamParser.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Constructor.
 *
 * @param stream The stream to parse.
 * @throws IOException If there is an error initializing the stream.
 * 
 * @deprecated Use {@link PDFStreamParser#PDFStreamParser(PDContentStream)} instead.
 */
@Deprecated
public PDFStreamParser(COSStream stream) throws IOException
{
    super(new InputStreamSource(stream.createInputStream()));
}