Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Fri Mar 15, 2013 7:25 am

How can i access the header of a document, and to insert something there only if its not the first page?
Thank you.

catalin
 
Posts: 48
Joined: Wed Mar 14, 2012 12:56 pm

Fri Mar 15, 2013 9:59 am

Dear Catalin,

Thanks for your inquiry.
Please try sample code below to implement it.
Code: Select all
           Document document = new Document();
            document.LoadFromFile("sample.docx");
            foreach (Section section in document.Sections)
            {
                Paragraph p = section.HeadersFooters.Header.AddParagraph();
                TextRange txt= p.AppendText("HelloWorld");
                txt.CharacterFormat.FontName = "Arial";
                txt.CharacterFormat.FontSize = 10;
                txt.CharacterFormat.Italic = true;
                p.Format.HorizontalAlignment = HorizontalAlignment.Right;

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

If the code above doesn't solve your problem, please tell us.

Best regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2912
Joined: Wed Jun 27, 2012 8:50 am

Fri Mar 15, 2013 10:20 am

This is a generic way to insert a paragraph in the header. That's fine, but what i want is that the header of the first page remains intact and only following page headers will have some text. Is that possible?

catalin
 
Posts: 48
Joined: Wed Mar 14, 2012 12:56 pm

Mon Mar 18, 2013 10:07 am

Dear Catalin,

Thanks for your feedback.
Please refer to the code snippet to implement it.
Code: Select all
 Document document = new Document();
            document.LoadFromFile(@"..\..\sample.docx");

            foreach (Section section in document.Sections)
            {
                section.PageSetup.DifferentFirstPageHeaderFooter = true;
            }

            HeaderFooter ss = document.Sections[0].HeadersFooters.Header;
            HeaderFooter header = document.Sections[0].HeadersFooters.FirstPageHeader;
            header.Paragraphs.Clear();

            foreach (Paragraph p in ss.Paragraphs)
            {
                header.Paragraphs.Add(p.Clone() as Paragraph);
            }
            foreach (Section section in document.Sections)
            {
                HeaderFooter head = section.HeadersFooters.Header;
                head.LinkToPrevious = false;
                Paragraph p = head.AddParagraph();
                TextRange txt = p.AppendText("HelloWorld");
                txt.CharacterFormat.FontName = "Arial";
                txt.CharacterFormat.FontSize = 10;
                txt.CharacterFormat.Italic = true;
                p.Format.HorizontalAlignment = HorizontalAlignment.Right;
            }
            document.SaveToFile("output.docx", FileFormat.Docx);


If you need any assistance, please tell us.

Best Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2912
Joined: Wed Jun 27, 2012 8:50 am

Wed Mar 20, 2013 10:24 am

Dear Catalin,

Does the code in our last post solve your issue?
Please give us a feedback on this issue at your early convenience. Thank you!
If you have any issue, please tell us.

Best Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2912
Joined: Wed Jun 27, 2012 8:50 am

Thu Apr 04, 2013 7:57 am

amy.zhao wrote:Dear Catalin,

Does the code in our last post solve your issue?
Please give us a feedback on this issue at your early convenience. Thank you!
If you have any issue, please tell us.

Best Regards,
Amy
E-iceblue support team


Yes, my issue is now solved. Thank you very much.

catalin
 
Posts: 48
Joined: Wed Mar 14, 2012 12:56 pm

Thu Apr 04, 2013 8:25 am

Dear catalin,

Thanks for your feedback.
If you have any other questions, please feel free to contact us.
Have a nice day!

Best regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2912
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.Doc

cron