Example usage for org.apache.pdfbox.pdmodel PDPage getContentStreams

List of usage examples for org.apache.pdfbox.pdmodel PDPage getContentStreams

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel PDPage getContentStreams.

Prototype

public Iterator<PDStream> getContentStreams() 

Source Link

Document

Returns the content streams which make up this page.

Usage

From source file:at.gv.egiz.pdfas.lib.impl.pdfbox2.placeholder.SignaturePlaceholderExtractor.java

License:EUPL

/**
 * Search the document for placeholder images and possibly included
 * additional info.<br/>//from   w w w  .  java  2s .  c o m
 * Searches only for the first placeholder page after page from top.
 *
 * @param inputStream
 * @return all available info from the first found placeholder.
 * @throws PDFDocumentException
 *             if the document could not be read.
 * @throws PlaceholderExtractionException
 *             if STRICT matching mode was requested and no suitable
 *             placeholder could be found.
 */
public static SignaturePlaceholderData extract(PDDocument doc, String placeholderId, int matchMode)
        throws PdfAsException {
    SignaturePlaceholderContext.setSignaturePlaceholderData(null);

    SignaturePlaceholderExtractor extractor;
    try {
        extractor = new SignaturePlaceholderExtractor(placeholderId, matchMode, doc);
    } catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException e2) {
        throw new PDFIOException("error.pdf.io.04", e2);
    }

    int pageNr = 0;
    for (PDPage page : doc.getPages()) {
        pageNr++;

        try {
            extractor.setCurrentPage(pageNr);
            if (page.getContents() != null && page.getResources() != null && page.getContentStreams() != null) {
                extractor.processPage(page); //TODO: pdfbox2 - right?

            }
            SignaturePlaceholderData ret = matchPlaceholderPage(extractor.placeholders, placeholderId,
                    matchMode);
            if (ret != null) {
                SignaturePlaceholderContext.setSignaturePlaceholderData(ret);
                return ret;
            }
        } catch (IOException e1) {
            throw new PDFIOException("error.pdf.io.04", e1);
        } catch (Throwable e) {
            throw new PDFIOException("error.pdf.io.04", e);
        }
    }
    if (extractor.placeholders.size() > 0) {
        SignaturePlaceholderData ret = matchPlaceholderDocument(extractor.placeholders, placeholderId,
                matchMode);
        SignaturePlaceholderContext.setSignaturePlaceholderData(ret);
        return ret;
    }
    // no placeholders found, apply strict mode if set
    if (matchMode == PLACEHOLDER_MATCH_MODE_STRICT) {
        throw new PlaceholderExtractionException("error.pdf.stamp.09");
    }

    return null;
}