private void removePageTagFromParent(IStructureNode pageTag, IStructureNode parent) { if (parent instanceof PdfStructElem) { PdfStructElem structParent = (PdfStructElem) parent; if (!structParent.isFlushed()) { structParent.removeKid(pageTag); PdfDictionary parentStructDict = structParent.getPdfObject(); if (waitingTagsManager.getObjForStructDict(parentStructDict) == null && parent.getKids().size() == 0 && !(structParent.getParent() instanceof PdfStructTreeRoot)) { removePageTagFromParent(structParent, parent.getParent()); PdfIndirectReference indRef = parentStructDict.getIndirectReference(); if (indRef != null) { // TODO how about possible references to structure element from refs or structure destination for instance? indRef.setFree(); } } } else { if (pageTag instanceof PdfMcr) { throw new PdfException(PdfException.CannotRemoveTagBecauseItsParentIsFlushed); } } } else { // it is StructTreeRoot // should never happen as we always should have only one root tag and we don't remove it } }
/** * Removes page from the document by page number. * * @param pageNum the one-based index of the PdfPage to be removed */ public void removePage(int pageNum) { checkClosingStatus(); PdfPage removedPage = catalog.getPageTree().removePage(pageNum); if (removedPage != null) { catalog.removeOutlines(removedPage); removeUnusedWidgetsFromFields(removedPage); if (isTagged()) { getTagStructureContext().removePageTags(removedPage); } // TODO should we remove everything (outlines, tags) if page won't be removed in the end, because it's already flushed? wouldn't tags be also flushed? if (!removedPage.getPdfObject().isFlushed()) { removedPage.getPdfObject().remove(PdfName.Parent); removedPage.getPdfObject().getIndirectReference().setFree(); } dispatchEvent(new PdfDocumentEvent(PdfDocumentEvent.REMOVE_PAGE, removedPage)); } }
if (indRef != null) { indRef.setFree();