List of usage examples for org.apache.pdfbox.contentstream PDFStreamEngine processPage
public void processPage(PDPage page) throws IOException
From source file:org.geoserver.wms.map.PDFGetMapTest.java
License:Open Source License
/** * Returns the last tiling pattern found during a render of the PDF document. Can be used to extract * one tiling pattern that gets actually used to render shapes (meant to be used against a document * that only has a single tiling pattern) * //from w ww .j a v a2s.co m * @param pdfDocument * @return * @throws InvalidPasswordException * @throws IOException */ PDTilingPattern getTilingPattern(byte[] pdfDocument) throws InvalidPasswordException, IOException { // load the document using PDFBOX (iText is no good for parsing tiling patterns, mostly works // well for text and image extraction, spent a few hours trying to use it with no results) PDDocument doc = PDDocument.load(pdfDocument); PDPage page = doc.getPage(0); // use a graphics stream engine, it's the only thing I could find that parses the PDF // deep enough to allow catching the tiling pattern in parsed form AtomicReference<PDTilingPattern> pattern = new AtomicReference<>(); PDFStreamEngine engine = new PDFGraphicsStreamEngine(page) { @Override public void strokePath() throws IOException { } @Override public void shadingFill(COSName shadingName) throws IOException { } @Override public void moveTo(float x, float y) throws IOException { } @Override public void lineTo(float x, float y) throws IOException { } @Override public Point2D getCurrentPoint() throws IOException { return null; } @Override public void fillPath(int windingRule) throws IOException { } @Override public void fillAndStrokePath(int windingRule) throws IOException { } @Override public void endPath() throws IOException { } @Override public void drawImage(PDImage pdImage) throws IOException { } @Override public void curveTo(float x1, float y1, float x2, float y2, float x3, float y3) throws IOException { } @Override public void closePath() throws IOException { } @Override public void clip(int windingRule) throws IOException { } @Override public void appendRectangle(Point2D p0, Point2D p1, Point2D p2, Point2D p3) throws IOException { } }; // setup the tiling pattern trap engine.addOperator(new SetNonStrokingColorN() { @Override public void process(Operator operator, List<COSBase> arguments) throws IOException { super.process(operator, arguments); PDColor color = context.getGraphicsState().getNonStrokingColor(); if (context.getGraphicsState().getNonStrokingColorSpace() instanceof PDPattern) { PDPattern colorSpace = (PDPattern) context.getGraphicsState().getNonStrokingColorSpace(); PDAbstractPattern ap = colorSpace.getPattern(color); if (ap instanceof PDTilingPattern) { pattern.set((PDTilingPattern) ap); } } } }); // run it engine.processPage(page); return pattern.get(); }