-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordMigrationGuide.cs
More file actions
192 lines (179 loc) · 7 KB
/
Copy pathWordMigrationGuide.cs
File metadata and controls
192 lines (179 loc) · 7 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using DocumentFormat.OpenXml.Wordprocessing;
public class WordMigrationGuide
{
[Test]
public async Task Standard()
{
using var stream = new MemoryStream();
#region migration-word-standard
using (var doc = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document))
{
var mainPart = doc.AddMainDocumentPart();
// Add a styles part through the DOM. The SDK wires the relationship
// from the main part to the styles part automatically.
var stylesPart = mainPart.AddNewPart<StyleDefinitionsPart>();
stylesPart.Styles = new(
new Style(
new StyleName
{
Val = "Heading 1"
},
new BasedOn
{
Val = "Normal"
},
new NextParagraphStyle
{
Val = "Normal"
},
new StyleRunProperties(
new Bold(),
new FontSize
{
Val = "32"
}))
{
Type = StyleValues.Paragraph,
StyleId = "Heading1"
});
// Assign the main document body. Paragraphs reference the style by id.
mainPart.Document =
new(
new Body(
new Paragraph(
new ParagraphProperties(
new ParagraphStyleId
{
Val = "Heading1"
}),
new Run(new Text("Quarterly Report"))),
new Paragraph(
new Run(new Text("Revenue grew 15% year-over-year."))),
new Paragraph(
new Run(new Text("Operating costs held flat.")))));
}
#endregion
stream.Position = 0;
await Verify(stream, extension: "docx");
}
[Test]
public async Task Streaming()
{
using var stream = new MemoryStream();
#region migration-word-streaming
await using (var writer = StreamingDocument.CreateWord(stream, leaveOpen: true))
{
// Write the styles part first. Every part is written as a
// complete element tree — there's no DOM to mutate later.
writer.WritePart(
new("/word/styles.xml", UriKind.Relative),
"application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml",
new Styles(
new Style(
new StyleName
{
Val = "Heading 1"
},
new BasedOn
{
Val = "Normal"
},
new NextParagraphStyle
{
Val = "Normal"
},
new StyleRunProperties(
new Bold(),
new FontSize
{
Val = "32"
}))
{
Type = StyleValues.Paragraph,
StyleId = "Heading1"
}));
// Write the main document, declaring its relationship to the
// styles part inline via the relationships parameter.
writer.WritePart(
new("/word/document.xml", UriKind.Relative),
"application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml",
new Document(
new Body(
new Paragraph(
new ParagraphProperties(
new ParagraphStyleId
{
Val = "Heading1"
}),
new Run(new Text("Quarterly Report"))),
new Paragraph(
new Run(new Text("Revenue grew 15% year-over-year."))),
new Paragraph(
new Run(new Text("Operating costs held flat."))))),
[
new(
new("styles.xml", UriKind.Relative),
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles",
id: "rId1"),
]);
}
#endregion
stream.Position = 0;
await Verify(stream, extension: "docx");
}
[Test]
public async Task Builder()
{
using var stream = new MemoryStream();
#region migration-word-builder
await using (var word = new StreamingWordDocumentBuilder(stream, leaveOpen: true))
{
// Add the styles part. The builder writes it immediately and
// tracks the relationship for the main document below.
word.AddStyles(
new(
new Style(
new StyleName
{
Val = "Heading 1"
},
new BasedOn
{
Val = "Normal"
},
new NextParagraphStyle
{
Val = "Normal"
},
new StyleRunProperties(
new Bold(),
new FontSize
{
Val = "32"
}))
{
Type = StyleValues.Paragraph,
StyleId = "Heading1"
}));
// Write the main document. The builder wires up the styles
// relationship for you — no PartRelationship plumbing.
word.WriteDocument(
new(
new Body(
new Paragraph(
new ParagraphProperties(
new ParagraphStyleId
{
Val = "Heading1"
}),
new Run(new Text("Quarterly Report"))),
new Paragraph(
new Run(new Text("Revenue grew 15% year-over-year."))),
new Paragraph(
new Run(new Text("Operating costs held flat."))))));
}
#endregion
stream.Position = 0;
await Verify(stream, extension: "docx");
}
}