-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload-a-source-document-define-split-criteria-and-execute-split-operation-in-a-single-workf.cs
More file actions
120 lines (103 loc) · 4.97 KB
/
Copy pathload-a-source-document-define-split-criteria-and-execute-split-operation-in-a-single-workf.cs
File metadata and controls
120 lines (103 loc) · 4.97 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.IO;
using Aspose.Words;
using Aspose.Words.Saving;
namespace SplitDocumentExample
{
// Custom callback to control the filenames of split document parts.
class SavedDocumentPartRename : IDocumentPartSavingCallback
{
private readonly string _baseFileName;
private readonly DocumentSplitCriteria _splitCriteria;
private int _partCount = 0;
public SavedDocumentPartRename(string baseFileName, DocumentSplitCriteria splitCriteria)
{
_baseFileName = baseFileName;
_splitCriteria = splitCriteria;
}
void IDocumentPartSavingCallback.DocumentPartSaving(DocumentPartSavingArgs args)
{
// Determine a readable part type name.
string partType = _splitCriteria switch
{
DocumentSplitCriteria.PageBreak => "Page",
DocumentSplitCriteria.ColumnBreak => "Column",
DocumentSplitCriteria.SectionBreak => "Section",
DocumentSplitCriteria.HeadingParagraph => "Heading",
_ => "Part"
};
// Build a unique filename for each part.
string partFileName = $"{_baseFileName}_part{++_partCount}_{partType}{Path.GetExtension(args.DocumentPartFileName)}";
// Set the filename; Aspose.Words will place the file in the same folder as the main output.
args.DocumentPartFileName = partFileName;
}
}
class Program
{
static void Main()
{
// Define folders for input and output.
string outputDir = Path.Combine(Directory.GetCurrentDirectory(), "Output");
Directory.CreateDirectory(outputDir);
// -----------------------------------------------------------------
// 1. Create a sample source document with two sections.
// -----------------------------------------------------------------
Document sourceDoc = new Document();
DocumentBuilder builder = new DocumentBuilder(sourceDoc);
// First section content.
builder.Writeln("Section 1 - Paragraph 1");
builder.Writeln("Section 1 - Paragraph 2");
// Insert a section break to start a new section.
builder.InsertBreak(BreakType.SectionBreakNewPage);
// Second section content.
builder.Writeln("Section 2 - Paragraph 1");
builder.Writeln("Section 2 - Paragraph 2");
// Save the source document to disk.
string sourcePath = Path.Combine(outputDir, "SourceDocument.docx");
sourceDoc.Save(sourcePath);
// -----------------------------------------------------------------
// 2. Load the source document.
// -----------------------------------------------------------------
Document loadedDoc = new Document(sourcePath);
// -----------------------------------------------------------------
// 3. Define split criteria (split by section break) and save.
// -----------------------------------------------------------------
string baseOutputName = "SplitDocument.html";
string baseOutputPath = Path.Combine(outputDir, baseOutputName);
HtmlSaveOptions saveOptions = new HtmlSaveOptions
{
DocumentSplitCriteria = DocumentSplitCriteria.SectionBreak,
// Optional: keep original formatting.
// Keep source formatting when splitting.
// This is the default behavior for HTML saving.
DocumentPartSavingCallback = new SavedDocumentPartRename(
Path.GetFileNameWithoutExtension(baseOutputName), DocumentSplitCriteria.SectionBreak)
};
// Save the document; this will generate multiple HTML files.
loadedDoc.Save(baseOutputPath, saveOptions);
// -----------------------------------------------------------------
// 4. Validate that split parts were created.
// -----------------------------------------------------------------
// The main file and the split parts are placed in the same folder.
string[] expectedFiles =
{
baseOutputPath,
Path.Combine(outputDir, "SplitDocument_part1_Section.html"),
Path.Combine(outputDir, "SplitDocument_part2_Section.html")
};
bool allExist = true;
foreach (string file in expectedFiles)
{
if (!File.Exists(file))
{
allExist = false;
Console.WriteLine($"Expected file not found: {file}");
}
}
if (allExist)
Console.WriteLine("Document split successfully. All expected files are present.");
else
Console.WriteLine("Document split failed. Some expected files are missing.");
}
}
}