Broken bullet points and numbering when inserting RTF to a Word Document

Hi,

I have to insert multiple RTF contents into a single word document which I receive from another application (they have there their own user control to create RTF contents by their users).

This is my code, using Aspose 22.1.0 with .NET. For sake of simplicity I am using a new blank Word document.

Get RTF from string as Document)

private static Document RtfStringToDocument(string rtf)
{
    Document doc = null;
    // Convert RTF string to byte array.
    byte[] rtfBytes = Encoding.UTF8.GetBytes(rtf);

    LoadOptions loadoptions = new LoadOptions();
    loadoptions.LoadFormat = LoadFormat.Rtf;
    // Create stream.
    using (MemoryStream rtfStream = new MemoryStream(rtfBytes))
    {
        // Open document from stream.
        doc = new Document(rtfStream, loadoptions);
    }
    return doc;
}

Scenario 1) Insert just one RTF content (“RTF_Input.rtf”). In this case everything is fine (generated document looks like RTF input; see attached “GeneratedDocumentScenario1.docx”)

string workingDirectory = @"c:\daten\tmp\";

Document documentBase = new Document(Path.Combine(workingDirectory, "TargetDocument.docx"));
DocumentBuilder builder = new DocumentBuilder(documentBase);

builder.MoveToDocumentEnd();
// just one RTF content
builder.InsertDocument(RtfStringToDocument(File.ReadAllText(Path.Combine(workingDirectory, "RTF_Input.rtf"))), ImportFormatMode.KeepSourceFormatting);

documentBase.Save(Path.Combine(workingDirectory, "GeneratedDocumentScenario1.docx"));

Scenario 2) Insert two RTF contents (same RTF content twice; “RTF_Input.rtf”). In this case the document is broken (bullet points and nummering are lost; see attached "GeneratedDocumentScenario2.docx")

string workingDirectory = @"c:\daten\tmp\";

Document documentBase = new Document(Path.Combine(workingDirectory, "TargetDocument.docx"));
DocumentBuilder builder = new DocumentBuilder(documentBase);

builder.MoveToDocumentEnd();
// two RTF content
builder.InsertDocument(RtfStringToDocument(File.ReadAllText(Path.Combine(workingDirectory, "RTF_Input.rtf"))), ImportFormatMode.KeepSourceFormatting);
builder.InsertDocument(RtfStringToDocument(File.ReadAllText(Path.Combine(workingDirectory, "RTF_Input.rtf"))), ImportFormatMode.KeepSourceFormatting);

documentBase.Save(Path.Combine(workingDirectory, "GeneratedDocumentScenario2.docx"));

This also happens with completly different RTF content. But only bullet points and numbering is affected. Everything else is fine.

Looking forward for your input.

best regards,
Thomas

Documents.zip (30.2 KB)

@pulla2908 To resolve the problem you can set ImportFormatOptions.KeepSourceNumbering. The following code produces the correct output:

string rtf = File.ReadAllText(Path.Combine(workingDirectory, "RTF_Input.rtf"));

ImportFormatOptions options = new ImportFormatOptions();
options.KeepSourceNumbering = true;

builder.InsertDocument(RtfStringToDocument(rtf), ImportFormatMode.KeepSourceFormatting, options);
builder.InsertDocument(RtfStringToDocument(rtf), ImportFormatMode.KeepSourceFormatting, options);
1 Like

Looks good, thank you for the quick response, at first glance it works as you say, brilliant!

1 Like