A document can have one or more pages. It is probably easy to add a header for all pages of the document. If you want to add the header only for the first page of the document, Spire.Doc for .NET component can provide you an easy and flexible solution to handle it. The following steps will guide how to add a header into the first page of a document using Spire.Doc for .NET component in C#. In the example, the header is got from an existing document.

Step 1: Load a word document, documen1.docx.

Document document1 = new Document();
document1.LoadFromFile("D:\\document1.docx");

Step 2: Get the header of document1.docx.

HeaderFooter header = document1.Sections[0].HeadersFooters.Header;

Step 3: Load another word document which will be added the header, document2.docx.

Document document2 = new Document();
document2.LoadFromFile("D:\\document2.docx");

Step 4: Get the first page header of document2.docx.

HeaderFooter firstPageHeader = document2.Sections[0].HeadersFooters.FirstPageHeader;

Step 5: Specify that the current section has a different header/footer for the first page.

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

Step 6: Removes all child objects in firstPageHeader.

firstPageHeader.Paragraphs.Clear();

Step 7: Add all child objects of the header to firstPageHeader.

foreach (DocumentObject obj in header.ChildObjects)
{
firstPageHeader.ChildObjects.Add(obj.Clone());
}

Step 8: Save document2.docx to a new document, header.docx.

document2.SaveToFile("D:\\Header.docx"", FileFormat.Docx);

Full code:

Document document1 = new Document();
document1.LoadFromFile(@"..\..\document1.docx");
Document document2 = new Document();
document2.LoadFromFile(@"..\..\document2.docx");
HeaderFooter header = document1.Sections[0].HeadersFooters.Header;
HeaderFooter firstPageHeader = document2.Sections[0].HeadersFooters.FirstPageHeader;

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

firstPageHeader.Paragraphs.Clear();
foreach (DocumentObject obj in header.ChildObjects)
{
firstPageHeader.ChildObjects.Add(obj.Clone());
}

document2.SaveToFile("Header.docx", FileFormat.Docx);

Screenshots:

document1.docx:

Add a header only into the first page

document2.docx:

Add a header only into the first page

Header.docx:

Add a header only into the first page

Published in Header and Footer
Wednesday, 06 April 2011 01:41

PDF Template in C#, VB.NET

The sample demonstrates how to set PDF documentaion template, page header&footer, page number and automatic page number count.

Download Template.pdf

Published in Page
Tuesday, 05 April 2011 09:39

PDF Pagination in C#, VB.NET

The sample demonstrates how to paginate pages, draw page header&footer and set page label in PDF document.

Download Pagination.pdf

Published in Page
Tuesday, 05 April 2011 09:25

PDF TextLayout in C#, VB.NET

The sample demonstrates how to work with text layout in PDF document.

Download TextLayout.pdf

Published in Formating
Tuesday, 21 December 2010 16:34

Insert Header/Footer in Word Document in C#

Word header appears in the top of each page in a document, while Word footer appears in the bottom. Header and footer can be text or images, for example, document title, time and date, page numbers, company logo etc. Also, users can change or format header/footer contents according to own requirement.

Spire.Doc for .NET, a professional component to manipulate Word document with .NET, enables developers to insert header/footer in Word document by using C#/VB.NET. And this guide will show details about how to realize this function.

Users can set header or footer for Word document through HeadersFooters property of Section Class which Spire.Doc for .NET provides. Then, invoke header/footer.AddParagraph() method to add header/footer paragraph and add contents by invoking paragraph.AppendText(string) method. Now Download and Install Spire.Doc for .NET. Then, use the following code to insert header/footer in Word.

Insert Word Header

[C#]
           //Add Header
            Section section = document.Sections[0];
            HeaderFooter header = section.HeadersFooters.Header;
            Paragraph HParagraph = header.AddParagraph();
            TextRange HText = HParagraph.AppendText("Spire.Doc for .NET");

            //Set Header Text Format
            HText.CharacterFormat.FontName = "Algerian";
            HText.CharacterFormat.FontSize = 15;
            HText.CharacterFormat.TextColor = Color.RoyalBlue;

            //Set Header Paragraph Format
            HParagraph.Format.HorizontalAlignment = HorizontalAlignment.Left;
            HParagraph.Format.Borders.Bottom.BorderType=BorderStyle.ThickThinMediumGap;
            HParagraph.Format.Borders.Bottom.Space = 0.05f;
            HParagraph.Format.Borders.Bottom.Color = Color.DarkGray;
[VB.NET]
             'Add Header
            Dim section As Section = document.Sections(0)
            Dim header As HeaderFooter = section.HeadersFooters.Header
            Dim HParagraph As Paragraph = header.AddParagraph()
            Dim HText As TextRange = HParagraph.AppendText("Spire.Doc for .NET")

            'Set Header Text Format
            HText.CharacterFormat.FontName = "Algerian"
            HText.CharacterFormat.FontSize = 15
            HText.CharacterFormat.TextColor = Color.RoyalBlue

            'Set Header Paragraph Format
            HParagraph.Format.HorizontalAlignment = HorizontalAlignment.Left
            HParagraph.Format.Borders.Bottom.BorderType = BorderStyle.ThickThinMediumGap
            HParagraph.Format.Borders.Bottom.Space = 0.05F
            HParagraph.Format.Borders.Bottom.Color = Color.DarkGray

Header Result:

Insert Text Header/Footer

Insert Word Footer

[C#]
           //Add Footer
            HeaderFooter footer = section.HeadersFooters.Footer;
            Paragraph FParagraph = footer.AddParagraph();
            TextRange FText = FParagraph.AppendText("E-iceblue Co., Ltd. 2012 All Rights Reserverd");

            //Set Footer Text Format
            FText.CharacterFormat.FontName = "Calibri";
            FText.CharacterFormat.FontSize = 12;
            FText.CharacterFormat.TextColor = Color.DarkCyan;

            //Set Footer Paragrah Format
            FParagraph.Format.HorizontalAlignment = HorizontalAlignment.Right;
            FParagraph.Format.Borders.Top.BorderType=BorderStyle.ThinThinSmallGap;
            FParagraph.Format.Borders.Top.Space = 0.15f;
            FParagraph.Format.Borders.Color = Color.DarkGray;
[VB.NET]
           'Add Footer
            Dim footer As HeaderFooter = section.HeadersFooters.Footer
            Dim FParagraph As Paragraph = footer.AddParagraph()
            Dim FText As TextRange = FParagraph.AppendText("E-iceblue Co., Ltd. 2012 All Rights Reserverd")

            'Set Footer Text Format
            FText.CharacterFormat.FontName = "Calibri"
            FText.CharacterFormat.FontSize = 12
            FText.CharacterFormat.TextColor = Color.DarkCyan

            'Set Footer Paragrah Format
            FParagraph.Format.HorizontalAlignment = HorizontalAlignment.Right
            FParagraph.Format.Borders.Top.BorderType = BorderStyle.ThinThinSmallGap
            FParagraph.Format.Borders.Top.Space = 0.15F
            FParagraph.Format.Borders.Color = Color.DarkGray

Footer Result:

Insert Text Header/Footer

Spire.Doc is a Microsoft Word component, which enables users to perform a wide range of Word document processing tasks directly, such as generate, read, write and modify Word document in WPF, .NET and Silverlight.

Published in Header and Footer