Skip to content

Commit 3a44ce5

Browse files
Merge pull request #289 from VijayadharshiniMathiyalagan/ES-882953-How-to-apply-continuous-list-numbering-when-merging-two-documents-with-different-list-styles
Add the sample for apply continuous list numbering when merging
2 parents 03ee5fb + bac2ff2 commit 3a44ce5

File tree

6 files changed

+106
-0
lines changed

6 files changed

+106
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35417.141 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Continue-numbering-different-styles", "Continue-numbering-different-styles\Continue-numbering-different-styles.csproj", "{D87D9913-CBE4-4D96-9424-B59326FDC058}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{D87D9913-CBE4-4D96-9424-B59326FDC058}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{D87D9913-CBE4-4D96-9424-B59326FDC058}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{D87D9913-CBE4-4D96-9424-B59326FDC058}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{D87D9913-CBE4-4D96-9424-B59326FDC058}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Continue_numbering_different_styles</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Data\DestinationDocument.docx">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Data\SourceDocument.docx">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
<None Update="Output\.gitkeep">
23+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
24+
</None>
25+
</ItemGroup>
26+
27+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
4+
using (FileStream destinationStream = new FileStream(Path.GetFullPath("Data/DestinationDocument.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
5+
{
6+
// Open the destination Word document.
7+
using (WordDocument destinationDocument = new WordDocument(destinationStream, FormatType.Docx))
8+
{
9+
using (FileStream sourceStream = new FileStream(Path.GetFullPath("Data/SourceDocument.docx"), FileMode.Open, FileAccess.ReadWrite))
10+
{
11+
// Open the source Word document.
12+
using (WordDocument sourceDocument = new WordDocument(sourceStream, FormatType.Docx))
13+
{
14+
// Set the break code as no break to prevent page breaks.
15+
sourceDocument.Sections[0].BreakCode = SectionBreakCode.NoBreak;
16+
// Initialize a variable to hold the list style from the destination document.
17+
ListStyle listStyle = null;
18+
// Iterate through the paragraphs in the last section of the destination document.
19+
int paragraphCount = destinationDocument.LastSection.Paragraphs.Count;
20+
for (int i = paragraphCount - 1; i >= 0; i--)
21+
{
22+
WParagraph paragraph = destinationDocument.LastSection.Paragraphs[i];
23+
// Get the current list style.
24+
if (paragraph.ListFormat.CurrentListStyle != null)
25+
listStyle = paragraph.ListFormat.CurrentListStyle;
26+
else
27+
{
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;
32+
}
33+
// Break the loop if a list style is found.
34+
if (listStyle != null)
35+
break;
36+
}
37+
// Import the content of the source document at the end of the destination document.
38+
destinationDocument.ImportContent(sourceDocument, ImportOptions.ListContinueNumbering);
39+
if (listStyle != null)
40+
{
41+
// Apply liststyle to the paragraphs in the destination document to maintain continuous numbering.
42+
foreach (WParagraph paragraph in destinationDocument.LastSection.Paragraphs)
43+
{
44+
if (paragraph.ListFormat.CurrentListStyle != null)
45+
paragraph.ListFormat.ApplyStyle(listStyle.Name);
46+
}
47+
}
48+
// Save the merged document.
49+
using (FileStream outputStream = new FileStream(Path.GetFullPath("Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
50+
{
51+
destinationDocument.Save(outputStream, FormatType.Docx);
52+
}
53+
}
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)