Skip to content

Commit 91a2487

Browse files
Apply indentation for import content
1 parent 3d93e56 commit 91a2487

File tree

1 file changed

+28
-26
lines changed
  • Paragraphs/Apply-indentation-for-import-content/.NET/Apply-indentation-for-import-content

1 file changed

+28
-26
lines changed

Paragraphs/Apply-indentation-for-import-content/.NET/Apply-indentation-for-import-content/Program.cs

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,41 @@
55
using (FileStream destinationStream = new FileStream(Path.GetFullPath(@"Data/DestinationDocument.docx"), FileMode.Open))
66
{
77
// Load the main Word document.
8-
WordDocument destinationDocument = new WordDocument(destinationStream, FormatType.Docx);
9-
10-
// Open the existing source document from a file stream.
11-
using (FileStream sourceStream = new FileStream(Path.GetFullPath(@"Data/SourceDocument.docx"), FileMode.Open))
8+
using (WordDocument destinationDocument = new WordDocument(destinationStream, FormatType.Docx))
129
{
13-
// Load the source Word document.
14-
WordDocument sourceDocument = new WordDocument(sourceStream, FormatType.Docx);
15-
16-
// Set the first section break in the source document to "NoBreak" for seamless merging.
17-
sourceDocument.Sections[0].BreakCode = SectionBreakCode.NoBreak;
10+
// Open the existing source document from a file stream.
11+
using (FileStream sourceStream = new FileStream(Path.GetFullPath(@"Data/SourceDocument.docx"), FileMode.Open))
12+
{
13+
// Load the source Word document.
14+
using (WordDocument sourceDocument = new WordDocument(sourceStream, FormatType.Docx))
15+
{
16+
// Set the first section break in the source document to "NoBreak" for seamless merging.
17+
sourceDocument.Sections[0].BreakCode = SectionBreakCode.NoBreak;
1818

19-
// Get the index of the last section in the destination document.
20-
int secIndex = destinationDocument.ChildEntities.IndexOf(destinationDocument.LastSection);
19+
// Get the index of the last section in the destination document.
20+
int secIndex = destinationDocument.ChildEntities.IndexOf(destinationDocument.LastSection);
2121

22-
// Get the index of the last paragraph in the last section of the destination document.
23-
int paraIndex = destinationDocument.LastSection.Body.ChildEntities.IndexOf(destinationDocument.LastParagraph);
22+
// Get the index of the last paragraph in the last section of the destination document.
23+
int paraIndex = destinationDocument.LastSection.Body.ChildEntities.IndexOf(destinationDocument.LastParagraph);
2424

25-
// Get the style and formatting of the last paragraph for reference.
26-
WParagraph lastPara = destinationDocument.LastParagraph;
25+
// Get the style and formatting of the last paragraph for reference.
26+
WParagraph lastPara = destinationDocument.LastParagraph;
2727

28-
// Import content from the source document into the destination document, using destination styles.
29-
destinationDocument.ImportContent(sourceDocument, ImportOptions.UseDestinationStyles);
28+
// Import content from the source document into the destination document, using destination styles.
29+
destinationDocument.ImportContent(sourceDocument, ImportOptions.UseDestinationStyles);
3030

31-
// Modify the paragraph style for the newly added contents by applying left indentation.
32-
AddLeftIndentation(destinationDocument, secIndex, paraIndex + 1, lastPara.ParagraphFormat.LeftIndent);
31+
// Modify the paragraph style for the newly added contents by applying left indentation.
32+
AddLeftIndentation(destinationDocument, secIndex, paraIndex + 1, lastPara.ParagraphFormat.LeftIndent);
3333

34-
// Save the updated destination document to a new file.
35-
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Output.docx"), FileMode.Create, FileAccess.Write))
36-
{
37-
destinationDocument.Save(outputStream, FormatType.Docx);
38-
};
39-
};
40-
};
34+
// Save the updated destination document to a new file.
35+
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Output.docx"), FileMode.Create, FileAccess.Write))
36+
{
37+
destinationDocument.Save(outputStream, FormatType.Docx);
38+
}
39+
}
40+
}
41+
}
42+
}
4143

4244
/// <summary>
4345
/// Applies left indentation to paragraphs and tables in a specified section and paragraph range of a Word document.

0 commit comments

Comments
 (0)