Example usage for org.apache.pdfbox.cos COSStream createOutputStream

List of usage examples for org.apache.pdfbox.cos COSStream createOutputStream

Introduction

In this page you can find the example usage for org.apache.pdfbox.cos COSStream createOutputStream.

Prototype

public OutputStream createOutputStream(COSBase filters) throws IOException 

Source Link

Document

Returns a new OutputStream for writing stream data, using and the given filters.

Usage

From source file:ReducePDFSize.java

License:Apache License

public static void main(String[] args) throws IOException {
    if (2 != args.length) {
        throw new RuntimeException("arg0 must be input file, org1 must be output file");
    }// ww  w.j ava 2 s. c  o m
    String in = args[0];
    String out = args[1];
    PDDocument doc = null;

    try {
        doc = PDDocument.load(new File(in));
        doc.setAllSecurityToBeRemoved(true);
        for (COSObject cosObject : doc.getDocument().getObjects()) {
            COSBase base = cosObject.getObject();
            // if it's a stream: decode it, then re-write it using FLATE_DECODE
            if (base instanceof COSStream) {
                COSStream stream = (COSStream) base;
                byte[] bytes;
                try {
                    bytes = new PDStream(stream).toByteArray();
                } catch (IOException ex) {
                    // NOTE: original example code from PDFBox just logged & "continue;"d here, 'skipping' this stream.
                    // If this type of failure ever happens, we can (perhaps) consider (re)ignoring this type of failure?
                    //
                    // IIUC then that will leave the original (non-decoded / non-flated) stream in place?
                    throw new RuntimeException("can't serialize byte[] from: " + cosObject.getObjectNumber()
                            + " " + cosObject.getGenerationNumber() + " obj: " + ex.getMessage(), ex);
                }
                stream.removeItem(COSName.FILTER);
                OutputStream streamOut = stream.createOutputStream(COSName.FLATE_DECODE);
                streamOut.write(bytes);
                streamOut.close();
            }
        }
        doc.getDocumentCatalog();
        doc.save(out);
    } finally {
        if (doc != null) {
            doc.close();
        }
    }
}