public override VisitorAction VisitParagraphStart(Paragraph paragraph)
{
if (paragraph.IsListItem)
{
List paraList = paragraph.ListFormat.List;
ListLevel currentLevel = paragraph.ListFormat.ListLevel;
// Since we have encountered a list item we need to check if this will reset
// Any subsequent list levels and if so then update the numbering of the level.
int currentListLevelNumber = paragraph.ListFormat.ListLevelNumber;
for (int i = currentListLevelNumber + 1; i < paraList.ListLevels.Count; i++)
{
ListLevel paraLevel = paraList.ListLevels[i];
if (paraLevel.RestartAfterLevel >= currentListLevelNumber)
{
// This list level needs to be reset after the current list number.
mListLevelToListNumberLookup[paraLevel] = paraLevel.StartAt;
}
}
// A list which was used on a previous page is present on a different page, the list
// Needs to be copied so list numbering is retained when extracting individual pages.
if (ContainsListLevelAndPageChanged(paragraph))
{
List copyList = paragraph.Document.Lists.AddCopy(paraList);
mListLevelToListNumberLookup[currentLevel] = paragraph.ListLabel.LabelValue;
// Set the numbering of each list level to start at the numbering of the level on the previous page.
for (int i = 0; i < paraList.ListLevels.Count; i++)
{
ListLevel paraLevel = paraList.ListLevels[i];
if (mListLevelToListNumberLookup.ContainsKey(paraLevel))
copyList.ListLevels[i].StartAt = (int)mListLevelToListNumberLookup[paraLevel];
}
mListToReplacementListLookup[paraList] = copyList;
}
if (mListToReplacementListLookup.ContainsKey(paraList))
{
// This paragraph belongs to a list from a previous page. Apply the replacement list.
paragraph.ListFormat.List = (List)mListToReplacementListLookup[paraList];
// This is a trick to get the spacing of the list level to set correctly.
paragraph.ListFormat.ListLevelNumber += 0;
}
mListLevelToPageLookup[currentLevel] = mPageNumberFinder.GetPage(paragraph);
mListLevelToListNumberLookup[currentLevel] = paragraph.ListLabel.LabelValue;
}
Section prevSection = (Section)paragraph.ParentSection.PreviousSibling;
Paragraph prevBodyPara = paragraph.PreviousSibling as Paragraph;
Paragraph prevSectionPara = prevSection != null && paragraph == paragraph.ParentSection.Body.FirstChild ? prevSection.Body.LastParagraph : null;
Paragraph prevParagraph = prevBodyPara != null ? prevBodyPara : prevSectionPara;
if (paragraph.IsEndOfSection && !paragraph.HasChildNodes)
paragraph.Remove();
// Paragraphs across pages can merge or remove spacing depending upon the previous paragraph.
if (prevParagraph != null)
{
if (mPageNumberFinder.GetPage(paragraph) != mPageNumberFinder.GetPageEnd(prevParagraph))
{
if (paragraph.IsListItem && prevParagraph.IsListItem && !prevParagraph.IsEndOfSection)
prevParagraph.ParagraphFormat.SpaceAfter = 0;
else if (prevParagraph.ParagraphFormat.StyleName == paragraph.ParagraphFormat.StyleName && paragraph.ParagraphFormat.NoSpaceBetweenParagraphsOfSameStyle)
paragraph.ParagraphFormat.SpaceBefore = 0;
else if (paragraph.ParagraphFormat.PageBreakBefore || (prevParagraph.IsEndOfSection && prevSection.PageSetup.SectionStart != SectionStart.NewColumn))
paragraph.ParagraphFormat.SpaceBefore = System.Math.Max(paragraph.ParagraphFormat.SpaceBefore - prevParagraph.ParagraphFormat.SpaceAfter, 0);
else
paragraph.ParagraphFormat.SpaceBefore = 0;
}
}
return VisitorAction.Continue;
}