We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Header footer of source document is not display in destination document. It displays only source header footer

Hi,

I am using Aspose 11.0.0

I am licensed user of Aspose (arvind.bhatt2@halliburton.com). I have 2 document. Source Document A and destination document B. Both document have its own h/f. After mail merging separately A and B i am merging the all nodes of A to B using composite node of B as parent and adding node of A as child (using InserAfter method). Problem is that after adding all child of A the final document B doesn't contain h/f of A.beside it shows h/f of destination document B.

I want to h/f information of A in to B in same way as other child is added. I am expecting follwing stps from you.

1. Take 2 document A and B with separate h/f.

2. Insert the contents of document A with h/f information in middle of document B. say a bookmark "abc" is added in middle of destination document B. I want to insert all node of A with h/f beneath the bookmark "abc" in document B.Contents of A should inserted in middle of document B not in the end of B.

3. Final document should have contents and h/f of A beneath bookmark "abc" and contents and h/f of B.

Doc B

start doc B

..parent node..

bookmark "abc"

(h/f of A should here for all corresponding page)

All child node of A

end doc A

end doc B

thanks

arvind

Hi Arvind,

Thanks for your inquiry. I have created two document A and B as you have explained. I have attached these document with this post.

Unfortunately, I have not understood the detail of expected document. Please manually create your expected Word document using Microsoft Word and attach it here for our reference. We will investigate as to how you want your final Word output be generated like. We will then provide you more information on this along with code.

Thanks for reply.

You did the right thing now please do some more things to understand/resolve of my issue.

1. The document B which created by you has one bookmark say "Bookmark" in middle of document.

2. Now copy all contents of document A (with h/f) and insert it at "Bookmark" place in document B.

Please let me know if you want more info for this.Attached B.docx is the final output which i need. refer to highlighted portion (in yellow color) of the document. which i am expecting to done by aspose programatically.

thanks

arvind

Hi Arvind,


Thanks for sharing the details. Please use the following code snippet to achieve your requirements. Hope this helps you. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.

<span style=“font-size:
10.0pt;font-family:“Courier New”;color:#2B91AF;mso-no-proof:yes”>Document<span style=“font-size:10.0pt;font-family:“Courier New”;mso-no-proof:yes”> docA = new Document(MyDir

  • “A.docx”);<o:p></o:p>

Document docB = new Document(MyDir + "B.docx");

DocumentBuilder builder = new DocumentBuilder(docB);

builder.MoveToBookmark("bm");

Node insertAfterNode = builder.CurrentParagraph;

CompositeNode dstStory = insertAfterNode.ParentNode;

// This object will be translating styles and lists during the import.

NodeImporter importer = new NodeImporter(docA, docB, ImportFormatMode.KeepSourceFormatting);

foreach (Section srcSection in docA.Sections)

{

// Import HeaderFooter

foreach (HeaderFooter hf in srcSection.HeadersFooters)

{

foreach (Node srcNode in hf.ChildNodes)

{

Node newNode = docB.ImportNode(srcNode, true, ImportFormatMode.KeepSourceFormatting);

dstStory.InsertAfter(newNode, insertAfterNode);

insertAfterNode = newNode;

}

}

// Loop through all block level nodes (paragraphs and tables) in the body of the section.

foreach (Node srcNode in srcSection.Body)

{

// Let's skip the node if it is a last empty paragraph in a section.

if (srcNode.NodeType.Equals(NodeType.Paragraph))

{

Paragraph para = (Paragraph)srcNode;

if (para.IsEndOfSection && !para.HasChildNodes)

continue;

}

// This creates a clone of the node, suitable for insertion into the destination document.

Node newNode = importer.ImportNode(srcNode, true);

// Insert new node after the reference node.

dstStory.InsertAfter(newNode, insertAfterNode);

insertAfterNode = newNode;

}

}

docB.Save(MyDir + "AsposeOut.docx");


Hi,

thanks for reply.

i tried code which you sent. this code is importing h/f information from source document(A) to destination document (B). its ok ny intenstion import h/f contents of source as in same h/f format to destination document. copying content of h/f of sorce is not an issue. Issue is to copy h/f of source to destination as h/f.

My simple concern is to insert all content of source (in same format with h/f) doc to destination doc at bookmark place. so finally destination doc should have 2 type of heder one for destination doc itself and other header which is copied from destination.

If requirement is still missing some information please let me know. Please send me sample code which will have such functionality.

-arvind

Hi Arvind,


Thanks for sharing the details. Please note that a Word document can contain one or more sections. At the end of the section, there is a section break that separates one section from the next in a document. Each section has its own set of properties that specify page size, orientation, margins, the number of text columns, headers and footers and so on.

Section can have one Body and maximum one HeaderFooter of each HeaderFooterType. Body and HeaderFooter nodes can be in any order inside Section. Please read following documentation link for your kind reference.
http://www.aspose.com/docs/display/wordsnet/Working+with+Sections

In your case, you can achieve you requirements by using following steups:
  • Move the cursor to bookmark and insert the contents of document A into document B
  • Insert section break before and after the contents of document A so that new header contents of document A can be imported into this section
  • Import the header into this new section

Please check the following code snippet for your kind reference.

<span style=“font-size:
10.0pt;font-family:“Courier New”;color:#2B91AF;mso-no-proof:yes”>Document<span style=“font-size:10.0pt;font-family:“Courier New”;mso-no-proof:yes”> docA = new Document(MyDir

  • “A.docx”);<o:p></o:p>

Document docB = new Document(MyDir + "B.docx");

DocumentBuilder builder = new DocumentBuilder(docB);

builder.MoveToBookmark("bm");

Node insertAfterNode = builder.CurrentParagraph;

Section currentSectionB = (Section)insertAfterNode.GetAncestor(NodeType.Section);

CompositeNode dstStory = insertAfterNode.ParentNode;

// This object will be translating styles and lists during the import.

NodeImporter importer = new NodeImporter(docA, docB, ImportFormatMode.KeepSourceFormatting);

foreach (Section srcSection in docA.Sections)

{

// Loop through all block level nodes (paragraphs and tables) in the body of the section.

foreach (Node srcNode in srcSection.Body)

{

// Let's skip the node if it is a last empty paragraph in a section.

if (srcNode.NodeType.Equals(NodeType.Paragraph))

{

Paragraph para = (Paragraph)srcNode;

if (para.IsEndOfSection && !para.HasChildNodes)

continue;

}

// This creates a clone of the node, suitable for insertion into the destination document.

Node newNode = importer.ImportNode(srcNode, true);

// Insert new node after the reference node.

dstStory.InsertAfter(newNode, insertAfterNode);

insertAfterNode = newNode;

}

}

builder.MoveTo(insertAfterNode);

builder.InsertBreak(BreakType.SectionBreakContinuous);

builder.MoveToBookmark("bm");

builder.InsertBreak(BreakType.SectionBreakContinuous);

Section currentSection = (Section)builder.CurrentSection;

// Import HeaderFooter of document A to B

currentSection.HeadersFooters.Clear();

currentSection.HeadersFooters.LinkToPrevious(false);

foreach (HeaderFooter hf in docA.FirstSection.HeadersFooters)

{

foreach (Node srcNode in hf.ChildNodes.ToArray())

{

HeaderFooter header = currentSection.HeadersFooters[hf.HeaderFooterType];

if (header == null)

{

// There is no header of the specified type in the current section, create it.

header = new HeaderFooter(docB, hf.HeaderFooterType);

currentSection.HeadersFooters.Add(header);

}

Node newNode = docB.ImportNode(srcNode, true, ImportFormatMode.KeepSourceFormatting);

currentSection.HeadersFooters[hf.HeaderFooterType].AppendChild(newNode);

}

}

foreach (Section sec in docB.Sections)

{

if (sec == currentSectionB || sec == currentSection)

continue;

else

{

foreach (HeaderFooter hf in currentSectionB.HeadersFooters)

{

foreach (Node srcNode in hf.ChildNodes.ToArray())

{

HeaderFooter header = sec.HeadersFooters[hf.HeaderFooterType];

if (header == null)

{

// There is no header of the specified type in the current section, create it.

header = new HeaderFooter(docB, hf.HeaderFooterType);

sec.HeadersFooters.Add(header);

}

Node newNode = docB.ImportNode(srcNode, true, ImportFormatMode.KeepSourceFormatting);

sec.HeadersFooters[hf.HeaderFooterType].AppendChild(newNode);

}

}

}

}

docB.Save(MyDir + "AsposeOut.docx");