Unplanned
Last Updated: 01 Sep 2025 11:35 by Martin Ivanov
Martin Ivanov
Created on: 01 Sep 2025 11:35
Category: RichTextBox
Type: Bug Report
0
RichTextBox: Exceptions occur on loading documents with element tags that don't have direct content but are using opening and closing tags instead of self-closing tags

The RichTextBox internal logic that imports .docx documents (DocxFormatProvider) doesn't support elements that don't need direct content, but have both start and end tags. Instead such elements should use self-closing tags. 

For example, instead of <w:vertAlign w:val="baseline"></w:vertAlign> it should be <w:vertAlign w:val="baseline"/>

When such element is read during import, issues may occur. Two main known issues here are with the w:vertAlign and Relationship tags.

Using

<w:vertAlign w:val="baseline"></w:vertAlign> 

instead of

<w:vertAlign w:val="baseline"/> 

throws NullReferenceException with the following stack trace:

System.NullReferenceException: 'Object reference not set to an instance of an object.'
Telerik.Windows.Documents.FormatProviders.OpenXml.dll!Telerik.Windows.Documents.FormatProviders.OpenXml.OpenXmlHelper.ConvertStringToBaselineAlignment(string value) Line 168 C#  
Telerik.Windows.Documents.FormatProviders.OpenXml.dll!Telerik.Windows.Documents.FormatProviders.OpenXml.Docx.Import.SpanStyleImporter.ReadBaselineAlignment(Telerik.Windows.Documents.FormatProviders.OpenXml.Docx.Parsing.Style style) Line 243 C#  
Telerik.Windows.Documents.FormatProviders.OpenXml.dll!Telerik.Windows.Documents.FormatProviders.OpenXml.Docx.Import.SpanStyleImporter.ReadStyle(Telerik.Windows.Documents.FormatProviders.OpenXml.Docx.Parsing.Style style) Line 88 C#  
Telerik.Windows.Documents.FormatProviders.OpenXml.dll!Telerik.Windows.Documents.FormatProviders.OpenXml.Docx.Import.StyleDefinitionsImporter.ReadSpanStyleProperties(Telerik.Windows.Documents.FormatProviders.OpenXml.Docx.Parsing.Style style) Line 278 C#  
Telerik.Windows.Documents.FormatProviders.OpenXml.dll!Telerik.Windows.Documents.FormatProviders.OpenXml.Docx.Import.StyleDefinitionsImporter.ReadStyle() Line 188 C#  
Telerik.Windows.Documents.FormatProviders.OpenXml.dll!Telerik.Windows.Documents.FormatProviders.OpenXml.Docx.Import.StyleDefinitionsImporter.Import() Line 67 C#  
Telerik.Windows.Documents.FormatProviders.OpenXml.dll!Telerik.Windows.Documents.FormatProviders.OpenXml.Docx.Import.DocxImporter.ReadXmlContentFromPackage(Telerik.Windows.Documents.FormatProviders.OpenXml.Docx.Import.DocxPartImporterBase importer) Line 430 C#  
Telerik.Windows.Documents.FormatProviders.OpenXml.dll!Telerik.Windows.Documents.FormatProviders.OpenXml.Docx.Import.DocxImporter.ReadXmlContentAndRelationsFromPackage(Telerik.Windows.Documents.FormatProviders.OpenXml.Docx.Import.DocxPartImporterBase importer) Line 406 C#  
Telerik.Windows.Documents.FormatProviders.OpenXml.dll!Telerik.Windows.Documents.FormatProviders.OpenXml.Docx.Import.DocxImporter.Import() Line 190 C#  
Telerik.Windows.Documents.FormatProviders.OpenXml.dll!Telerik.Windows.Documents.FormatProviders.OpenXml.Docx.DocxFormatProvider.Import(System.IO.Stream input) Line 147 C#
Telerik.Windows.Documents.dll!Telerik.Windows.Documents.FormatProviders.DocumentFormatProviderBase.Import(byte[] input) Line 90 C#

Using

<Relationship Id="rId00004" Target="styles.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"></Relationship> 

instead of

<Relationship Id="rId00004" Target="styles.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"/> 

throws KeyNotFoundException with the following stack trace:

KeyNotFoundException: The given key was not present in the dictionary.
    System.ThrowHelper.ThrowKeyNotFoundException() in ThrowHelper.cs
    System.Collections.Generic.Dictionary<TKey, TValue>.this[TKey].get(TKey) in Dictionary.cs
    Telerik.Windows.Documents.FormatProviders.OpenXml.Docx.Import.PartRelationsImporter.GetRelationByID(string) in PartRelationsImporter.cs
    Telerik.Windows.Documents.FormatProviders.OpenXml.Docx.Import.DocxImporter.GetRelationByID(string) in DocxImporter.cs
    Telerik.Windows.Documents.FormatProviders.OpenXml.Docx.Import.MainDocumentImporter.BuildSection() in MainDocumentImporter.cs
    Telerik.Windows.Documents.FormatProviders.OpenXml.Docx.Import.MainDocumentImporter.BuildBody() in MainDocumentImporter.cs
    Telerik.Windows.Documents.FormatProviders.OpenXml.Docx.Import.MainDocumentImporter.BuildDocument() in MainDocumentImporter.cs
    Telerik.Windows.Documents.FormatProviders.OpenXml.Docx.Import.MainDocumentImporter.Import() in MainDocumentImporter.cs
    Telerik.Windows.Documents.FormatProviders.OpenXml.Docx.Import.DocxImporter.ReadXmlContentFromPackage(Telerik.Windows.Documents.FormatProviders.OpenXml.Docx.Import.DocxPartImporterBase) in DocxImporter.cs
    Telerik.Windows.Documents.FormatProviders.OpenXml.Docx.Import.DocxImporter.ReadXmlContentAndRelationsFromPackage(Telerik.Windows.Documents.FormatProviders.OpenXml.Docx.Import.DocxPartImporterBase) in DocxImporter.cs

To work this around, import the document with the WordsProcessing library. If you need to display it in RadRichTextBox, you can export it again using the WordsProcessing library. This will normalize the tags. Then import it again using the RadDocument's DocxFormatProvider.

 var dplDocxFormatProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider();
 RadFlowDocument flowDocument = dplDocxFormatProvider.Import(File.ReadAllBytes("../../../test.docx"), null);

 var docBytes = dplDocxFormatProvider.Export(flowDocument, null);
 File.WriteAllBytes("../../../modified-doc.docx", docBytes);

 var rtbDocxFormatProvider= new Telerik.Windows.Documents.FormatProviders.OpenXml.Docx.DocxFormatProvider();
 RadDocument document = rtbDocxFormatProvider.Import(File.ReadAllBytes("../../../modified-doc.docx"));

 

0 comments