Skip to content

SAX examples skip elements' text #351

Description

@0MG-DEN

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 })
        )));
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions