Example usage for org.apache.pdfbox.pdmodel.common PDStream createOutputStream

List of usage examples for org.apache.pdfbox.pdmodel.common PDStream createOutputStream

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel.common PDStream createOutputStream.

Prototype

public OutputStream createOutputStream() throws IOException 

Source Link

Document

This will get a stream that can be written to.

Usage

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 w  w w  . j  av  a  2  s  .  com*/
        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);
    }
}