import org.apache.pdfbox.exceptions.COSVisitorException; import org.apache.pdfbox.exceptions.CryptographyException; import org.apache.pdfbox.exceptions.InvalidPasswordException; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDDocumentCatalog; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDResources; import java.io.IOException; public class Main { public static void main(String[] argv) throws COSVisitorException, InvalidPasswordException, CryptographyException, IOException { PDDocument document = PDDocument.load("input.pdf"); if (document.isEncrypted()) { document.decrypt(""); } PDDocumentCatalog catalog = document.getDocumentCatalog(); for (Object pageObj : catalog.getAllPages()) { PDPage page = (PDPage) pageObj; PDResources resources = page.findResources(); resources.getImages().clear(); } document.save("strippedOfImages.pdf"); } }
// 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();
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(); }
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(); }
printer.processStream(page, page.findResources(), page.getContents().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; }
PDResources resources = page.findResources(); processStream( page, resources, page.getContents().getStream() ); List annotations = page.getAnnotations();
ocprops.addGroup(layer); PDResources resources = targetPage.findResources(); if(resources == null ) { resources = new PDResources(new COSDictionary());
/** * 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(); } } }
PDResources resources = page.findResources(); processStream( page, page.findResources(), content ); long stopProcess = System.currentTimeMillis(); long startFlush = System.currentTimeMillis();
PDResources resources = page.findResources();
PDResources resources = page.findResources();
if(pdResources == null) pdResources = page.findResources();
/** * Output a PDF with as many pages as there are interesting areas in the * input document */ @Override public PDDocument extract() throws IOException { PDDocument extractedDocument = new PDDocument(); extractedDocument.setDocumentInformation(sourceDocument.getDocumentInformation()); extractedDocument.getDocumentCatalog().setViewerPreferences( sourceDocument.getDocumentCatalog().getViewerPreferences()); @SuppressWarnings("unchecked") List<PDPage> pages = sourceDocument.getDocumentCatalog().getAllPages(); int pageCounter = 1; for (PDPage page : pages) { if (pageCounter >= startPage && pageCounter <= endPage) { List<PDRectangle> zoomedFragments = getFragments(page); for (PDRectangle fragment : zoomedFragments) { PDPage outputPage = extractedDocument.importPage(page); outputPage.setCropBox(fragment); outputPage.setMediaBox(page.getMediaBox()); outputPage.setResources(page.findResources()); outputPage.setRotation(page.findRotation()); // TODO: rotate the page in landscape mode is width > height } } pageCounter++; } return extractedDocument; }
if(pdResources == null) pdResources = page.findResources();