I have a RadDocument that I am building up programatically. Within the document I have five Sections and in one of the section elements I have added a Footer and inserted a PageField to dynamically generate the page number during rendering of the document.
All of that is working and I am, so far, content. However...
I need the page numbers from that section for use in another section (building an index with key terms and references). I found the following code somewhere on the web but it isn't working properly for me...
For the first page when I make the call the ChildIndex is 3. I'm not sure if this is good or bad but it is most definitely not the solution since the first page of the document is only one page so I should be getting 1. I'm obviously doing *something* incorrectly but am not certain what that is.
Any insight is greatly appreciated!
rjsjr
var box = mergedDocument.CaretPosition.GetCurrentInlineBox();
if (box != null)
{
DocumentElement element = box.AssociatedDocumentElement;
var page = GetPageNumberForDocumentElement(element);
}
private int GetPageNumberForDocumentElement(DocumentElement element)
{
var layoutBox = GetSectionLayoutBox(element);
return layoutBox.ChildIndex + 1;
}
private LayoutBox GetSectionLayoutBox(DocumentElement element)
{
LayoutBox layoutBox = element.FirstLayoutBox;
while (!(layoutBox is SectionLayoutBox))
{
layoutBox = layoutBox.Parent;
}
return layoutBox;
}
All of that is working and I am, so far, content. However...
I need the page numbers from that section for use in another section (building an index with key terms and references). I found the following code somewhere on the web but it isn't working properly for me...
For the first page when I make the call the ChildIndex is 3. I'm not sure if this is good or bad but it is most definitely not the solution since the first page of the document is only one page so I should be getting 1. I'm obviously doing *something* incorrectly but am not certain what that is.
Any insight is greatly appreciated!
rjsjr
var box = mergedDocument.CaretPosition.GetCurrentInlineBox();
if (box != null)
{
DocumentElement element = box.AssociatedDocumentElement;
var page = GetPageNumberForDocumentElement(element);
}
private int GetPageNumberForDocumentElement(DocumentElement element)
{
var layoutBox = GetSectionLayoutBox(element);
return layoutBox.ChildIndex + 1;
}
private LayoutBox GetSectionLayoutBox(DocumentElement element)
{
LayoutBox layoutBox = element.FirstLayoutBox;
while (!(layoutBox is SectionLayoutBox))
{
layoutBox = layoutBox.Parent;
}
return layoutBox;
}