PageLabelUtils { public static PDPageLabels removePages(PDPageLabels pageLabels, List<Integer> pagesToRemove, int totalPages) { Map<Integer, PDPageLabelRange> labels = new TreeMap<>(pageLabels.getLabels()); List<Integer> pagesToRemoveSortedLastFirst = new ArrayList<>(pagesToRemove); pagesToRemoveSortedLastFirst.sort(Collections.reverseOrder()); for(int pageToRemove : pagesToRemoveSortedLastFirst) { Map<Integer, PDPageLabelRange> updatedLabels = new TreeMap<>(); int pageIndex = pageToRemove - 1; for(int key : labels.keySet()) { if(key <= pageIndex) { updatedLabels.put(key, labels.get(key)); } else if(key > pageIndex) { int prevKey = key - 1; if (prevKey >= 0) { updatedLabels.put(prevKey, labels.get(key)); } } } labels = updatedLabels; } int newTotalPages = totalPages - pagesToRemove.size(); PDPageLabels result = new PDPageLabels(); for(int index: labels.keySet()) { if(index < newTotalPages) { result.setLabelItem(index, labels.get(index)); } } return result; } private  PageLabelUtils(); static PDPageLabels getLabels(Map<Integer, PdfPageLabel> labels, int totalPages); static PDPageLabels removePages(PDPageLabels pageLabels, List<Integer> pagesToRemove, int totalPages);  }
@Test public void removeFirstPage() throws IOException { PDDocument doc = new DocBuilder().withPages(10) .withPageLabelRange(0, "r") .withPageLabelRange(2, "D") .get(); removePages(doc, asList(1), result -> { assertPageLabelIndexesAre(result, 0, 1); assertPageLabelRangeIs(result, 0, "r"); assertPageLabelRangeIs(result, 1, "D"); }); }
@Test public void removeFirstPages() throws IOException { PDDocument doc = new DocBuilder().withPages(10) .withPageLabelRange(0, "r") .withPageLabelRange(2, "D") .get(); removePages(doc, asList(1, 2), result -> { assertPageLabelIndexesAre(result, 0); assertPageLabelRangeIs(result, 0, "D"); }); }
@Test public void removeMiddlePage() throws IOException { PDDocument doc = new DocBuilder().withPages(10) .withPageLabelRange(0, "r") .withPageLabelRange(2, "D") .get(); removePages(doc, asList(2), result -> { assertPageLabelIndexesAre(result, 0, 1); assertPageLabelRangeIs(result, 0, "r"); assertPageLabelRangeIs(result, 1, "D"); }); }
@Test public void removeMiddlePages() throws IOException { PDDocument doc = new DocBuilder().withPages(10) .withPageLabelRange(0, "r") .withPageLabelRange(2, "D") .withPageLabelRange(4, "A") .get(); removePages(doc, asList(3, 4, 5), result -> { assertPageLabelIndexesAre(result, 0, 2); assertPageLabelRangeIs(result, 0, "r"); assertPageLabelRangeIs(result, 2, "A"); }); }
@Test public void removeLastPage() throws IOException { PDDocument doc = new DocBuilder().withPages(10) .withPageLabelRange(0, "r") .withPageLabelRange(2, "D") .get(); removePages(doc, asList(10), result -> { assertPageLabelIndexesAre(result, 0, 2); assertPageLabelRangeIs(result, 0, "r"); assertPageLabelRangeIs(result, 2, "D"); }); }
@Test public void removeLastPages() throws IOException { PDDocument doc = new DocBuilder().withPages(5) .withPageLabelRange(0, "r") .withPageLabelRange(2, "D") .get(); removePages(doc, asList(3, 4, 5), result -> { assertPageLabelIndexesAre(result, 0); assertPageLabelRangeIs(result, 0, "r"); }); }