Prepending text to rtf document greatly increases it's size

The following code is used to prepend information onto an RTF document. This information will be a text string such as “[No Summary]”. When this is done, depending on the document and it’s complexity, the resulting document may be as much as twice the size of the original document. Is there a way to prevent such aggressive growth of the document? Is there something blatantly wrong with the code in use below?

public static string AddHeader(string rtfDoc, string textToAdd)
{
    using (var rtfDocStream = new MemoryStream())
    {
        using (var writer = new StreamWriter(rtfDocStream))
        {
            writer.WriteLine(rtfDoc);
            writer.Flush();

            var originalDoc = new Document(rtfDocStream);

            var documentBuilder = new DocumentBuilder(originalDoc);
            documentBuilder.Document.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;
            documentBuilder.PageSetup.TopMargin = documentBuilder.PageSetup.BottomMargin;
            documentBuilder.Font.Name = "Arial";
            documentBuilder.Font.Bold = true;
            documentBuilder.Writeln(textToAdd);

            using (var combinedDocStream = new MemoryStream())
            {
                documentBuilder.Document.Save(combinedDocStream, SaveFormat.Rtf);
                combinedDocStream.Position = 0;
                var reader = new StreamReader(combinedDocStream);
                string rtfData = reader.ReadToEnd();
                return rtfData;
            }
        }
    }
}

Hi Jon,

Thanks for your inquiry. Could you please attach your input RTF document here for testing? I will investigate the issue on my side and provide you more information.

My company is scrubbing the RTF file to ensure no confidential information is contained in it. I will respond with the file ASAP.

Thank you,
Jon Ediger

The attached files do appear to have the growth issue, but not as much as another file. That final file looks corrupted, and may be a root cause of it’s massive growth when using the code above.

Thank you,
Jon Ediger

Hi Jon,

Thanks for your inquiry. Please note that Aspose.Words tries to mimic the same behaviour as MS Word do. Please add some contents into ALLINA SUMMARY DATA - NORAN 2.rtf by using MS Word and check the size of new RTF. Aspose.Words generates the same file size. MS Words also increase the file size.

However, I have managed to reproduce the same issue with InstructionsPartial.rtf at my side. I have logged this issue as WORDSNET-8352 in our issue tracking system. I have linked this forum thread to the same issue and you will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

Hi Jon,

Thanks for your patience. On further investigation our development team came to know that they won’t be able to implement the fix to your issue. Your issue will be closed with ‘‘Won’t Fix’’ resolution. Please use RtfSaveOptions.ExportCompactSize as a workaround of this issue.

var originalDoc = new Document(MyDir + "InstructionsPartial.rtf");
RtfSaveOptions options = new RtfSaveOptions();
options.ExportCompactSize = true;
originalDoc.Save(MyDir + "out.rtf", options);