I am trying to add text to an existing HeaderFooter object. I am creating a Document object and loading it with RTF. The RTF text already has headers and footers defined. I have been using the DocumentBuilder object, navigating to the HeaderFooter and adding text but its not displaying after I save and open the document. I’m not sure what is wrong. Please review the attachment and see if you can advise.
Additionally, How can I add information to the bottom of the headerfooter instead of the top where the DocumentBuilder puts it?
Thanks
Mike
Hi Mike,
Thanks for your inquiry. The header/footer has paragraphs with hidden formatting. When you insert the text in the header/footer, the text will be written with hidden formatting. Please check the attached image for detail.
Please use the following modified code to achieve your requirements.
Document doc = new Document(@"C:\temp\input.rtf", loadOptions);
Console.WriteLine("LoadWarnings: " + loadWarnings.ToString());
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.ParagraphFormat.ClearFormatting();
builder.Font.ClearFormatting();
builder.Font.Hidden = false;
builder.InsertParagraph();
builder.Write("This is my header... there are none... wait...");
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
builder.ParagraphFormat.ClearFormatting();
builder.Font.ClearFormatting();
builder.Font.Hidden = false;
builder.Write("Footers are a dime-a-dozen...");
DocumentWarnings saveWarnings = new DocumentWarnings();
Aspose.Words.Saving.RtfSaveOptions saveOptions = new Aspose.Words.Saving.RtfSaveOptions();
saveOptions.WarningCallback = saveWarnings;
doc.FirstSection.PageSetup.DifferentFirstPageHeaderFooter = false;
doc.Save(@"C:\temp\output.rtf", saveOptions);
Great! Thanks for the information! I have a few follow up questions though.
-
How were you able to determine there was hidden text?
-
Will the ClearAllFormatting method clear the existing formatting in the header?
-
Lastly, In my experiments my text was added to the beginning of the header or footer. How can I add text to the end of the header or footer?
Thanks!