Skip to content

Commit 7145131

Browse files
committed
Implement more optimized version of checking if kid is flushed
DEVSIX-8836
1 parent 749a16f commit 7145131

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

kernel/src/main/java/com/itextpdf/kernel/pdf/tagging/PdfStructElem.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,27 @@ public List<IStructureNode> getKids() {
336336
return kids;
337337
}
338338

339+
/**
340+
* Checks if the kid with the given index is flushed.
341+
*
342+
* @param index index of the kid to check.
343+
* @return {@code true} if the kid with the given index is flushed, {@code false} otherwise.
344+
*/
345+
public boolean isKidFlushed(int index) {
346+
PdfObject k = getK();
347+
if (k == null) {
348+
return false;
349+
}
350+
if (k.isArray()) {
351+
PdfArray array = (PdfArray) k;
352+
if (index >= array.size()) {
353+
return false;
354+
}
355+
return array.get(index).isFlushed();
356+
}
357+
return index == 0 && k.isFlushed();
358+
}
359+
339360
public PdfObject getK() {
340361
return getPdfObject().get(PdfName.K);
341362
}

kernel/src/main/java/com/itextpdf/kernel/pdf/tagutils/TagTreePointer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ public TagTreePointer relocateKid(int kidIndex, TagTreePointer pointerToNewParen
389389
pointerToNewParent.setNextNewKidIndex(pointerToNewParent.nextNewKidIndex - 1);
390390
}
391391
}
392-
if (getCurrentStructElem().getKids().get(kidIndex) == null) {
392+
if (getCurrentStructElem().isKidFlushed(kidIndex)) {
393393
throw new PdfException(KernelExceptionMessageConstant.CANNOT_RELOCATE_TAG_WHICH_IS_ALREADY_FLUSHED);
394394
}
395395
IStructureNode removedKid = getCurrentStructElem().removeKid(kidIndex, true);

0 commit comments

Comments
 (0)