-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathafter-splitting-open-each-output-document-programmatically-to-verify-headers-and-footers.cs
More file actions
106 lines (86 loc) · 4.47 KB
/
Copy pathafter-splitting-open-each-output-document-programmatically-to-verify-headers-and-footers.cs
File metadata and controls
106 lines (86 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.IO;
using Aspose.Words;
using Aspose.Words.Saving;
public class SplitDocumentExample
{
public static void Main()
{
// Folder for all generated files.
string outputDir = Path.Combine(Directory.GetCurrentDirectory(), "Output");
Directory.CreateDirectory(outputDir);
// -----------------------------------------------------------------
// 1. Create a sample source document with two sections and headers/footers.
// -----------------------------------------------------------------
Document sourceDoc = new Document();
DocumentBuilder builder = new DocumentBuilder(sourceDoc);
// Enable different headers/footers for first page and odd/even pages.
builder.PageSetup.DifferentFirstPageHeaderFooter = true;
builder.PageSetup.OddAndEvenPagesHeaderFooter = true;
// ----- First section -----
// Header – first page
builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
builder.Write("First page header");
// Header – even pages
builder.MoveToHeaderFooter(HeaderFooterType.HeaderEven);
builder.Write("Even page header");
// Header – primary (odd) pages
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.Write("Primary page header");
// Footer – primary (odd) pages
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
builder.Write("Primary page footer");
// Body content for first section
builder.MoveToDocumentEnd();
builder.Writeln("Content of the first section.");
builder.InsertBreak(BreakType.PageBreak);
builder.Writeln("Second page of the first section.");
// Insert a section break to start a new section.
builder.InsertBreak(BreakType.SectionBreakNewPage);
// ----- Second section -----
// Header – primary (odd) pages for second section
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.Write("Second section header");
// Footer – primary (odd) pages for second section
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
builder.Write("Second section footer");
// Body content for second section
builder.MoveToDocumentEnd();
builder.Writeln("Content of the second section.");
// -----------------------------------------------------------------
// 2. Split the document by sections, preserving headers and footers.
// -----------------------------------------------------------------
for (int i = 0; i < sourceDoc.Sections.Count; i++)
{
// Create a new empty document.
Document splitDoc = new Document();
splitDoc.EnsureMinimum();
// Import the current section into the new document.
NodeImporter importer = new NodeImporter(sourceDoc, splitDoc, ImportFormatMode.KeepSourceFormatting);
Section importedSection = (Section)importer.ImportNode(sourceDoc.Sections[i], true);
// Replace the default empty section with the imported one.
splitDoc.Sections.Clear();
splitDoc.Sections.Add(importedSection);
// Save the split part.
string partPath = Path.Combine(outputDir, $"Part_{i + 1}.docx");
splitDoc.Save(partPath);
}
// -----------------------------------------------------------------
// 3. Verify that each split document contains the expected headers/footers.
// -----------------------------------------------------------------
Console.WriteLine("Verification of split documents:");
for (int i = 0; i < sourceDoc.Sections.Count; i++)
{
string partPath = Path.Combine(outputDir, $"Part_{i + 1}.docx");
Document partDoc = new Document(partPath);
// Retrieve primary header and footer (if they exist).
HeaderFooter header = partDoc.FirstSection?.HeadersFooters[HeaderFooterType.HeaderPrimary];
HeaderFooter footer = partDoc.FirstSection?.HeadersFooters[HeaderFooterType.FooterPrimary];
string headerText = header?.GetText().Trim() ?? "(no header)";
string footerText = footer?.GetText().Trim() ?? "(no footer)";
Console.WriteLine($" Part {i + 1}:");
Console.WriteLine($" Header: {headerText}");
Console.WriteLine($" Footer: {footerText}");
}
}
}