@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"); }); }
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; }
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; } }
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(); }
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); }
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);  }