List of usage examples for org.apache.pdfbox.pdmodel PDDocument getDocumentCatalog
public PDDocumentCatalog getDocumentCatalog()
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();/* w w w.ja v a 2 s. co m*/ 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); } }
From source file:utilities.PDFUtilities.java
public static ArrayList<BufferedImage> extractPagesAsImage(File file) throws Exception { ArrayList<BufferedImage> images = new ArrayList<>(); if (file != null) { PDDocument doc = PDDocument.load(file); List<PDPage> pages = doc.getDocumentCatalog().getAllPages(); for (PDPage page : pages) { BufferedImage bImage = page.convertToImage(BufferedImage.TYPE_INT_RGB, 160); images.add(bImage);/* w ww.ja va 2 s . c o m*/ } } return images; }
From source file:vortext.TextHighlight.java
License:Apache License
@SuppressWarnings("unchecked") public void initialize(final PDDocument pdf) throws IOException { resetEngine();/* w w w . j a v a 2s . co m*/ document = pdf; if (document.isEncrypted()) { // We are expecting non-encrypted documents here, but it is common // for users to pass in a document that is encrypted with an empty // password (such a document appears to not be encrypted by // someone viewing the document, thus the confusion). We will // attempt to decrypt with the empty password to handle this case. // try { document.decrypt(""); } catch (CryptographyException e) { throw new IllegalArgumentException("Error decrypting document, details: ", e); } } textAggregate = new TextAggregate(this.isSkipAllWhitespace(), this.isNormalizeText()); if (getAddMoreFormatting()) { setParagraphEnd(getLineSeparator()); setPageStart(getLineSeparator()); setArticleStart(getLineSeparator()); setArticleEnd(getLineSeparator()); } startDocument(pdf); processPages(pdf.getDocumentCatalog().getAllPages()); endDocument(pdf); }
From source file:webhooks.core.services.util.ThumbnailUtil.java
License:Apache License
private static BufferedImage createPDFThumbnail(String filename, int thumbWidth, int thumbHeight) throws IOException { PDDocument document = PDDocument.load(new File(filename)); List<?> pages = document.getDocumentCatalog().getAllPages(); // get first page PDPage page = (PDPage) pages.get(0); PDRectangle mBox = page.findMediaBox(); float scaling = thumbWidth / mBox.getWidth(); Dimension pageDimension = new Dimension((int) mBox.getWidth(), (int) mBox.getHeight()); BufferedImage thumbImg = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = (Graphics2D) thumbImg.getGraphics(); graphics.setBackground(new Color(255, 255, 255, 0)); // TRANSPARENT_WHITE graphics.clearRect(0, 0, thumbImg.getWidth(), thumbImg.getHeight()); graphics.scale(scaling, scaling);//from w w w . j a v a 2 s .c om PageDrawer drawer = new PageDrawer(); drawer.drawPage(graphics, page, pageDimension); return thumbImg; }