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