Skip to content

Commit a7c2996

Browse files
Added sample for apply continuos list numbering
1 parent 8ecbe1d commit a7c2996

File tree

6 files changed

+12
-13
lines changed

6 files changed

+12
-13
lines changed

Mail-Merge/Continue-numbering-different-styles/Continue-numbering-different-styles.sln renamed to Word-document/Continue-numbering-different-styles/Continue-numbering-different-styles.sln

File renamed without changes.

Mail-Merge/Continue-numbering-different-styles/Continue-numbering-different-styles/Continue-numbering-different-styles.csproj renamed to Word-document/Continue-numbering-different-styles/Continue-numbering-different-styles/Continue-numbering-different-styles.csproj

File renamed without changes.

Mail-Merge/Continue-numbering-different-styles/Continue-numbering-different-styles/Data/DestinationDocument.docx renamed to Word-document/Continue-numbering-different-styles/Continue-numbering-different-styles/Data/DestinationDocument.docx

File renamed without changes.

Mail-Merge/Continue-numbering-different-styles/Continue-numbering-different-styles/Data/SourceDocument.docx renamed to Word-document/Continue-numbering-different-styles/Continue-numbering-different-styles/Data/SourceDocument.docx

File renamed without changes.

Mail-Merge/Continue-numbering-different-styles/Continue-numbering-different-styles/Output/.gitkeep renamed to Word-document/Continue-numbering-different-styles/Continue-numbering-different-styles/Output/.gitkeep

File renamed without changes.

Mail-Merge/Continue-numbering-different-styles/Continue-numbering-different-styles/Program.cs renamed to Word-document/Continue-numbering-different-styles/Continue-numbering-different-styles/Program.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,46 @@
33

44
using (FileStream destinationStream = new FileStream(Path.GetFullPath("Data/DestinationDocument.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
55
{
6-
using (FileStream sourceStream = new FileStream(Path.GetFullPath("Data/SourceDocument.docx"), FileMode.Open, FileAccess.ReadWrite))
6+
// Open the destination Word document.
7+
using (WordDocument destinationDocument = new WordDocument(destinationStream, FormatType.Docx))
78
{
8-
// Open the destination Word document.
9-
using (WordDocument destinationDocument = new WordDocument(destinationStream, FormatType.Docx))
9+
using (FileStream sourceStream = new FileStream(Path.GetFullPath("Data/SourceDocument.docx"), FileMode.Open, FileAccess.ReadWrite))
1010
{
1111
// Open the source Word document.
1212
using (WordDocument sourceDocument = new WordDocument(sourceStream, FormatType.Docx))
1313
{
14-
// Set the break code of the source document's first section to prevent page breaks.
14+
// Set the break code as no break to prevent page breaks.
1515
sourceDocument.Sections[0].BreakCode = SectionBreakCode.NoBreak;
1616
// Initialize a variable to hold the list style from the destination document.
1717
ListStyle listStyle = null;
1818
// Iterate through the paragraphs in the last section of the destination document.
1919
foreach (WParagraph paragraph in destinationDocument.LastSection.Paragraphs)
2020
{
21-
// Store the current list style if available.
2221
if (paragraph.ListFormat.CurrentListStyle != null)
2322
{
24-
listStyle = paragraph.ListFormat.CurrentListStyle;
23+
// Get the current list style.
24+
listStyle = paragraph.ListFormat.CurrentListStyle;
2525
}
2626
else
2727
{
28-
// If no current list style, check the paragraph style for a list style and store it.
29-
WParagraphStyle style1 = destinationDocument.Styles.FindByName(paragraph.StyleName) as WParagraphStyle;
30-
if (style1 != null)
31-
listStyle = style1.ListFormat.CurrentListStyle;
28+
// Check the paragraph style for a list style and store it.
29+
WParagraphStyle style = destinationDocument.Styles.FindByName(paragraph.StyleName) as WParagraphStyle;
30+
if (style != null)
31+
listStyle = style.ListFormat.CurrentListStyle;
3232
}
3333
// Break the loop if a list style is found.
3434
if (listStyle != null)
3535
break;
3636
}
3737
// Import the content of the source document at the end of the destination document.
3838
destinationDocument.ImportContent(sourceDocument, ImportOptions.ListContinueNumbering);
39-
// If a list style is found, apply it to the paragraphs in the destination document to maintain continuous numbering.
4039
if (listStyle != null)
4140
{
41+
// Apply liststyle to the paragraphs in the destination document to maintain continuous numbering.
4242
foreach (WParagraph paragraph in destinationDocument.LastSection.Paragraphs)
4343
{
44-
// Apply the found list style.
4544
if (paragraph.ListFormat.CurrentListStyle != null)
46-
paragraph.ListFormat.ApplyStyle(listStyle.Name);
45+
paragraph.ListFormat.ApplyStyle(listStyle.Name);
4746
}
4847
}
4948
// Save the merged document.

0 commit comments

Comments
 (0)