Skip to content

Commit 9a2202d

Browse files
Snipxitext-teamcity
authored andcommitted
PdfDocument#getPage(int) returns null in case of broken page tree
Autoported commit. Original commit hash: [5db36e2be]
1 parent 5d33394 commit 9a2202d

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

itext/itext.io/itext/io/LogMessageConstant.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ public sealed class LogMessageConstant {
180180

181181
public const String OPENTYPE_GDEF_TABLE_ERROR = "OpenType GDEF table error: {0}";
182182

183+
public const String PAGE_TREE_IS_BROKEN_FAILED_TO_RETRIEVE_PAGE = "Page tree is broken. Failed to retrieve page number {0}. Null will be returned.";
184+
183185
public const String PATH_KEY_IS_PRESENT_VERTICES_WILL_BE_IGNORED = "Path key is present. Vertices will be ignored";
184186

185187
public const String PDF_OBJECT_FLUSHING_NOT_PERFORMED = "PdfObject flushing is not performed: PdfDocument is opened in append mode and the object is not marked as modified ( see PdfObject#setModified() ).";

itext/itext.kernel/itext/kernel/pdf/PdfDocument.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,11 @@ public virtual int GetNumberOfPdfObjects() {
298298

299299
/// <summary>Gets the page by page number.</summary>
300300
/// <param name="pageNum">page number.</param>
301-
/// <returns>page by page number.</returns>
301+
/// <returns>
302+
/// page by page number. may return
303+
/// <see langword="null"/>
304+
/// in case the page tree is broken
305+
/// </returns>
302306
public virtual PdfPage GetPage(int pageNum) {
303307
CheckClosingStatus();
304308
return catalog.GetPageTree().GetPage(pageNum);

itext/itext.kernel/itext/kernel/pdf/PdfPagesTree.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,15 @@ public virtual PdfPage GetPage(int pageNum) {
118118
PdfPage pdfPage = pages[pageNum];
119119
if (pdfPage == null) {
120120
LoadPage(pageNum);
121-
pdfPage = new PdfPage(pageRefs[pageNum]);
122-
int parentIndex = FindPageParent(pageNum);
123-
PdfPages parentPages = parents[parentIndex];
124-
pdfPage.parentPages = parentPages;
121+
if (pageRefs[pageNum] != null) {
122+
int parentIndex = FindPageParent(pageNum);
123+
pdfPage = new PdfPage(pageRefs[pageNum]);
124+
pdfPage.parentPages = parents[parentIndex];
125+
}
126+
else {
127+
LogManager.GetLogger(GetType()).Error(MessageFormatUtil.Format(iText.IO.LogMessageConstant.PAGE_TREE_IS_BROKEN_FAILED_TO_RETRIEVE_PAGE
128+
, pageNum + 1));
129+
}
125130
pages[pageNum] = pdfPage;
126131
}
127132
return pdfPage;
@@ -357,7 +362,7 @@ private void LoadPage(int pageNum) {
357362
}
358363
PdfObject pageKids = page.Get(PdfName.Kids);
359364
if (pageKids != null) {
360-
if (pageKids.GetObjectType() == PdfObject.ARRAY) {
365+
if (pageKids.IsArray()) {
361366
findPdfPages = true;
362367
}
363368
else {

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
04d92262a715b272ebeba27d8cf01f6fcb793df6
1+
5db36e2be0d56e38c48b704886e0ceaf1149ff75

0 commit comments

Comments
 (0)