org.apache.pdfbox.pdmodel.common.PDStream Java Examples

The following examples show how to use org.apache.pdfbox.pdmodel.common.PDStream. 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: ColorsProcessor.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
/**
 * Parses a document extracting the colors for the specified words in
 * the constructor
 *
 * @param filename PDF document path
 */
public void parse (String filename) throws IOException {
    PDDocument document = null;
    try {
        document = PDDocument.load(filename, false);
        List allPages = document.getDocumentCatalog().getAllPages();
        for( int i=0; i<allPages.size(); i++ ) {
            PDPage page = (PDPage)allPages.get( i );
            PDStream contents = page.getContents();
            if (contents != null) {
                processStream( page, page.getResources(),
                    page.getContents().getStream() );
            }
        }
    } finally {
        if (document != null) {
            document.close();
        }
    }
}
 
Example #2
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 #3
Source File: PDCIDFontType2Embedder.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Builds the CIDSet entry, required by PDF/A. This lists all CIDs in the font, including those
 * that don't have a GID.
 */
private void buildCIDSet(Map<Integer, Integer> cidToGid) throws IOException
{
    int cidMax = Collections.max(cidToGid.keySet());
    byte[] bytes = new byte[cidMax / 8 + 1];
    for (int cid = 0; cid <= cidMax; cid++)
    {
        int mask = 1 << 7 - cid % 8;
        bytes[cid / 8] |= mask;
    }

    InputStream input = new ByteArrayInputStream(bytes);
    PDStream stream = new PDStream(document, input, COSName.FLATE_DECODE);

    fontDescriptor.setCIDSet(stream);
}
 
Example #4
Source File: PDIndexed.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private byte[] getLookupData() throws IOException
{
    if (lookupData == null)
    {
        COSBase lookupTable = array.getObject(3);
        if (lookupTable instanceof COSString)
        {
            lookupData = ((COSString) lookupTable).getBytes();
        }
        else if (lookupTable instanceof COSStream)
        {
            lookupData = new PDStream((COSStream)lookupTable).toByteArray();
        }
        else if (lookupTable == null)
        {
            lookupData = new byte[0];
        }
        else
        {
            throw new IOException("Error: Unknown type for lookup table " + lookupTable);
        }
    }
    return lookupData;
}
 
Example #5
Source File: PDImageXObject.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Creates an Image XObject with the given stream as its contents and current color spaces. This
 * constructor is for internal PDFBox use and is not for PDF generation. Users who want to
 * create images should look at {@link #createFromFileByExtension(File, PDDocument) }.
 *
 * @param stream the XObject stream to read
 * @param resources the current resources
 * @throws java.io.IOException if there is an error creating the XObject.
 */
public PDImageXObject(PDStream stream, PDResources resources) throws IOException
{
    super(stream, COSName.IMAGE);
    this.resources = resources;
    List<COSName> filters = stream.getFilters();
    if (filters != null && !filters.isEmpty() && COSName.JPX_DECODE.equals(filters.get(filters.size()-1)))
    {
        COSInputStream is = null;
        try
        {
            is = stream.createInputStream();
            DecodeResult decodeResult = is.getDecodeResult();
            stream.getCOSObject().addAll(decodeResult.getParameters());
            this.colorSpace = decodeResult.getJPXColorSpace();
        }
        finally
        {
            IOUtils.closeQuietly(is);
        }
    }
}
 
Example #6
Source File: PDImageXObject.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Returns the Mask Image XObject associated with this image, or null if there is none.
 * @return Mask Image XObject
 * @throws java.io.IOException
 */
public PDImageXObject getMask() throws IOException
{
    COSBase mask = getCOSObject().getDictionaryObject(COSName.MASK);
    if (mask instanceof COSArray)
    {
        // color key mask, no explicit mask to return
        return null;
    }
    else
    {
        COSStream cosStream = getCOSObject().getCOSStream(COSName.MASK);
        if (cosStream != null)
        {
            // always DeviceGray
            return new PDImageXObject(new PDStream(cosStream), null);
        }
        return null;
    }
}
 
Example #7
Source File: TrueTypeEmbedder.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
public void buildFontFile2(InputStream ttfStream) throws IOException
{
    PDStream stream = new PDStream(document, ttfStream, COSName.FLATE_DECODE);

    // as the stream was closed within the PDStream constructor, we have to recreate it
    InputStream input = null;
    try
    {
        input = stream.createInputStream();
        ttf = new TTFParser().parseEmbedded(input);
        if (!isEmbeddingPermitted(ttf))
        {
            throw new IOException("This font does not permit embedding");
        }
        if (fontDescriptor == null)
        {
            fontDescriptor = createFontDescriptor(ttf);
        }
    }
    finally
    {
        IOUtils.closeQuietly(input);
    }
    stream.getCOSObject().setLong(COSName.LENGTH1, ttf.getOriginalDataSize());
    fontDescriptor.setFontFile2(stream);
}
 
Example #8
Source File: PDCIDFontType2Embedder.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private void buildCIDToGIDMap(Map<Integer, Integer> cidToGid) throws IOException
{
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int cidMax = Collections.max(cidToGid.keySet());
    for (int i = 0; i <= cidMax; i++)
    {
        int gid;
        if (cidToGid.containsKey(i))
        {
            gid = cidToGid.get(i);
        }
        else
        {
            gid = 0;
        }
        out.write(new byte[] { (byte)(gid >> 8 & 0xff), (byte)(gid & 0xff) });
    }

    InputStream input = new ByteArrayInputStream(out.toByteArray());
    PDStream stream = new PDStream(document, input, COSName.FLATE_DECODE);

    cidFont.setItem(COSName.CID_TO_GID_MAP, stream);
}
 
Example #9
Source File: PDVisibleSigBuilder.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void createImageForm(PDResources imageFormResources, PDResources innerFormResource,
                            PDStream imageFormStream, PDRectangle bbox, AffineTransform at,
                            PDImageXObject img) throws IOException
{
    PDFormXObject imageForm = new PDFormXObject(imageFormStream);
    imageForm.setBBox(bbox);
    imageForm.setMatrix(at);
    imageForm.setResources(imageFormResources);
    imageForm.setFormType(1);

    imageFormResources.getCOSObject().setDirect(true);

    COSName imageFormName = COSName.getPDFName("n2");
    innerFormResource.put(imageFormName, imageForm);
    COSName imageName = imageFormResources.add(img, "img");
    pdfStructure.setImageForm(imageForm);
    pdfStructure.setImageFormName(imageFormName);
    pdfStructure.setImageName(imageName);
    LOG.info("Created image form");
}
 
Example #10
Source File: PDVisibleSigBuilder.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void injectAppearanceStreams(PDStream holderFormStream, PDStream innerFormStream,
                                    PDStream imageFormStream, COSName imageFormName,
                                    COSName imageName, COSName innerFormName,
                                    PDVisibleSignDesigner properties) throws IOException
{
    // Use width and height of BBox as values for transformation matrix.
    int width = (int) this.getStructure().getFormatterRectangle().getWidth();
    int height = (int) this.getStructure().getFormatterRectangle().getHeight();

    String imgFormContent    = "q " + width + " 0 0 " + height + " 0 0 cm /" + imageName.getName() + " Do Q\n";
    String holderFormContent = "q 1 0 0 1 0 0 cm /" + innerFormName.getName() + " Do Q\n";
    String innerFormContent  = "q 1 0 0 1 0 0 cm /n0 Do Q q 1 0 0 1 0 0 cm /" + imageFormName.getName() + " Do Q\n";

    appendRawCommands(pdfStructure.getHolderFormStream().createOutputStream(), holderFormContent);
    appendRawCommands(pdfStructure.getInnerFormStream().createOutputStream(), innerFormContent);
    appendRawCommands(pdfStructure.getImageFormStream().createOutputStream(), imgFormContent);
    LOG.info("Injected appearance stream to pdf");
}
 
Example #11
Source File: ColorsProcessor.java    From asciidoctorj-pdf with Apache License 2.0 6 votes vote down vote up
/**
 * Parses a document extracting the colors for the specified words in
 * the constructor
 *
 * @param filename PDF document path
 */
public void parse (String filename) throws IOException {
    PDDocument document = null;
    try {
        document = PDDocument.load(filename, false);
        List allPages = document.getDocumentCatalog().getAllPages();
        for( int i=0; i<allPages.size(); i++ ) {
            PDPage page = (PDPage)allPages.get( i );
            PDStream contents = page.getContents();
            if (contents != null) {
                processStream( page, page.getResources(),
                    page.getContents().getStream() );
            }
        }
    } finally {
        if (document != null) {
            document.close();
        }
    }
}
 
Example #12
Source File: FontTable.java    From Pdf2Dom with GNU Lesser General Public License v3.0 6 votes vote down vote up
private byte[] loadOtherTypeFont(PDStream fontFile) throws IOException
{
    // Likley Bare CFF which needs to be converted to a font supported by browsers, can be
    // other font types which are not yet supported.
    try
    {
        FVFont font = FontVerter.convertFont(fontFile.toByteArray(), FontVerter.FontFormat.WOFF1);
        mimeType = "application/x-font-woff";
        fileEnding = font.getProperties().getFileEnding();

        return font.getData();
    } catch (Exception ex) {
        log.error("Issue converting Bare CFF font or the font type is not supportedby Pdf2Dom, " +
                "Font: {} Exception: {} {}", fontName, ex.getMessage(), ex.getClass());

        // don't barf completley for font conversion issue, html will still be useable without.
        return new byte[0];
    }
}
 
Example #13
Source File: PDImageXObject.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Creates a thumbnail Image XObject from the given COSBase and name.
 * @param cosStream the COS stream
 * @return an XObject
 * @throws IOException if there is an error creating the XObject.
 */
public static PDImageXObject createThumbnail(COSStream cosStream) throws IOException
{
    // thumbnails are special, any non-null subtype is treated as being "Image"
    PDStream pdStream = new PDStream(cosStream);
    return new PDImageXObject(pdStream, null);
}
 
Example #14
Source File: PDICCBased.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Creates a new ICC color space with an empty stream.
 * @param doc the document to store the ICC data
 */
public PDICCBased(PDDocument doc)
{
    array = new COSArray();
    array.add(COSName.ICCBASED);
    stream = new PDStream(doc);
    array.add(stream);
}
 
Example #15
Source File: NurminenDetectionAlgorithm.java    From tabula-java with MIT License 5 votes vote down vote up
private PDDocument removeText(PDPage page) throws IOException {

        PDFStreamParser parser = new PDFStreamParser(page);
        parser.parse();
        List<Object> tokens = parser.getTokens();
        List<Object> newTokens = new ArrayList<>();
        for (Object token : tokens) {
            if (token instanceof Operator) {
                Operator op = (Operator) token;
                if (op.getName().equals("TJ") || op.getName().equals("Tj")) {
                    //remove the one argument to this operator
                    newTokens.remove(newTokens.size() - 1);
                    continue;
                }
            }
            newTokens.add(token);
        }

        PDDocument document = new PDDocument();
        PDPage newPage = document.importPage(page);
        newPage.setResources(page.getResources());

        PDStream newContents = new PDStream(document);
        OutputStream out = newContents.createOutputStream(COSName.FLATE_DECODE);
        ContentStreamWriter writer = new ContentStreamWriter(out);
        writer.writeTokens(newTokens);
        out.close();
        newPage.setContents(newContents);
        return document;
    }
 
Example #16
Source File: PDXObject.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Creates a new XObject instance of the appropriate type for the COS stream.
 *
 * @param base The stream which is wrapped by this XObject.
 * @param resources
 * @return A new XObject instance.
 * @throws java.io.IOException if there is an error creating the XObject.
 */
public static PDXObject createXObject(COSBase base, PDResources resources) throws IOException
{
    if (base == null)
    {
        // TODO throw an exception?
        return null;
    }

    if (!(base instanceof COSStream))
    {
        throw new IOException("Unexpected object type: " + base.getClass().getName());
    }

    COSStream stream = (COSStream)base;
    String subtype = stream.getNameAsString(COSName.SUBTYPE);

    if (COSName.IMAGE.getName().equals(subtype))
    {
        return new PDImageXObject(new PDStream(stream), resources);
    }
    else if (COSName.FORM.getName().equals(subtype))
    {
        ResourceCache cache = resources != null ? resources.getResourceCache() : null;
        COSDictionary group = (COSDictionary)stream.getDictionaryObject(COSName.GROUP);
        if (group != null && COSName.TRANSPARENCY.equals(group.getCOSName(COSName.S)))
        {
            return new PDTransparencyGroup(stream, cache);
        }
        return new PDFormXObject(stream, cache);
    }
    else if (COSName.PS.getName().equals(subtype))
    {
        return new PDPostScriptXObject(stream);
    }
    else
    {
        throw new IOException("Invalid XObject Subtype: " + subtype);
    }
}
 
Example #17
Source File: PDFontDescriptor.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Get the CIDSet stream.
 *
 * @return A stream containing a CIDSet.
 */
public PDStream getCIDSet()
{
    COSObjectable cidSet = dic.getDictionaryObject(COSName.CID_SET);
    if (cidSet instanceof COSStream)
    {
        return new PDStream((COSStream) cidSet);
    }
    return null;
}
 
Example #18
Source File: FontTable.java    From Pdf2Dom with GNU Lesser General Public License v3.0 5 votes vote down vote up
private byte[] loadTrueTypeFont(PDStream fontFile) throws IOException
{
    // could convert to WOFF though for optimal html output instead.
    mimeType = "application/x-font-truetype";
    fileEnding = "otf";

    byte[] fontData = fontFile.toByteArray();

    FVFont font = FontVerter.readFont(fontData);
    byte[] fvFontData = tryNormalizeFVFont(font);
    if (fvFontData.length != 0)
        fontData = fvFontData;

    return fontData;
}
 
Example #19
Source File: TrueTypeEmbedder.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Creates a new TrueType font for embedding.
 */
@SuppressWarnings("deprecation")
TrueTypeEmbedder(PDDocument document, COSDictionary dict, TrueTypeFont ttf,
                 boolean embedSubset) throws IOException
{
    this.document = document;
    this.embedSubset = embedSubset;
    this.ttf = ttf;
    fontDescriptor = createFontDescriptor(ttf);

    if (!isEmbeddingPermitted(ttf))
    {
        throw new IOException("This font does not permit embedding");
    }

    if (!embedSubset)
    {
        // full embedding
        PDStream stream = new PDStream(document, ttf.getOriginalData(), COSName.FLATE_DECODE);
        stream.getCOSObject().setLong(COSName.LENGTH1, ttf.getOriginalDataSize());
        fontDescriptor.setFontFile2(stream);
    }

    dict.setName(COSName.BASE_FONT, ttf.getName());

    // choose a Unicode "cmap"
    cmap = ttf.getUnicodeCmap();
    cmapLookup = ttf.getUnicodeCmapLookup();
}
 
Example #20
Source File: PDFunction.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Constructor.
 *
 * @param function The function stream.
 * 
 */
public PDFunction( COSBase function )
{
    if (function instanceof COSStream)
    {
        functionStream = new PDStream( (COSStream)function );
        functionStream.getCOSObject().setItem( COSName.TYPE, COSName.FUNCTION );
    }
    else if (function instanceof COSDictionary)
    {
        functionDictionary = (COSDictionary)function;
    }
}
 
Example #21
Source File: NPEfix16_sixteen_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Default constructor, creates empty stream.
 *
 * @param doc The document to store the icc data.
 */
public PDICCBased( PDDocument doc )
{
    array = new COSArray();
    array.add( COSName.ICCBASED );
    stream = new PDStream(doc);
    array.add(stream);
}
 
Example #22
Source File: PDVisibleSigBuilder.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void createHolderFormStream(PDDocument template)
{
    PDStream holderForm = new PDStream(template);
    pdfStructure.setHolderFormStream(holderForm);
    LOG.info("Holder form stream has been created");
}
 
Example #23
Source File: PDVisibleSigBuilder.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void createHolderForm(PDResources holderFormResources, PDStream holderFormStream,
                             PDRectangle bbox)
{
    PDFormXObject holderForm = new PDFormXObject(holderFormStream);
    holderForm.setResources(holderFormResources);
    holderForm.setBBox(bbox);
    holderForm.setFormType(1);
    pdfStructure.setHolderForm(holderForm);
    LOG.info("Holder form has been created");

}
 
Example #24
Source File: PDVisibleSigBuilder.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void createInnerFormStream(PDDocument template)
{
    PDStream innerFormStream = new PDStream(template);
    pdfStructure.setInnterFormStream(innerFormStream);
    LOG.info("Stream of another form (inner form - it will be inside holder form) " +
             "has been created");
}
 
Example #25
Source File: PDVisibleSigBuilder.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void createInnerForm(PDResources innerFormResources,
                            PDStream innerFormStream,
                            PDRectangle bbox)
{
    PDFormXObject innerForm = new PDFormXObject(innerFormStream);
    innerForm.setResources(innerFormResources);
    innerForm.setBBox(bbox);
    innerForm.setFormType(1);
    pdfStructure.setInnerForm(innerForm);
    LOG.info("Another form (inner form - it will be inside holder form) has been created");
}
 
Example #26
Source File: PDVisibleSigBuilder.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void createImageFormStream(PDDocument template)
{
    PDStream imageFormStream = new PDStream(template);
    pdfStructure.setImageFormStream(imageFormStream);
    LOG.info("Created image form stream");
}
 
Example #27
Source File: PDPage.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * This will set the contents of this page.
 *
 * @param contents Array of new contents of the page.
 */
public void setContents(List<PDStream> contents)
{
    COSArray array = new COSArray();
    for (PDStream stream : contents)
    {
        array.add(stream);
    }
    page.setItem(COSName.CONTENTS, array);
}
 
Example #28
Source File: PdfContentStreamEditor.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
@Override
public void processPage(PDPage page) throws IOException {
    PDStream stream = new PDStream(document);
    replacement = new ContentStreamWriter(replacementStream = stream.createOutputStream(COSName.FLATE_DECODE));
    super.processPage(page);
    replacementStream.close();
    page.setContents(stream);
    replacement = null;
    replacementStream = null;
}
 
Example #29
Source File: NPEfix16_sixteen_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Default constructor, creates empty stream.
 *
 * @param doc The document to store the icc data.
 */
public PDICCBased( PDDocument doc )
{
    array = new COSArray();
    array.add( COSName.ICCBASED );
    array.add( new PDStream( doc ) );
}
 
Example #30
Source File: PDXObject.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Creates a new XObject of the given subtype for writing.
 * @param document The document in which to create the XObject.
 * @param subtype The subtype of the new XObject.
 */
protected PDXObject(PDDocument document, COSName subtype)
{
    stream = new PDStream(document);
    stream.getCOSObject().setName(COSName.TYPE, COSName.XOBJECT.getName());
    stream.getCOSObject().setName(COSName.SUBTYPE, subtype.getName());
}