Skip to content

Commit c5e355e

Browse files
committed
Modified the code
1 parent 71477c9 commit c5e355e

1 file changed

Lines changed: 31 additions & 27 deletions

File tree

  • Word-document/Insert-PageBreak-Before-Heading-Paragraphs/.NET/Insert-PageBreak-Before-Heading-Paragraphs

Word-document/Insert-PageBreak-Before-Heading-Paragraphs/.NET/Insert-PageBreak-Before-Heading-Paragraphs/Program.cs

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,45 @@ public static void Main(string[] args)
1313

1414
if (entities == null)
1515
{
16-
Console.WriteLine("No paragraphs with the style 'Heading 1' found.");
17-
return;
16+
Console.WriteLine("No paragraphs with the style 'Heading 1' found.");
1817
}
19-
foreach (Entity entity in entities)
18+
else
2019
{
21-
// Check if the entity is a paragraph
22-
WParagraph paragraph = entity as WParagraph;
23-
// Get the index of the current paragraph in its parent text body
24-
int index = paragraph.OwnerTextBody.ChildEntities.IndexOf(paragraph);
25-
// Continue only if there is a previous entity
26-
if (index > 0)
20+
foreach (Entity entity in entities)
2721
{
28-
// Get the previous entity and cast it to a paragraph
29-
WParagraph previousParagraph = paragraph.OwnerTextBody.ChildEntities[index - 1] as WParagraph;
30-
bool hasPageBreak = false;
31-
// Check if the previous paragraph ends with a page break
32-
if (previousParagraph != null && previousParagraph.ChildEntities.Count > 0)
22+
// Check if the entity is a paragraph
23+
WParagraph paragraph = entity as WParagraph;
24+
// Get the index of the current paragraph in its parent text body
25+
int index = paragraph.OwnerTextBody.ChildEntities.IndexOf(paragraph);
26+
// Continue only if there is a previous entity
27+
if (index > 0)
3328
{
34-
// Get the last item in the previous paragraph
35-
ParagraphItem lastItem = previousParagraph.ChildEntities[previousParagraph.ChildEntities.Count - 1] as ParagraphItem;
36-
// Enable the boolean if the last item is a page break
37-
if (lastItem != null && lastItem is Break && (lastItem as Break).BreakType == BreakType.PageBreak)
38-
hasPageBreak = true;
39-
}
40-
// If no page break is found, insert the page break before the current paragraph
41-
if (!hasPageBreak)
42-
{
43-
WParagraph newPara = new WParagraph(document);
44-
newPara.AppendBreak(BreakType.PageBreak);
45-
// Insert the new paragraph with page break at the correct position
46-
paragraph.OwnerTextBody.ChildEntities.Insert(index, newPara);
29+
// Get the previous entity and cast it to a paragraph
30+
WParagraph previousParagraph = paragraph.OwnerTextBody.ChildEntities[index - 1] as WParagraph;
31+
bool hasPageBreak = false;
32+
// Check if the previous paragraph ends with a page break
33+
if (previousParagraph != null && previousParagraph.ChildEntities.Count > 0)
34+
{
35+
// Get the last item in the previous paragraph
36+
ParagraphItem lastItem = previousParagraph.ChildEntities[previousParagraph.ChildEntities.Count - 1] as ParagraphItem;
37+
// Enable the boolean if the last item is a page break
38+
if (lastItem != null && lastItem is Break && (lastItem as Break).BreakType == BreakType.PageBreak)
39+
hasPageBreak = true;
40+
}
41+
// If no page break is found, insert the page break before the current paragraph
42+
if (!hasPageBreak)
43+
{
44+
WParagraph newPara = new WParagraph(document);
45+
newPara.AppendBreak(BreakType.PageBreak);
46+
// Insert the new paragraph with page break at the correct position
47+
paragraph.OwnerTextBody.ChildEntities.Insert(index, newPara);
48+
}
4749
}
4850
}
4951
}
52+
// Save the Word document.
5053
document.Save(Path.GetFullPath("../../../Output/Output.docx"));
54+
// Close the Word document.
5155
document.Close();
5256
}
5357
}

0 commit comments

Comments
 (0)