using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace CreateHeaderAndInsertIntoDocument
{
    class Program
    {
        static void Main(string[] args)
        {
            Document document = new Document();

            document.LoadFromFile("input.docx");

            //insert header
            foreach (Section section in document.Sections)
            {

                InsertHeaderAndFooter(section);

            }

            document.SaveToFile("output.docx", FileFormat.Docx);
        }

        private static void InsertHeaderAndFooter(Section section)
        {

            HeaderFooter header = section.HeadersFooters.Header;

            //insert text to header

            Paragraph headerParagraph = header.Paragraphs[0];

            TextRange text = headerParagraph.AppendText("Demo of Spire.Doc");

            text.CharacterFormat.FontName = "Arial";

            text.CharacterFormat.FontSize = 12;

            text.CharacterFormat.Italic = true;

            headerParagraph.Format.HorizontalAlignment

            = Spire.Doc.Documents.HorizontalAlignment.Left;

        }

    }
}
