List of usage examples for org.apache.pdfbox.pdmodel PDPageTree indexOf
public int indexOf(PDPage page)
From source file:com.repeatability.pdf.PDFTextStripper.java
License:Apache License
/** * This will process all of the pages and the text that is in them. * * @param pages The pages object in the document. * * @throws IOException If there is an error parsing the text. *//*from w w w . j a v a2 s . c o m*/ protected void processPages(PDPageTree pages) throws IOException { PDPage startBookmarkPage = startBookmark == null ? null : startBookmark.findDestinationPage(document); if (startBookmarkPage != null) { startBookmarkPageNumber = pages.indexOf(startBookmarkPage) + 1; } else { // -1 = undefined startBookmarkPageNumber = -1; } PDPage endBookmarkPage = endBookmark == null ? null : endBookmark.findDestinationPage(document); if (endBookmarkPage != null) { endBookmarkPageNumber = pages.indexOf(endBookmarkPage) + 1; } else { // -1 = undefined endBookmarkPageNumber = -1; } if (startBookmarkPageNumber == -1 && startBookmark != null && endBookmarkPageNumber == -1 && endBookmark != null && startBookmark.getCOSObject() == endBookmark.getCOSObject()) { // this is a special case where both the start and end bookmark // are the same but point to nothing. In this case // we will not extract any text. startBookmarkPageNumber = 0; endBookmarkPageNumber = 0; } for (PDPage page : pages) { currentPageNo++; if (page.hasContents()) { processPage(page); } } }
From source file:org.pdfmetamodifier.OutlineHelper.java
License:Apache License
private static Integer getOutlinesPageNumber(final PDPageDestination pageDestination, final PDPageTree pages) { return pages.indexOf(pageDestination.getPage()) + 1; }
From source file:org.pdfmetamodifier.OutlineHelperTest.java
License:Apache License
/** * Test for {@link OutlineHelper#lineListToOutlines(org.apache.pdfbox.pdmodel.PDPageTree, java.util.List)} and * {@link OutlineHelper#outlinesToLineList(org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline, org.apache.pdfbox.pdmodel.PDPageTree, org.apache.pdfbox.pdmodel.PDDestinationNameTreeNode)}. * //from ww w.j a v a 2 s .c om * @throws IOException */ @Test public void lineListToOutlinesAndBack() throws IOException { final List<String> lineList = new ArrayList<>(); lineList.add("Bookmarks"); lineList.add(" Title 1|1"); lineList.add(" Title 1.1|2"); lineList.add(" Title 1.2|3"); lineList.add(""); lineList.add(" Title 2|5"); lineList.add(" Title 2.1|6"); lineList.add(" Title 2.2|7"); lineList.add(""); lineList.add(" Title 3|9"); lineList.add(" Title 3.1|10"); lineList.add(" Title 3.2|11"); lineList.add(""); final List<String> cleanLineList = new ArrayList<>(); for (String line : lineList) { if (!line.isEmpty()) { cleanLineList.add(line); } } final PDPageTree pageTree = mock(PDPageTree.class); final List<PDPage> mockPages = new ArrayList<>(); when(pageTree.get(anyInt())).then(new Answer<PDPage>() { @Override public PDPage answer(final InvocationOnMock invocation) throws Throwable { final int idx = (int) invocation.getArguments()[0]; for (int i = mockPages.size(); i <= idx; ++i) { mockPages.add(new PDPage()); } return mockPages.get(idx); } }); when(pageTree.indexOf(any(PDPage.class))).then(new Answer<Integer>() { @Override public Integer answer(final InvocationOnMock invocation) throws Throwable { final PDPage page = (PDPage) invocation.getArguments()[0]; return mockPages.indexOf(page); } }); final PDDocumentOutline documentOutline = OutlineHelper.lineListToOutlines(pageTree, lineList); final List<String> resultLineList = OutlineHelper.outlinesToLineList(documentOutline, pageTree, null); assertEquals(cleanLineList.size(), resultLineList.size()); for (int i = 0; i < cleanLineList.size(); ++i) { assertEquals(cleanLineList.get(i), resultLineList.get(i)); } }