Free Support Forum - aspose.com

How to insert Headerfooter in word document

Hi,

We extracting only few paragaraphs from one document and inserting these paragarphs to new document.Here we want to insert same header and footer of the orginal document to new document but oginal document first page header footer are different for remaining pages.using below code we can display first page headerfooter for all pages in new document.can u please suggest me how apply same header footer of orginal document to new document.

We wrote following code for inserting headerfooter

NodeImporter importer = new NodeImporter(originaldoc, newdoc1, ImportFormatMode.KeepSourceFormatting);

newdoc.FirstSection.HeadersFooters.Clear();

foreach (HeaderFooter hf in originaldoc.FirstSection.HeadersFooters)

{

newdoc.FirstSection.HeadersFooters.Add(importer.ImportNode(hf, true));

}

Thanks,

Sailaja.

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. You can try using the following code.

NodeImporter importer = new NodeImporter(originaldoc, newdoc, ImportFormatMode.KeepSourceFormatting);

newdoc.FirstSection.HeadersFooters.Clear();

foreach (HeaderFooter hf in originaldoc.FirstSection.HeadersFooters)

{

newdoc.FirstSection.HeadersFooters.Add(importer.ImportNode(hf, true));

}

newdoc.FirstSection.PageSetup.DifferentFirstPageHeaderFooter = originaldoc.FirstSection.PageSetup.DifferentFirstPageHeaderFooter;

Hope this helps.

Best regards.

Hi,

Thanks for the response.We add the below code in our application

newdoc.FirstSection.PageSetup.DifferentFirstPageHeaderFooter = originaldoc.FirstSection.PageSetup.DifferentFirstPageHeaderFooter;

But it is showing first page headerfooter images to all pages headerfooters of new document.

Here I am attaching two documents for reference .Is there any possibity to implement same table of content styles can apply to new document while pressing F9.

Thanks,

Sailaja.

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks you for additional information.

1. This occurs because original document contains multiple sections. You can try using the following code to solve this problem.

//Open original document

Document originaldoc = new Document(@"Test056\in.doc");

//Create new document

Document newdoc = new Document();

DocumentBuilder builder = new DocumentBuilder(newdoc);

NodeImporter importer = new NodeImporter(originaldoc, newdoc, ImportFormatMode.KeepSourceFormatting);

foreach (Section originalSection in originaldoc.Sections)

{

//reate section in the new document

Section newSection = null;

if (!originalSection.Equals(originaldoc.FirstSection))

{

//Import SectionStart

builder.MoveToDocumentEnd();

switch (originalSection.PageSetup.SectionStart)

{

case SectionStart.Continuous:

builder.InsertBreak(BreakType.SectionBreakContinuous);

break;

case SectionStart.EvenPage:

builder.InsertBreak(BreakType.SectionBreakEvenPage);

break;

case SectionStart.NewColumn:

builder.InsertBreak(BreakType.SectionBreakNewColumn);

break;

case SectionStart.NewPage:

builder.InsertBreak(BreakType.SectionBreakNewPage);

break;

case SectionStart.OddPage:

builder.InsertBreak(BreakType.SectionBreakOddPage);

break;

}

newSection = builder.CurrentSection;

}

else

{

newSection = newdoc.FirstSection;

}

//Remove Headers/Footers from new section

newSection.ClearHeadersFooters();

//import headers and footers

foreach (HeaderFooter hf in originalSection.HeadersFooters)

{

newSection.HeadersFooters.Add(importer.ImportNode(hf, true));

}

newSection.PageSetup.DifferentFirstPageHeaderFooter = originalSection.PageSetup.DifferentFirstPageHeaderFooter;

}

//Save new Document

newdoc.Save(@"Test056\out.doc");

Also you can clone original document. See the following code.

//Open original document

Document originaldoc = new Document(@"Test056\in.doc");

//Create new document

Document newdoc = (Document)originaldoc.Clone(true);

//Crear content

foreach (Section newSection in newdoc.Sections)

{

newSection.Body.ChildNodes.Clear();

//Body can't be absolutely empty

Paragraph par = new Paragraph(newdoc);

newSection.Body.AppendChild(par);

}

//Save new Document

newdoc.Save(@"Test056\out.doc");

2. TOC items have “Hyperlink” style. So you can modify this style. See the following code.

newdoc.Styles[StyleIdentifier.Hyperlink].Font.Name = originaldoc.Styles[StyleIdentifier.Hyperlink].Font.Name;

newdoc.Styles[StyleIdentifier.Hyperlink].Font.Size = originaldoc.Styles[StyleIdentifier.Hyperlink].Font.Size;

newdoc.Styles[StyleIdentifier.Hyperlink].Font.Color = originaldoc.Styles[StyleIdentifier.Hyperlink].Font.Color;

//etc

I hope this could help you.

Best regards.

Hi ,

We implemented your logic in our application but it is applying same header and footer to all pages except last page.Here I am attaching documents please test these document.test1234.doc is new document.We have to place same headerfooter of original document to test1234.doc.

Please give me soultion as earliy as possible.

Document originaldoc = new Document(@"C:\Documents and Settings\user\Desktop\Documents\OriginalDoc.doc");

//Create new document

Document newdoc = new Document(@"C:\Documents and Settings\user\Desktop\test1234.doc");

DocumentBuilder builder = new DocumentBuilder(newdoc);

NodeImporter importer = new NodeImporter(originaldoc, newdoc, ImportFormatMode.KeepSourceFormatting);

foreach (Section originalSection in originaldoc.Sections)

{

//reate section in the new document

Section newSection = null;

if (!originalSection.Equals(originaldoc.FirstSection))

{

//Import SectionStart

builder.MoveToDocumentEnd();

switch (originalSection.PageSetup.SectionStart)

{

// case SectionStart.Continuous:

builder.InsertBreak(BreakType.SectionBreakContinuous);

break;

case SectionStart.EvenPage:

builder.InsertBreak(BreakType.SectionBreakEvenPage);

break;

case SectionStart.NewColumn:

builder.InsertBreak(BreakType.SectionBreakNewColumn);

break;

case SectionStart.NewPage:

builder.InsertBreak(BreakType.SectionBreakNewPage);

break;

case SectionStart.OddPage:

builder.InsertBreak(BreakType.SectionBreakOddPage);

break;

}

newSection = builder.CurrentSection;

}

else

{

newSection = newdoc.FirstSection;

}

//Remove Headers/Footers from new section

newSection.ClearHeadersFooters();

//

// //import headers and footers

foreach (HeaderFooter hf in originalSection.HeadersFooters)

{

newSection.HeadersFooters.Add(importer.ImportNode(hf, true));

}

newSection.PageSetup.DifferentFirstPageHeaderFooter = originalSection.PageSetup.DifferentFirstPageHeaderFooter;

}

// //Save new Document

newdoc.Save("AutoGenerated.doc", SaveFormat.Doc, SaveType.OpenInWord, this.Response);

Thanks,

Sailaja

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks you for additional information. Your newdoc also contains several sections. So you can try using the following code.

Document originaldoc = new Document(@"Test059\OriginalDoc.doc");

//Create new document

Document newdoc = new Document(@"Test059\test1234.doc");

DocumentBuilder builder = new DocumentBuilder(newdoc);

NodeImporter importer = new NodeImporter(originaldoc, newdoc, ImportFormatMode.KeepSourceFormatting);

int sectionIndex = 0;

foreach (Section originalSection in originaldoc.Sections)

{

//reate section in the new document

Section newSection = null;

if(newdoc.Sections[sectionIndex]==null)

{

//Import SectionStart

builder.MoveToDocumentEnd();

switch (originalSection.PageSetup.SectionStart)

{

case SectionStart.Continuous:

builder.InsertBreak(BreakType.SectionBreakContinuous);

break;

case SectionStart.EvenPage:

builder.InsertBreak(BreakType.SectionBreakEvenPage);

break;

case SectionStart.NewColumn:

builder.InsertBreak(BreakType.SectionBreakNewColumn);

break;

case SectionStart.NewPage:

builder.InsertBreak(BreakType.SectionBreakNewPage);

break;

case SectionStart.OddPage:

builder.InsertBreak(BreakType.SectionBreakOddPage);

break;

}

newSection = builder.CurrentSection;

}

else

{

newSection = newdoc.Sections[sectionIndex];

}

//Remove Headers/Footers from new section

newSection.ClearHeadersFooters();

// //import headers and footers

foreach (HeaderFooter hf in originalSection.HeadersFooters)

{

newSection.HeadersFooters.Add(importer.ImportNode(hf, true));

}

newSection.PageSetup.DifferentFirstPageHeaderFooter = originalSection.PageSetup.DifferentFirstPageHeaderFooter;

sectionIndex++;

}

//Save new Document

newdoc.Save(@"Test059\AutoGenerated.doc");

Hope this helps.

Best regards.

Hello.

I have tried to apply what you have explained in the previous post but I have two problems.
I have attached a sample project.

I have a source document in C:\Temp\Header.doc that contains a TOC, an Index and a footer.
The purpose is to create a new document starting from the Header.doc one and have the footer repeated in each page.

The first problem is related to the index. When the target document is created (C:\Temp\TestFooter.doc) and I open this document, I can see the footer in the first page. Once I update the index (right click and update) the footer disappears ! I don't have the same effect with the TOC (I can update it and the footer is still there).

The second problem is a bug in the ApplyFooter function (see project). The application crashes in the

destSection.HeadersFooters.Add(importer.ImportNode(hf, false));

statement. The error message is Cannot insert a node of this type at this location. The ClearHeadersFooters method doesn't seem to clear the header and footers (I have 2 h/f before and 2 h/f after the clear).

Basically what I'm doing is to loop through all sections of the new document and I apply the header and footer to the section.

Can you have a look at my project and tell me what is wrong ?

I really would appreciate

Thanks
Regards
Augusto

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry.

1. I think you should use

destSection.HeadersFooters.Add(importer.ImportNode(hf, true));

2. Try using

destSection.HeadersFooters.Clear();

instead

destSection.ClearHeadersFooters();

I attached my output document.

Best regards.

Alexey,

Thanks a lot. It works as expeted !

Best regards
Augusto

Hi ,

I have one problem with header and footer.The problem is header footer of original documents not excatly replicate in new document. The space between the rule and the logo is too deep - it is pushing the logo into the bottom copy. The distance between the rule and the logo should be about 1/8.Please fix this one.Here I am attaching the documents and image for reference.

I would like to apply the same style of toc1 and toc2 of orginal document (MS PROP_Part (1).doc) to Autogenrated.doc.We applied all styles of old doc to new doc but we could not set the sub heading 1 margins.Please help me how to set margin.

here is the code that we applied to new doc.

testdoc.Styles[StyleIdentifier.Toc1].Font.Name=doc.Styles[StyleIdentifier.Toc1].Font.Name;

testdoc.Styles[StyleIdentifier.Toc1].Font.Bold =doc.Styles[StyleIdentifier.Toc1].Font.Bold;

testdoc.Styles[StyleIdentifier.Toc1].Font.AllCaps =doc.Styles[StyleIdentifier.Toc1].Font.AllCaps;

testdoc.Styles[StyleIdentifier.Toc1].ParagraphFormat.Alignment=doc.Styles[StyleIdentifier.Toc1].ParagraphFormat.Alignment;

testdoc.Styles[StyleIdentifier.Toc1].Font.Size=doc.Styles[StyleIdentifier.Toc1].Font.Size;

testdoc.Styles[StyleIdentifier.Toc1].ParagraphFormat.LineSpacing=doc.Styles[StyleIdentifier.Toc1].ParagraphFormat.LineSpacing;

testdoc.Styles[StyleIdentifier.Toc1].ParagraphFormat.SpaceAfter=doc.Styles[StyleIdentifier.Toc1].ParagraphFormat.SpaceAfter;

testdoc.Styles[StyleIdentifier.Toc1].ParagraphFormat.SpaceBefore=doc.Styles[StyleIdentifier.Toc1].ParagraphFormat.SpaceBefore;

testdoc.Styles[StyleIdentifier.Toc1].ParagraphFormat.LeftIndent=doc.Styles[StyleIdentifier.Toc1].ParagraphFormat.LeftIndent;

testdoc.Styles[StyleIdentifier.Toc1].ParagraphFormat.RightIndent=doc.Styles[StyleIdentifier.Toc1].ParagraphFormat.RightIndent;

// testdoc.Styles[StyleIdentifier.Toc2].ParagraphFormat.TabStops =doc.Styles[StyleIdentifier.Toc2].ParagraphFormat.TabStops.After;

// testdoc.Styles[StyleIdentifier.Toc1].ParagraphFormat.TabStops=doc.Styles[StyleIdentifier.Toc1].ParagraphFormat.TabStops;

testdoc.Styles[StyleIdentifier.Toc2].Font.Name=doc.Styles[StyleIdentifier.Toc2].Font.Name;

testdoc.Styles[StyleIdentifier.Toc2].Font.Size=doc.Styles[StyleIdentifier.Toc2].Font.Size;

testdoc.Styles[StyleIdentifier.Toc2].ParagraphFormat.Alignment=doc.Styles[StyleIdentifier.Toc2].ParagraphFormat.Alignment;

testdoc.Styles[StyleIdentifier.Toc2].ParagraphFormat.LineSpacing=doc.Styles[StyleIdentifier.Toc2].ParagraphFormat.LineSpacing;

testdoc.Styles[StyleIdentifier.Toc2].ParagraphFormat.SpaceAfter=doc.Styles[StyleIdentifier.Toc2].ParagraphFormat.SpaceAfter;

testdoc.Styles[StyleIdentifier.Toc2].ParagraphFormat.SpaceBefore=doc.Styles[StyleIdentifier.Toc2].ParagraphFormat.SpaceBefore;

testdoc.Styles[StyleIdentifier.Toc2].ParagraphFormat.LeftIndent=doc.Styles[StyleIdentifier.Toc2].ParagraphFormat.LeftIndent;

testdoc.Styles[StyleIdentifier.Toc2].ParagraphFormat.RightIndent=doc.Styles[StyleIdentifier.Toc2].ParagraphFormat.RightIndent;

// testdoc.Styles[StyleIdentifier.Toc2].ParagraphFormat.TabStops =doc.Styles[StyleIdentifier.Toc2].ParagraphFormat.TabStops.After;

// testdoc.Styles[StyleIdentifier.Toc2].ParagraphFormat.TabStops =doc.Styles[StyleIdentifier.Toc2].ParagraphFormat.TabStops;

testdoc.Styles[StyleIdentifier.Toc3].Font.Name=doc.Styles[StyleIdentifier.Toc3].Font.Name;

testdoc.Styles[StyleIdentifier.Toc3].Font.Size=doc.Styles[StyleIdentifier.Toc3].Font.Size;

testdoc.Styles[StyleIdentifier.Toc3].ParagraphFormat.Alignment=doc.Styles[StyleIdentifier.Toc3].ParagraphFormat.Alignment;

testdoc.Styles[StyleIdentifier.Toc3].ParagraphFormat.LineSpacing=doc.Styles[StyleIdentifier.Toc2].ParagraphFormat.LineSpacing;

testdoc.Styles[StyleIdentifier.Toc3].ParagraphFormat.SpaceAfter=doc.Styles[StyleIdentifier.Toc3].ParagraphFormat.SpaceAfter;

testdoc.Styles[StyleIdentifier.Toc3].ParagraphFormat.SpaceBefore=doc.Styles[StyleIdentifier.Toc3].ParagraphFormat.SpaceBefore;

testdoc.Styles[StyleIdentifier.Toc3].ParagraphFormat.LeftIndent=doc.Styles[StyleIdentifier.Toc3].ParagraphFormat.LeftIndent;

testdoc.Styles[StyleIdentifier.Toc3].ParagraphFormat.RightIndent=doc.Styles[StyleIdentifier.Toc3].ParagraphFormat.RightIndent;

// testdoc.Styles[StyleIdentifier.Toc3].ParagraphFormat.TabStops =doc.Styles[StyleIdentifier.Toc3].ParagraphFormat.TabStops

Thanks,

Sailaja.

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request.

1. Try also import FooterDistance and HeaderDistance. See the following code snippet.

newSection.PageSetup.FooterDistance = originalSection.PageSetup.FooterDistance;

newSection.PageSetup.HeaderDistance = originalSection.PageSetup.HeaderDistance;

2. I answered the second question in another thread.

http://www.aspose.com/community/forums/thread/119174/how-to-set-the-tab-postion-of-table-os-contents.aspx

Best regards.