Aspose Words inserting header into first- width restricted- column of RTF

I have code that inserts header text onto the first page of an RTF document. For the attached document, this insert causes the header to be a single character column of vertical text, rather than the expected line of text at the top of the document:

h
e
a
d
e
r

How do I have to change my code to get the expected result, “Header” at the top, not the single column from above?

The code is as follows:

public static string AddHeader(string rtfDoc, string textToAdd)
{
ASPOSE_LicenseHandler.SetLicense();
if (String.IsNullOrEmpty(textToAdd) || String.IsNullOrEmpty(rtfDoc))
{
return rtfDoc;
}

if (!IsRtf(rtfDoc) && !IsRtf(textToAdd))
{
return textToAdd + Environment.NewLine + rtfDoc;
}

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())
{
var options = new RtfSaveOptions {ExportCompactSize = true, SaveFormat = SaveFormat.Rtf};
documentBuilder.Document.Save(combinedDocStream, options);
combinedDocStream.Position = 0;
var reader = new StreamReader(combinedDocStream);
string rtfData = reader.ReadToEnd();
return rtfData;
}
}

}
}



Hi Jon,

Thanks for your inquiry. Please use the DocumentBuilder.MoveToHeaderFooter method to move the cursor to the beginning of a header or footer in the current section. After you moved the cursor into a header or footer, you can use the rest of
DocumentBuilder methods to modify the contents of the header or footer. Please use DocumentBuilder.MoveToSection to move out of the header into the main text. This method moves the cursor to the beginning of the body in a specified section.

I have modified your code, please see highlighted line of code. Hope this helps you. Please let us know if you have any more queries.

var originalDoc = new Document(MyDir + “Allina.rtf”);
var documentBuilder = new DocumentBuilder(originalDoc);
documentBuilder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
documentBuilder.Document.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;
documentBuilder.PageSetup.TopMargin = documentBuilder.PageSetup.BottomMargin;
documentBuilder.Font.Name = "Arial";
documentBuilder.Font.Bold = true;
documentBuilder.Writeln("Header");
var options = new RtfSaveOptions { ExportCompactSize = true, SaveFormat = SaveFormat.Rtf };
documentBuilder.Document.Save(MyDir + "out.rtf", options);

That works perfect. Thank you.

Jon

Hi Jon,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.