PDStream dest = new PDStream(this, page.getContents(), COSName.FLATE_DECODE); importedPage.setContents(dest); addPage(importedPage);
PDStream newStream = new PDStream(targetDoc, page.getContents(), COSName.FLATE_DECODE); PDFormXObject form = new PDFormXObject(newStream);
// read the existing document PDDocument doc; doc = PDDocument.load(musiqueFileName); List<?> pages = doc.getDocumentCatalog().getAllPages(); PDPage page = (PDPage) pages.get(0); PDPageContentStream content = new PDPageContentStream(doc, page, true, true); TestRewriter rewriter = new TestRewriter(content); rewriter.processStream(page, page.findResources(), page.getContents().getStream()); content.close(); // save the modified document doc.save(modifiedMusiqueFileName); doc.close();
PDPage pdPage = doc.getPage(page); InputStream oldContentStream = pdPage.getContents(); byte[] ba = IOUtils.toByteArray(oldContentStream); oldContentStream.close(); // brings a warning because a content stream already exists PDPageContentStream newContentStream = new PDPageContentStream(doc, pdPage, false, true); // createFromFile is the easiest way with an image file // if you already have the image in a BufferedImage, // call LosslessFactory.createFromImage() instead PDImageXObject pdImage = PDImageXObject.createFromFile(imagePath, doc); newContentStream.saveGraphicsState(); newContentStream.drawImage(pdImage, 0, 0); newContentStream.restoreGraphicsState(); newContentStream.close(); // append the saved existing content stream PDPageContentStream newContentStream2 = new PDPageContentStream(doc, pdPage, true, true); newContentStream2.appendRawCommands(ba); // deprecated... needs to be rediscussed among devs newContentStream2.close();
PDDocument doc = null; try { doc = PDDocument.load("C:/Path/To/Pdf/Sample.pdf"); PDFStreamEngine engine = new PDFStreamEngine(ResourceLoader.loadProperties("org/apache/pdfbox/resources/PageDrawer.properties")); 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(); }
locator.processStream(page, page.findResources(), page.getContents().getStream());
PDDocument doc = null; try { doc = PDDocument.load("C:/Path/To/Pdf/Sample.pdf"); PDFStreamEngine engine = new PDFStreamEngine(ResourceLoader.loadProperties("org/apache/pdfbox/resources/PageDrawer.properties")); 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(); }
PDStream contents = page.getContents(); PDFStreamParser parser = new PDFStreamParser(contents.getStream()); parser.parse();
List<PDPage> pages = doc.getDocumentCatalog().getAllPages(); for (PDPage page : pages) { this.processStream(page, page.getResources(), page.getContents().getStream());
PDStream contentStream = page.getContents(); if (contentStream != null) { COSStream contents = contentStream.getStream();
PDFormXObject importAsXObject(PDDocument target, PDPage page) throws IOException { final PDStream src = page.getContents(); if (src != null) { final PDFormXObject xobject = new PDFormXObject(target); OutputStream os = xobject.getPDStream().createOutputStream(); InputStream is = src.createInputStream(); try { IOUtils.copy(is, os); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); } xobject.setResources(page.findResources()); xobject.setBBox(page.findCropBox()); return xobject; } return null; }
PDFStreamParser parser = new PDFStreamParser(page.getContents()); parser.parse(); List tokens = parser.getTokens();
PDDocument document = PDDocument.load("0006-sun1-4.pdf"); List<PDPage> allPages = document.getDocumentCatalog().getAllPages(); PDPage firstPage = allPages.get(0); PDRectangle pageSize = firstPage.findMediaBox(); PDStream contents = firstPage.getContents(); PDFStreamParser parser = new PDFStreamParser(contents.getStream()); parser.parse(); List<Object> tokens = parser.getTokens(); tokens.add(0, PDFOperator.getOperator("q")); tokens.add(PDFOperator.getOperator("Q")); PDStream updatedStream = new PDStream(document); OutputStream out = updatedStream.createOutputStream(); ContentStreamWriter tokenWriter = new ContentStreamWriter(out); tokenWriter.writeTokens(tokens); firstPage.setContents(updatedStream); PDPageContentStream contentStream = new PDPageContentStream(document, firstPage, true, true); contentStream.setStrokingColor(Color.red); contentStream.beginText(); contentStream.moveTextPositionByAmount(300, pageSize.getUpperRightY() - 420 - 10); contentStream.setFont(PDType1Font.HELVETICA_BOLD, 10); contentStream.drawString("MIDDLE"); contentStream.endText(); contentStream.close(); document.save("0006-sun1-4-restoredAdd.pdf"); document.close();
/** * Parses a document extracting the colors for the specified words in * the constructor * * @param filename PDF document path */ public void parse (String filename) throws IOException { PDDocument document = null; try { document = PDDocument.load(filename, false); List allPages = document.getDocumentCatalog().getAllPages(); for( int i=0; i<allPages.size(); i++ ) { PDPage page = (PDPage)allPages.get( i ); PDStream contents = page.getContents(); if (contents != null) { processStream( page, page.getResources(), page.getContents().getStream() ); } } } finally { if (document != null) { document.close(); } } }
processStream( page, resources, page.getContents().getStream() ); List annotations = page.getAnnotations(); for( int i=0; i<annotations.size(); i++ )
/** * Parses a document extracting the images * * @param filename PDF document path */ public void parse(String filename) throws IOException { PDDocument document = null; try { document = PDDocument.load(filename, false); List allPages = document.getDocumentCatalog().getAllPages(); for( int i=0; i<allPages.size(); i++ ) { PDPage page = (PDPage)allPages.get( i ); currentPage = i; processStream( page, page.findResources(), page.getContents().getStream() ); } } finally { if (document != null) { document.close(); } } }
PDStream dest = new PDStream(this, page.getContents(), COSName.FLATE_DECODE); importedPage.setContents(dest); addPage(importedPage);
PDStream dest = new PDStream(this, page.getContents(), COSName.FLATE_DECODE); importedPage.setContents(dest); addPage(importedPage);
PDStream newStream = new PDStream(targetDoc, page.getContents(), COSName.FLATE_DECODE); PDFormXObject form = new PDFormXObject(newStream);
PDStream newStream = new PDStream(targetDoc, page.getContents(), COSName.FLATE_DECODE); PDFormXObject form = new PDFormXObject(newStream);