-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit-an-html-source-into-chapters-and-save-each-as-docx-while-preserving-inline-styles.cs
More file actions
108 lines (92 loc) · 4.46 KB
/
Copy pathsplit-an-html-source-into-chapters-and-save-each-as-docx-while-preserving-inline-styles.cs
File metadata and controls
108 lines (92 loc) · 4.46 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
using System;
using System.Collections.Generic;
using System.IO;
using Aspose.Words;
using Aspose.Words.Loading;
using Aspose.Words.Saving;
public class Program
{
public static void Main()
{
// Prepare a temporary folder for all generated files.
string outputDir = Path.Combine(Directory.GetCurrentDirectory(), "SplitOutput");
if (Directory.Exists(outputDir))
Directory.Delete(outputDir, true);
Directory.CreateDirectory(outputDir);
// Sample HTML content with headings (chapters) and inline styles.
string htmlContent = @"
<!DOCTYPE html>
<html>
<head><title>Sample Document</title></head>
<body>
<h1>Chapter 1</h1>
<p>This is the <span style='color:red;'>first</span> paragraph of chapter 1.</p>
<p>Another paragraph with <b>bold</b> text.</p>
<h2>Section 1.1</h2>
<p>Content of a sub‑section.</p>
<h1>Chapter 2</h1>
<p>Paragraph in chapter 2 with <i>italic</i> style.</p>
<h1>Chapter 3</h1>
<p>Final chapter paragraph.</p>
</body>
</html>";
// Load the HTML into an Aspose.Words Document.
using (MemoryStream htmlStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(htmlContent)))
{
// LoadOptions can be used to specify the load format explicitly.
LoadOptions loadOptions = new LoadOptions { LoadFormat = LoadFormat.Html };
Document sourceDoc = new Document(htmlStream, loadOptions);
// Collect all paragraphs in the document.
NodeCollection paragraphs = sourceDoc.GetChildNodes(NodeType.Paragraph, true);
int chapterIndex = 0;
for (int i = 0; i < paragraphs.Count; i++)
{
Paragraph para = (Paragraph)paragraphs[i];
// Identify chapter start: a paragraph styled as Heading 1.
if (para.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading1)
{
chapterIndex++;
// Create a new empty document for the current chapter.
Document chapterDoc = new Document();
// Remove the default empty section created by the constructor.
chapterDoc.RemoveAllChildren();
// Add a new section with a body to hold imported nodes.
Section chapterSection = new Section(chapterDoc);
chapterDoc.AppendChild(chapterSection);
Body chapterBody = new Body(chapterDoc);
chapterSection.AppendChild(chapterBody);
// Prepare an importer to copy nodes while preserving source formatting.
NodeImporter importer = new NodeImporter(sourceDoc, chapterDoc, ImportFormatMode.KeepSourceFormatting);
// Import the heading paragraph itself.
Node importedNode = importer.ImportNode(para, true);
chapterBody.AppendChild(importedNode);
// Import subsequent nodes until the next Heading 1 or end of document.
int j = i + 1;
while (j < paragraphs.Count)
{
Paragraph nextPara = (Paragraph)paragraphs[j];
if (nextPara.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading1)
break; // Next chapter starts here.
// Import the node (could be a paragraph, table, etc.).
Node imported = importer.ImportNode(nextPara, true);
chapterBody.AppendChild(imported);
j++;
}
// Save the chapter as DOCX.
string chapterPath = Path.Combine(outputDir, $"Chapter_{chapterIndex}.docx");
chapterDoc.Save(chapterPath, SaveFormat.Docx);
// Advance the outer loop to the node before the next heading.
i = j - 1;
}
}
// Simple validation: ensure at least one chapter file was created.
string[] generatedFiles = Directory.GetFiles(outputDir, "*.docx");
if (generatedFiles.Length == 0)
throw new InvalidOperationException("No chapter files were generated.");
// Optionally, output the list of generated files to the console.
Console.WriteLine("Generated chapter files:");
foreach (string file in generatedFiles)
Console.WriteLine(file);
}
}
}