This demo uses the DevExpress Word Processing Document API to add headers and footers to a predefined sample file.
Use checkboxes to indicate whether to add headers, footers, or both. Expand the Header/Footer Settings panel to specify section content and alignment. Use the Add Headers/Footers and Save as... dropdown button to select the output format, apply changes, and download the result.
Sample Document
HeaderFooter_template.docx
Header/Footer Settings
- C#
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
void AddHeader(RichEditDocumentServer wordProcessor, HeaderFooterType headerFooterType, string text, ParagraphAlignment alignment) {
Document document = wordProcessor.Document;
foreach (Section section in document.Sections) {
SubDocument header = section.BeginUpdateHeader(headerFooterType);
header.InsertText(header.Paragraphs[0].Range.Start, text ?? "");
ParagraphProperties props = header.BeginUpdateParagraphs(header.Range);
props.Alignment = alignment;
header.EndUpdateParagraphs(props);
section.EndUpdateHeader(header);
}
}
void AddFooter(RichEditDocumentServer wordProcessor, HeaderFooterType headerFooterType, string text, ParagraphAlignment alignment, bool addPageNumbering) {
Document document = wordProcessor.Document;
foreach (Section section in document.Sections) {
SubDocument footer = section.BeginUpdateFooter(headerFooterType);
footer.InsertText(footer.Paragraphs[0].Range.Start, text ?? "");
if (addPageNumbering) {
footer.Paragraphs.Append();
footer.AppendText("Page ");
footer.Fields.Create(footer.Range.End, "PAGE");
footer.AppendText(" of ");
footer.Fields.Create(footer.Range.End, "NUMPAGES");
}
ParagraphProperties props = footer.BeginUpdateParagraphs(footer.Range);
props.Alignment = alignment;
footer.EndUpdateParagraphs(props);
section.EndUpdateFooter(footer);
}
}