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

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

Introduction

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

Prototype

@Override
public InputStream getContents() throws IOException 

Source Link

Document

Returns the content stream(s) of this page as a single input stream.

Usage

From source file:pdfreader.ColorScheme.java

public void getColor() throws IOException {
    PDDocument doc = null;/*  w  w w  . j  a  va2 s .c  om*/
    try {
        doc = PDDocument.load("D://My.pdf");
        PDFStreamEngine engine = new PDFStreamEngine(
                ResourceLoader.loadProperties("org//apache//pdfbox//resources//PageDrawer.properties", true));
        PDPage page = (PDPage) doc.getDocumentCatalog().getAllPages().get(0);
        engine.processStream(page, page.findResources(), page.getContents().getStream());
        PDGraphicsState graphicState = engine.getGraphicsState();
        System.out.println(graphicState.getStrokingColor().getColorSpace().getName());
        float colorSpaceValues[] = graphicState.getStrokingColor().getColorSpaceValue();
        for (float c : colorSpaceValues) {
            System.out.println(c * 255);
        }
    } finally {
        if (doc != null) {
            doc.close();
        }

    }
}

From source file:se.streamsource.streamflow.web.application.pdf.Underlay.java

License:Apache License

private PDXObjectForm importAsXObject(PDDocument target, PDPage page) throws IOException {
    final PDStream xobjectStream = new PDStream(target, page.getContents().createInputStream(), false);
    final PDXObjectForm xobject = new PDXObjectForm(xobjectStream);

    xobject.setResources(page.findResources());
    xobject.setBBox(page.findCropBox());

    COSDictionary group = new COSDictionary();
    group.setName("S", "Transparency");
    group.setBoolean(COSName.getPDFName("K"), true);
    xobject.getCOSStream().setItem(COSName.getPDFName("Group"), group);

    return xobject;
}

From source file:Utilities.GlobalVar.java

public static void updateSeqNum(PDDocument doc, String cycle) throws IOException {
    int sequenceNum = 1;
    List pages = doc.getDocumentCatalog().getAllPages();

    for (int i = 0; i < pages.size(); i++) {
        PDPage page = (PDPage) pages.get(i);
        PDStream contents = page.getContents();
        PDFStreamParser parser = new PDFStreamParser(contents.getStream());
        parser.parse();/*from   www .j a v a 2s .  c  om*/
        List tokens = parser.getTokens();
        for (int j = 0; j < tokens.size(); j++) {
            Object next = tokens.get(j);
            if (next instanceof PDFOperator) {
                PDFOperator op = (PDFOperator) next;
                // Tj and TJ are the two operators that display strings in a PDF
                if (op.getOperation().equals("Tj")) {
                    // Tj takes one operator and that is the string
                    // to display so lets update that operator
                    COSString previous = (COSString) tokens.get(j - 1);
                    String string = previous.getString();
                    //                        System.out.println(string);
                    //                        System.out.println(string.charAt(5));
                    if (string.contains("/0")) {
                        String seq = cycle + "/" + GlobalVar.globalCountGenerator5Digit(sequenceNum);
                        string = string.replaceFirst(string, seq);
                        previous.reset();
                        previous.append(string.getBytes("ISO-8859-1"));
                        sequenceNum++;
                        break;
                    }
                    //Word you want to change. Currently this code changes word "Solr" to "Solr123"
                    previous.reset();
                    previous.append(string.getBytes("ISO-8859-1"));

                } else if (op.getOperation().equals("TJ")) {
                    COSArray previous = (COSArray) tokens.get(j - 1);
                    for (int k = 0; k < previous.size(); k++) {
                        Object arrElement = previous.getObject(k);
                        if (arrElement instanceof COSString) {
                            COSString cosString = (COSString) arrElement;
                            String string = cosString.getString();
                            //                                System.out.println(string);
                            if (string.contains("/00")) {
                                String seq = cycle + "/" + GlobalVar.globalCountGenerator5Digit(sequenceNum);
                                string = string.replaceFirst(string, seq);
                                cosString.reset();
                                cosString.append(string.getBytes("ISO-8859-1"));
                                sequenceNum++;
                                break;
                            }
                            // Currently this code changes word "Solr" to "Solr123"
                            cosString.reset();
                            cosString.append(string.getBytes("ISO-8859-1"));
                            //                                break;
                        }
                    }
                }
            }
        }
        // now that the tokens are updated we will replace the page content stream.
        PDStream updatedStream = new PDStream(doc);
        OutputStream out = updatedStream.createOutputStream();
        ContentStreamWriter tokenWriter = new ContentStreamWriter(out);
        tokenWriter.writeTokens(tokens);
        page.setContents(updatedStream);
    }
}