-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSamples.Word.cs
More file actions
222 lines (191 loc) · 7.76 KB
/
Copy pathSamples.Word.cs
File metadata and controls
222 lines (191 loc) · 7.76 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using DocumentFormat.OpenXml.Wordprocessing;
[TestFixture]
public partial class Samples
{
[Test]
public void MinimalWord()
{
using var stream = new MemoryStream();
#region minimal-word
using var writer = new OpenXmlPackageWriter(stream, leaveOpen: true);
writer.AddRelationship(
new("/word/document.xml", UriKind.Relative),
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",
"rId1");
writer.WritePart(
new("/word/document.xml", UriKind.Relative),
"application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml",
new Document(new Body(new Paragraph(new Run(new Text("Hello!"))))));
#endregion
}
[Test]
public void StreamingDocumentFactory()
{
using var stream = new MemoryStream();
#region streaming-document-factory
using var writer = StreamingDocument.CreateWord(stream, leaveOpen: true);
writer.WritePart(
new("/word/document.xml", UriKind.Relative),
"application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml",
new Document(new Body(new Paragraph(new Run(new Text("Forward-only!"))))));
#endregion
}
[Test]
public void StreamingPartContent()
{
using var stream = new MemoryStream();
using var writer = new OpenXmlPackageWriter(stream, leaveOpen: true);
writer.AddRelationship(
new("/word/document.xml", UriKind.Relative),
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",
"rId1");
#region streaming-part-content
using var entry = writer.CreatePart(
new("/word/document.xml", UriKind.Relative),
"application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml");
using var xmlWriter = OpenXmlWriter.Create(entry.Stream);
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement(new Document());
xmlWriter.WriteStartElement(new Body());
xmlWriter.WriteElement(new Paragraph(new Run(new Text("Streamed!"))));
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
#endregion
}
[Test]
public void ExternalRelationship()
{
using var stream = new MemoryStream();
using var writer = new OpenXmlPackageWriter(stream, leaveOpen: true);
writer.AddRelationship(
new("/word/document.xml", UriKind.Relative),
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",
"rId1");
using var entry = writer.CreatePart(
new("/word/document.xml", UriKind.Relative),
"application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml");
#region external-relationship
entry.AddRelationship(
new("https://example.com", UriKind.Absolute),
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
TargetMode.External,
"rId1");
#endregion
}
[Test]
public void ConstructionVariants()
{
using var stream = new MemoryStream();
#region construction-variants
// Direct construction
using var direct = new OpenXmlPackageWriter(stream, leaveOpen: true);
// Typed factories (pre-register the officeDocument relationship)
using var word = StreamingDocument.CreateWord(stream, leaveOpen: true);
using var spreadsheet = StreamingDocument.CreateSpreadsheet(stream, leaveOpen: true);
using var presentation = StreamingDocument.CreatePresentation(stream, leaveOpen: true);
#endregion
}
[Test]
public void PartRelationshipStruct()
{
#region part-relationship-struct
var relationship = new PartRelationship(
targetUri: new("styles.xml", UriKind.Relative),
relationshipType: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles",
// required — the part body almost always references its own
// relationships by id, so the caller must know it up front
id: "rId1",
// default
targetMode: TargetMode.Internal);
#endregion
_ = relationship;
}
[Test]
public async Task AsyncUsage()
{
using var stream = new MemoryStream();
#region async-usage
await using var writer = StreamingDocument.CreateWord(stream, leaveOpen: true);
writer.WritePart(
new("/word/document.xml", UriKind.Relative),
"application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml",
new Document(new Body(new Paragraph(new Run(new Text("Streamed async!"))))));
// DisposeAsync (triggered by `await using`) asynchronously flushes
// the final buffer — including the ZIP central directory — so remote
// sinks like SQL BLOB streams don't block the thread on network I/O.
#endregion
}
[Test]
public void CustomBufferSize()
{
using var stream = new MemoryStream();
#region custom-buffer-size
// Bigger buffer = fewer, larger writes hit the sink — good for
// remote streams where per-write overhead is high. Pass 0 to
// disable buffering entirely and write straight to the sink.
using var writer = new OpenXmlPackageWriter(
stream,
leaveOpen: true,
// 1 MB
bufferSize: 1024 * 1024);
#endregion
}
[Test]
public async Task WordDocumentBuilderSample()
{
using var stream = new MemoryStream();
#region word-document-builder
await using var word = new StreamingWordDocumentBuilder(stream, leaveOpen: true);
// Add an optional styles part — referenced by paragraphs via StyleId.
word.AddStyles(
new(
new Style(
new StyleName
{
Val = "Heading 1"
},
new BasedOn
{
Val = "Normal"
},
new StyleRunProperties(
new Bold(),
new FontSize
{
Val = "32"
}))
{
Type = StyleValues.Paragraph,
StyleId = "Heading1"
}));
// AddFooter returns the relationship id — plug it into SectionProperties
// in the document body below so the body-level FooterReference resolves.
var footerId = word.AddFooter(
new(
new Paragraph(new Run(new Text("— Confidential —")))));
// Last step: write the main document body. The builder wires up all
// the accumulated sub-part relationships (styles + footer) for you.
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 SectionProperties(
new FooterReference
{
Type = HeaderFooterValues.Default,
Id = footerId
}))));
#endregion
}
}