List of usage examples for org.apache.pdfbox.pdmodel PDDocument getPage
public PDPage getPage(int pageIndex)
From source file:merge_split.MergeSplit.java
License:Apache License
private void SplitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SplitButtonActionPerformed try {//from ww w . j a va 2 s . c o m File file = new File(SplitFileField.getText()); PDDocument doc1; if (splitcode.equals("ok")) { doc1 = PDDocument.load(file); } else { doc1 = PDDocument.load(file, splitcode); } doc1.setAllSecurityToBeRemoved(true); if (MultipleButton.isSelected()) { PDDocument pdf1 = new PDDocument(); PDDocument pdf2 = new PDDocument(); TreeSet tree = findPages(SplitPagesField.getText()); for (int j = 0; j < doc1.getNumberOfPages(); j++) { PDPage page = doc1.getPage(j); if (tree.contains(j + 1)) { pdf1.addPage(page); } else { pdf2.addPage(page); } } String destination1 = SplitDestinationField.getText() + "\\" + SplitNameField.getText() + "1.pdf"; String destination2 = SplitDestinationField.getText() + "\\" + SplitNameField.getText() + "2.pdf"; PDDocumentInformation info = pdf1.getDocumentInformation(); info.setAuthor(SplitAuthorField.getText()); PDDocumentInformation info2 = pdf2.getDocumentInformation(); info2.setAuthor(SplitAuthorField.getText()); if (pdf1.getNumberOfPages() > 0) { File output1 = new File(destination1); pdf1.save(output1); } if (pdf2.getNumberOfPages() > 0) { File output2 = new File(destination2); pdf2.save(output2); } pdf1.close(); pdf2.close(); } else if (SingleButton.isSelected()) { for (int j = 0; j < doc1.getNumberOfPages(); j++) { PDDocument pdf1 = new PDDocument(); PDPage page = doc1.getPage(j); pdf1.addPage(page); int pagenumber = j + 1; String destination1 = SplitDestinationField.getText() + "\\" + SplitNameField.getText() + pagenumber + ".pdf"; PDDocumentInformation info = pdf1.getDocumentInformation(); info.setAuthor(SplitAuthorField.getText()); if (pdf1.getNumberOfPages() > 0) { File output1 = new File(destination1); pdf1.save(output1); } pdf1.close(); } } doc1.close(); } catch (IOException ex) { JOptionPane.showMessageDialog(null, "Your input is incorrect. Please fill all the fields.", "Input warning", JOptionPane.WARNING_MESSAGE); java.util.logging.Logger.getLogger(MergeSplit.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } }
From source file:model.objects.Project.java
License:Apache License
/** * /*from w w w . j a v a 2 s . c om*/ * @param _doc * @param _bi * @param _pageindex index of page to which the BufferedImage is * inserted. * If it is equal to -1, new page is created. * */ public void attatchToPDF(final PDDocument _doc, final BufferedImage _bi, final int _pageindex) { PDPage page = null; try { if (_pageindex == -1) { page = new PDPage(new PDRectangle(State.getImageSize().width, State.getImageSize().height)); _doc.addPage(page); } else { page = _doc.getPage(_pageindex); // page.setCropBox(new PDRectangle(State.getImageSize().width , // State.getImageSize().height )); } int width = (int) page.getCropBox().getWidth(); int height = (int) page.getCropBox().getHeight(); PDImageXObject ximage = LosslessFactory.createFromImage(_doc, // _bi); Utils.resizeImage(width, height, _bi)); PDPageContentStream content = new PDPageContentStream(_doc, page, true, true); // contentStream.drawImage(ximage, 20, 20 ); // better method inspired by http://stackoverflow.com/a/22318681/535646 // reduce this value if the image is too large float scale = 1f; content.drawImage(ximage, 20, 20, ximage.getWidth() * scale, ximage.getHeight() * scale); content.close(); // LosslessFactory.createFromImage(doc, bim) // content.drawImage(ximage, 0, 0); // content.close(); } catch (IOException ie) { //handle exception } }
From source file:net.bookinaction.TextInfoExtractor.java
License:Apache License
public static void getTextPositionFromPage(PDDocument document, StripperParam stripperParam, int pageNum, PrintWriter writer, boolean testMode) throws IOException { //System.out.println(String.format("getPage: %d", pageNum)); PDPage page = document.getPage(pageNum - 1); // pdfbox uses the 0-base index PDRectangle cropBox = page.getCropBox(); // extract image locations ImageLocationListener imageLocationsListener = new ImageLocationListener(); List<Rectangle2D> imageRects = new ArrayList<Rectangle2D>(); imageLocationsListener.setImageRects(imageRects); imageLocationsListener.processPage(page); // extract Text locations StripString stripString = new StripString(); TextLocationListener stripper = new TextLocationListener(stripperParam, stripString); stripper.setSortByPosition(true);/*from www .j a v a 2 s .co m*/ List<StripLine> stripLines = new ArrayList<StripLine>(); stripper.setStartPage(pageNum); stripper.setEndPage(pageNum); try { stripper.writeText(document, new OutputStreamWriter(new ByteArrayOutputStream())); } catch (IOException e) { return; } if (page.getContents() != null) stripper.processPage(page); // declare canvas and keep this position PDPageContentStream canvas = new PDPageContentStream(document, page, true, true, true); Stamper s = new Stamper(); // utility class if (testMode) { // draw the bounding box of each character for (int i = 0; i < stripString.size(); i++) { // original Rectangle s.showBox(canvas, stripString.boundingRect(i), cropBox, Color.GRAY80); } } s.recordPageSize(writer, pageNum, cropBox); // splits into lines int lineNum = 1; int lineStart = 0, lineEnd = 0; String[] splits = stripString.toString().split("\r"); SimpleTokenizer simpleTokenizer = new SimpleTokenizer(); for (String lineText : splits) { if (lineText.length() < 1) continue; lineEnd = lineStart + lineText.length(); Rectangle2D mergedRect = stripString.boundingRect(lineStart, lineEnd - 1); String sub = stripString.substring(lineStart, lineEnd); stripLines.add(new StripLine(pageNum, lineNum, lineStart, lineEnd, mergedRect)); //System.out.println(String.format("%d-%d: %s - [%.0f %.0f %.0f %.0f]", pageNum, lineNum, sub, // mergedRect.getX(), mergedRect.getY(), mergedRect.getWidth(), mergedRect.getHeight())); if (testMode) { s.showBox(canvas, mergedRect, cropBox, Color.GREEN); } s.recordTextPosition(writer, sub, pageNum, mergedRect, "LINE"); /******* get words in the line *********/ List<Token> tokens = simpleTokenizer.getTokens(sub); for (String pattern : circles_patterns) { List<Token> symbolTokens = PatternAnalyzer.getTokensByPattern(sub, pattern); tokens.addAll(symbolTokens); } for (Token t : tokens) { mergedRect = stripString.boundingRect(lineStart + t.getStart(), lineStart + t.getEnd() - 1); //System.out.println(String.format("%d-%d: %s - [%.0f %.0f %.0f %.0f]", pageNum, lineNum, t.getStem(), mergedRect.getX(), mergedRect.getY(), mergedRect.getWidth(), mergedRect.getHeight())); s.recordTextPosition(writer, t.getStem(), pageNum, mergedRect, "TEXT"); if (testMode) { s.showBox(canvas, mergedRect, cropBox, Color.RED); } } lineStart += lineText.length() + 1; lineNum++; } // ------------------- // markup textMark annotation to the image int imageNum = 1; for (Rectangle2D imRect : imageRects) { //page.getAnnotations().add(annotationMaker.textMarkupAnnotation(Color.YELLOW, (Rectangle2D.Float) imRect, "image"+imageNum)); if (testMode) { s.showBox(canvas, imRect, cropBox, Color.YELLOW); } s.recordTextPosition(writer, "[image" + imageNum + "]", pageNum, imRect, "IMAGE"); imageNum++; } canvas.close(); }
From source file:org.apache.fop.render.pdf.PageParentTreeFinderTestCase.java
License:Apache License
@Test public void testGetPageParentTreeArray() throws IOException { File resource = new File(getClass().getResource(LINK).getFile()); PDDocument doc = PDDocument.load(resource); PDPage srcPage = doc.getPage(0); PageParentTreeFinder finder = new PageParentTreeFinder(srcPage); COSArray markedContentParents = finder.getPageParentTreeArray(doc); Assert.assertEquals(markedContentParents.size(), 3); COSObject firstObj = (COSObject) markedContentParents.get(0); COSObject secObj = (COSObject) markedContentParents.get(1); COSArray firstKids = (COSArray) firstObj.getDictionaryObject(COSName.K); COSDictionary firstKid = (COSDictionary) firstKids.get(0); int test = firstKid.getInt("MCID"); int expected = 0; Assert.assertEquals(test, expected); COSDictionary firstKidBrother = (COSDictionary) firstKids.get(2); test = firstKidBrother.getInt("MCID"); expected = 2;/*from w w w .ja v a2 s. c om*/ Assert.assertEquals(test, expected); COSArray secKidsArray = (COSArray) secObj.getDictionaryObject(COSName.K); COSDictionary secondKid = (COSDictionary) secKidsArray.get(0); test = secondKid.getInt("MCID"); expected = 1; Assert.assertEquals(test, expected); }
From source file:org.apache.fop.render.pdf.StructureTreeMergerTestCase.java
License:Apache License
@Test public void testCopyStructure() throws IOException { setUp();/*from ww w .jav a 2 s .c o m*/ PDDocument doc = PDDocument.load(new File(getClass().getResource(LINK).getFile())); PDPage srcPage = doc.getPage(0); PageParentTreeFinder finder = new PageParentTreeFinder(srcPage); COSArray markedContentParents = finder.getPageParentTreeArray(doc); PDFStructElem elem = new PDFStructElem(); elem.setObjectNumber(2); adapter = new PDFBoxAdapter(pdfPage, new HashMap(), new HashMap<Integer, PDFArray>()); adapter.setCurrentMCID(1); PDFLogicalStructureHandler handler = setUpPDFLogicalStructureHandler(); StructureTreeMerger merger = new StructureTreeMerger(elem, handler, adapter, srcPage); merger.copyStructure(markedContentParents); PDFArray array = handler.getPageParentTree(); checkMarkedContentsParentsForLinkTest(array); PDFStructElem first = (PDFStructElem) array.get(0); checkParentForLinkTest(first, 0); }
From source file:org.apache.fop.render.pdf.StructureTreeMergerTestCase.java
License:Apache License
@Test public void testNullEntriesInParentTree() throws IOException { setUp();// ww w . jav a 2s. c o m PDDocument doc = PDDocument.load(new File(getClass().getResource(LINK).getFile())); PDPage srcPage = doc.getPage(0); PageParentTreeFinder finder = new PageParentTreeFinder(srcPage); COSArray markedContentParents = finder.getPageParentTreeArray(doc); markedContentParents.add(0, null); PDFStructElem elem = new PDFStructElem(); elem.setObjectNumber(2); adapter = new PDFBoxAdapter(pdfPage, new HashMap(), new HashMap<Integer, PDFArray>()); PDFLogicalStructureHandler handler = setUpPDFLogicalStructureHandler(); StructureTreeMerger merger = new StructureTreeMerger(elem, handler, adapter, srcPage); merger.copyStructure(markedContentParents); PDFArray array = handler.getPageParentTree(); Assert.assertNull(array.get(0)); }
From source file:org.apache.fop.render.pdf.StructureTreeMergerTestCase.java
License:Apache License
@Test public void testOBJRCorrectPosition() throws IOException { setUp();/*from w ww . j a va2 s. com*/ PDDocument doc = PDDocument.load(new File(getClass().getResource(MissingOBJR).getFile())); PDPage srcPage = doc.getPage(0); PageParentTreeFinder finder = new PageParentTreeFinder(srcPage); COSArray markedContentParents = finder.getPageParentTreeArray(doc); PDFStructElem elem = new PDFStructElem(); elem.setObjectNumber(2); adapter = new PDFBoxAdapter(pdfPage, new HashMap(), new HashMap<Integer, PDFArray>()); PDFLogicalStructureHandler handler = setUpPDFLogicalStructureHandler(); StructureTreeMerger merger = new StructureTreeMerger(elem, handler, adapter, srcPage); merger.copyStructure(markedContentParents); // PDFArray array = handler.getPageParentTree(); // PDFStructElem kid = (PDFStructElem)array.get(0); // PDFReference reference = (PDFReference) kid.get("P"); // PDFStructElem parent = (PDFStructElem)reference.getObject(); // List<PDFObject> kids = parent.getKids(); // PDFDictionary first = (PDFDictionary) kids.get(0); // Assert.assertEquals(first.get("Type").toString(), "/OBJR"); // PDFDictionary last = (PDFDictionary) kids.get(2); // Assert.assertEquals(last.get("Type").toString(), "/OBJR"); // PDFStructElem middle = (PDFStructElem) kids.get(1); // Assert.assertEquals(middle.get("Type").toString(), "/StructElem"); }
From source file:org.apache.fop.render.pdf.StructureTreeMergerTestCase.java
License:Apache License
@Test public void testCheckNullCOSObject() throws IOException { setUp();//from ww w . j a v a 2 s . co m PDDocument doc = PDDocument.load(new File(getClass().getResource(BrokenLink).getFile())); PDPage srcPage = doc.getPage(0); PageParentTreeFinder finder = new PageParentTreeFinder(srcPage); COSArray markedContentParents = finder.getPageParentTreeArray(doc); COSObject nullObj = new COSObject(null); nullObj.setObjectNumber(100); nullObj.setGenerationNumber(0); PDFStructElem elem = new PDFStructElem(); elem.setObjectNumber(2); COSObject parent = (COSObject) markedContentParents.get(1); COSArray kids = (COSArray) parent.getDictionaryObject(COSName.K); COSDictionary kid = (COSDictionary) kids.get(1); kid.setItem(COSName.OBJ, nullObj); adapter = new PDFBoxAdapter(pdfPage, new HashMap(), new HashMap<Integer, PDFArray>()); PDFLogicalStructureHandler handler = setUpPDFLogicalStructureHandler(); StructureTreeMerger merger = new StructureTreeMerger(elem, handler, adapter, srcPage); merger.copyStructure(markedContentParents); PDFArray array = handler.getPageParentTree(); PDFStructElem parentElem = (PDFStructElem) array.get(1); PDFDictionary objrDict = (PDFDictionary) parentElem.getKids().get(1); Assert.assertNull(objrDict.get("Obj")); }
From source file:org.apache.fop.render.pdf.TaggedPDFConductorTestCase.java
License:Apache License
private void runConductor(String pdf, PDFStructElem elem) throws IOException { setUp();//w w w .j a va 2 s .c om PDDocument doc = PDDocument.load(new File(getClass().getResource(pdf).getFile())); PDPage srcPage = doc.getPage(0); elem.setObjectNumber(2); PDFBoxAdapter adapter = new PDFBoxAdapter(pdfPage, new HashMap(), new HashMap<Integer, PDFArray>()); PDFLogicalStructureHandler handler = setUpPDFLogicalStructureHandler(); new TaggedPDFConductor(elem, handler, srcPage, adapter).handleLogicalStructure(doc); }
From source file:org.esteco.jira.pdf.AddImageToPDF.java
License:Apache License
/** * Add an image to an existing PDF document. * * @param inputFile The input PDF to add the image to. * @param imagePath The filename of the image to put in the PDF. * @param outputFile The file to write to the pdf to. * @throws IOException If there is an error writing the data. *///from w w w. j a v a 2 s .c o m public void createPDFFromImage(String inputFile, String imagePath, String outputFile) throws IOException { // the document PDDocument doc = null; try { doc = PDDocument.load(new File(inputFile)); //we will add the image to the first page. PDPage page = doc.getPage(0); //page.setRotation(90); // 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); PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, true); // contentStream.drawImage(ximage, 20, 20 ); // better method inspired by http://stackoverflow.com/a/22318681/535646 // reduce this value if the image is too large float scale = 0.4f; contentStream.drawImage(pdImage, 20, 20, pdImage.getWidth() * scale, pdImage.getHeight() * scale); contentStream.close(); doc.save(outputFile); } finally { if (doc != null) { doc.close(); } } }