diff --git a/src/UglyToad.PdfPig/Content/PageTreeNode.cs b/src/UglyToad.PdfPig/Content/PageTreeNode.cs
index 2b48d303..7215372a 100644
--- a/src/UglyToad.PdfPig/Content/PageTreeNode.cs
+++ b/src/UglyToad.PdfPig/Content/PageTreeNode.cs
@@ -32,7 +32,7 @@
///
/// The number of this page if is .
///
- public int? PageNumber { get; }
+ public int? PageNumber { get; internal set; }
///
/// The child nodes of this node if is
diff --git a/src/UglyToad.PdfPig/Parser/CatalogFactory.cs b/src/UglyToad.PdfPig/Parser/CatalogFactory.cs
index d6e56074..da2bb2cc 100644
--- a/src/UglyToad.PdfPig/Parser/CatalogFactory.cs
+++ b/src/UglyToad.PdfPig/Parser/CatalogFactory.cs
@@ -5,8 +5,10 @@
using Content;
using Core;
using Parts;
+ using System.Linq;
using Tokenization.Scanner;
using Tokens;
+ using Util;
internal static class CatalogFactory
{
@@ -80,6 +82,8 @@
return new PageTreeNode(nodeDictionaryInput, referenceInput, true, pageNumber.PageCount).WithChildren(EmptyArray.Instance);
}
+
+
//If we got here, we have to iterate till we manage to exit
@@ -126,8 +130,6 @@
if (isChildPage)
{
- pageNumber.Increment();
-
var kidPageNode =
new PageTreeNode(kidDictionaryToken, kidRef.Data, true, pageNumber.PageCount).WithChildren(EmptyArray.Instance);
current.nodeChildren.Add(kidPageNode);
@@ -152,6 +154,12 @@
action();
}
+ foreach (var child in firstPage.Children.ToRecursiveOrderList(x=>x.Children).Where(child => child.IsPage))
+ {
+ pageNumber.Increment();
+ child.PageNumber = pageNumber.PageCount;
+ }
+
return firstPage;
}
diff --git a/src/UglyToad.PdfPig/Util/EnumerableExtensions.cs b/src/UglyToad.PdfPig/Util/EnumerableExtensions.cs
new file mode 100644
index 00000000..1d56d6bd
--- /dev/null
+++ b/src/UglyToad.PdfPig/Util/EnumerableExtensions.cs
@@ -0,0 +1,44 @@
+namespace UglyToad.PdfPig.Util
+{
+ using System;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Linq.Expressions;
+ using System.Reflection;
+
+
+ internal static class EnumerableExtensions
+ {
+ public static List ToRecursiveOrderList(this IEnumerable collection,
+ Expression>> childCollection)
+ {
+ var resultList = new List();
+ var currentItems = new Queue<(int Index, T Item, int Depth)>(collection.Select(i => (0, i, 0)));
+ var depthItemCounter = 0;
+ var previousItemDepth = 0;
+ var childProperty = (PropertyInfo)((MemberExpression)childCollection.Body).Member;
+ while (currentItems.Count > 0)
+ {
+ var currentItem = currentItems.Dequeue();
+ // Reset counter for number of items at this depth when the depth changes.
+ if (currentItem.Depth != previousItemDepth)
+ {
+ depthItemCounter = 0;
+ }
+
+ var resultIndex = currentItem.Index + depthItemCounter++;
+ resultList.Insert(resultIndex, currentItem.Item);
+
+ var childItems = childProperty.GetValue(currentItem.Item) as IEnumerable ?? Enumerable.Empty();
+ foreach (var childItem in childItems)
+ {
+ currentItems.Enqueue((resultIndex + 1, childItem, currentItem.Depth + 1));
+ }
+
+ previousItemDepth = currentItem.Depth;
+ }
+
+ return resultList;
+ }
+ }
+}
\ No newline at end of file