Current SAX examples don't write text of leaf elements by default. This might corrupt some files, e.g. Word documents with DrawingML objects which use wp:posOffset elements since they're required to have some value.
Below is an example of a document with a single field. Field's text is lost when, for example, text is replaced following this sample.
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
public static class Generator
{
public static void CreatePackage(string filePath)
{
using var package = WordprocessingDocument.Create(filePath, WordprocessingDocumentType.Document);
var part = package.AddMainDocumentPart();
var document = part.Document = new();
document.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
document.Append(new Body(new Paragraph(
new Run(new FieldChar { FieldCharType = FieldCharValues.Begin }),
new Run(new FieldCode { Text = " PAGE \\* MERGEFORMAT " }),
new Run(new FieldChar { FieldCharType = FieldCharValues.Separate }),
new Run(new Text { Text = "1" }),
new Run(new FieldChar { FieldCharType = FieldCharValues.End })
)));
}
}
Current SAX examples don't write text of leaf elements by default. This might corrupt some files, e.g. Word documents with DrawingML objects which use wp:posOffset elements since they're required to have some value.
Below is an example of a document with a single field. Field's text is lost when, for example, text is replaced following this sample.