PdfPages extends PdfObjectWrapper<PdfDictionary> { public boolean removePage(int pageNum) { if (pageNum < from || pageNum >= from + getCount()) return false; decrementCount(); kids.remove(pageNum - from); return true; } PdfPages(int from, PdfDocument pdfDocument, PdfPages parent);  PdfPages(int from, PdfDocument pdfDocument);  PdfPages(int from, int maxCount, PdfDictionary pdfObject, PdfPages parent); void addPage(PdfDictionary page); boolean addPage(int index, PdfPage pdfPage); boolean removePage(int pageNum); void addPages(PdfPages pdfPages); void removeFromParent(); int getFrom(); int getCount(); void correctFrom(int correction); PdfArray getKids(); PdfPages getParent(); void incrementCount(); void decrementCount(); int compareTo(int index);  }
@Test @LogMessages(messages = { @LogMessage(messageTemplate = LogMessageConstant.REMOVING_PAGE_HAS_ALREADY_BEEN_FLUSHED, count = 2) }) public void removeFlushedPage() throws IOException { String filename = "removeFlushedPage.pdf"; int pageCount = 10; PdfDocument pdfDoc = new PdfDocument(new PdfWriter(destinationFolder + filename)); PdfPage removedPage = pdfDoc.addNewPage(); int removedPageObjectNumber = removedPage.getPdfObject().getIndirectReference().getObjNumber(); removedPage.flush(); pdfDoc.removePage(removedPage); for (int i = 0; i < pageCount; i++) { PdfPage page = pdfDoc.addNewPage(); page.getPdfObject().put(PageNum, new PdfNumber(i + 1)); page.flush(); } Assert.assertTrue("Remove last page", pdfDoc.removePage(pdfDoc.getPage(pageCount))); Assert.assertFalse("Free reference", pdfDoc.getXref().get(removedPageObjectNumber).checkState(PdfObject.FREE)); pdfDoc.close(); verifyPagesOrder(destinationFolder + filename, pageCount - 1); }
@Test public void removeFlushedPageFromTaggedDocument() { try (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()))) { pdfDocument.setTagged(); pdfDocument.addNewPage(); pdfDocument.getPage(1).flush(); junitExpectedException.expect(PdfException.class); junitExpectedException.expectMessage(PdfException.FLUSHED_PAGE_CANNOT_BE_REMOVED); pdfDocument.removePage(1); } }
@Test public void removeFlushedPageFromDocumentWithAcroForm() { try (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()))) { pdfDocument.getCatalog().put(PdfName.AcroForm, new PdfDictionary()); pdfDocument.addNewPage(); pdfDocument.getPage(1).flush(); junitExpectedException.expect(PdfException.class); junitExpectedException.expectMessage(PdfException.FLUSHED_PAGE_CANNOT_BE_REMOVED); pdfDocument.removePage(1); } }
@Test public void reorderInheritedResourcesTest() throws IOException, InterruptedException { PdfDocument pdfDoc = new PdfDocument( new PdfReader(sourceFolder + "inheritedFontResources.pdf"), new PdfWriter(destinationFolder + "reorderInheritedFontResources.pdf") ); pdfDoc.movePage(1, pdfDoc.getNumberOfPages() + 1); pdfDoc.removePage(1); pdfDoc.close(); String compareResult = new CompareTool().compareByContent( destinationFolder + "reorderInheritedFontResources.pdf", sourceFolder + "cmp_reorderInheritedFontResources.pdf", destinationFolder, "diff_reorderInheritedFontResources_"); Assert.assertNull(compareResult); }
@Test public void removePageWithFormFieldsTest() throws IOException { String filename = sourceFolder + "docWithFields.pdf"; PdfDocument pdfDoc = new PdfDocument(new PdfReader(filename)); pdfDoc.removePage(1); PdfArray fields = pdfDoc.getCatalog().getPdfObject().getAsDictionary(PdfName.AcroForm) .getAsArray(PdfName.Fields); PdfDictionary field = (PdfDictionary) fields.get(0); PdfDictionary kid = (PdfDictionary) field.getAsArray(PdfName.Kids).get(0); Assert.assertEquals(6, kid.keySet().size()); Assert.assertEquals(3, fields.size()); pdfDoc.close(); }