Copy Header/Footer information from 1 document to another document

Hello,

I have 2 documents (A and B).

In document A I have defined a Header/Footer section, with Text blocks. I would like to copy/append the header/footer information from Document A to document B. It is possible that document B also has a Header/Footer defined.

Could you please give me an example. I tried many things but none of them worked.

re bert

Hi

Thanks for your inquiry. Yes of course you can achieve this using Aspose.Words. You can try using the following code:

//Open A document
Document docA = new Document(@"Test042\a.doc");
DocumentBuilder builderA = new DocumentBuilder(docA);
//Open B document
Document docB = new Document(@"Test042\b.doc");
//Loop through all sections in the B document
foreach (Section sectB in docB.Sections)
{
    Section sectA = null;
    int sectBIndex = docB.Sections.IndexOf(sectB);
    //Check whether document A conteins section with same index
    if (docA.Sections.Count > sectBIndex)
    {
        //Get correcponding section
        sectA = docA.Sections[sectBIndex];
    }
    else
    {
        //Insert empty section
        builderA.MoveToDocumentEnd();
        builderA.InsertBreak(BreakType.SectionBreakContinuous);
        sectA = builderA.CurrentSection;
    }
    //Loop throught all Headers/Footers in the B document
    foreach (HeaderFooter hfB in sectB.HeadersFooters)
    {
        //Check whether current section from docA conteins 
        //Header/Footer with the same type as h/f from docB
        if (sectA.HeadersFooters[hfB.HeaderFooterType] != null)
        {
            //Append content from h/f B to h/f A
            foreach (Node childB in hfB.ChildNodes)
            {
                //Import node
                Node childA = docA.ImportNode(childB, true, ImportFormatMode.KeepSourceFormatting);
                //Appent node to h/f
                sectA.HeadersFooters[hfB.HeaderFooterType].AppendChild(childA);
            }
        }
        else
        {
            //Copy whole h/f
            Node hfA = docA.ImportNode(hfB, true, ImportFormatMode.KeepSourceFormatting);
            //Insert h/f
            sectA.HeadersFooters.Add(hfA);
        }
    }
}
//Save output document
docA.Save(@"Test042\out.doc");

Hope this helps.

Best regards.

Hi,

Great. That works. Thanks for the fast response

re bert

Hello,

it worked almost. I have an example where it doesn’t work. I have 2 documents attached. Please try and let me know what the problem is.

I have used the following code (When I come into the green part, it doesn’t work):

private Document WordMerge(Document docA, Document docB)
{
    DocumentBuilder builderA = new DocumentBuilder(docA);
    //Loop through all sections in the B document
    foreach (Aspose.Words.Section sectB in docB.Sections)
    {
        Aspose.Words.Section sectA = null;
        int sectBIndex = docB.Sections.IndexOf(sectB);
        //Check whether document A contains section with same index
        if (docA.Sections.Count > sectBIndex)
        {
            //Get correcponding section
            sectA = docA.Sections[sectBIndex];
        }
        else
        {
            //Insert empty section
            builderA.MoveToDocumentEnd();
            builderA.InsertBreak(BreakType.SectionBreakContinuous);
            sectA = builderA.CurrentSection;
        }
        //Loop throught all Headers/Footers in the B document
        foreach (Aspose.Words.HeaderFooter hfB in sectB.HeadersFooters)
        {
            //Check whether current section from docA contains 
            //Header/Footer with the same type as h/f from docB
            if (sectA.HeadersFooters[hfB.HeaderFooterType] != null)
            {
                //Append content from h/f B to h/f A
                foreach (Node childB in hfB.ChildNodes)
                {
                    //Import node
                    Node childA = docA.ImportNode(childB, true, ImportFormatMode.KeepSourceFormatting);
                    //Append node to h/f
                    sectA.HeadersFooters[hfB.HeaderFooterType].AppendChild(childA);
                }
            }
            else
            {
                //Copy whole h/f
                Node hfA = docA.ImportNode(hfB, true, ImportFormatMode.KeepSourceFormatting);
                //Insert h/f
                sectA.HeadersFooters.Add(hfA);
            }
        }
    }
    //builderA.Document.Save(@"z:\tmp\xyz.doc");
    return builderA.Document;
}

BackGround.dot is docB

the 0062873 is the document which need the header footer from BackGround.dot

bert

Hi

Thanks for your inquiry. I tested your code and your documents and it seems that all works fine on my side. Here is code for testing:

Document docA = new Document(@"Test058\00062873.doc");
Document docB = new Document(@"Test058\BackGround.dot");
Document doc = WordMerge(docA, docB);
doc.Save(@"Test058\out.doc");

Best regards.

Hi

when I test the same (both on my development machine as well at the production machine) I get the document without the background.

I have original documents which works ok, however all these documents always go to the else part below the green part.

This document (and more) go into the green part and nothing happens.

I am using Aspose.Word 5.2.0.0 7/4/2008

re

Hi

Thank you for additional information. It seems that you attached wrong file in the previous post. The problem occurs because there is different First and Primary header in the docA. I think you should add the following two lines of code and this will solve the problem

sectA.PageSetup.DifferentFirstPageHeaderFooter = sectB.PageSetup.DifferentFirstPageHeaderFooter;
sectA.PageSetup.OddAndEvenPagesHeaderFooter = sectB.PageSetup.OddAndEvenPagesHeaderFooter;

Best regards.

Hi,

now it is working. Thanks.

Hello, I have the same problem: to copy one document header & footer to another. The code works fine, except the situation when I have a picture (company logo) in the header. The code didn’t throw any error, but the resulting document is not well formed and can’t be opened. Try to put a logo in the source header and see what happened. I have attached the document with the header and footer and company logo. From this document I want to copy the header and footer to other documents.

Hi

Thanks for your request. I cannot reproduce the problem on my side. I use code provided in my first post in this thread and the latest version of Aspose.Words (6.0.1) for testing.

Maybe there is something wrong with your destination document. Could you please attach it for testing? Also, try using the latest version of Aspose.Words.

Best regards,

hello, I have attached Blank.doc (the destination document) and ErrorBlank.doc (the file resulted after copying header and footer from the template). In have noticed that the problem occur only when the destination file has no header and footer. If it has any header or footer, the operation goes well, even if there is an image in the header.

Thank you

I have made further tests, and noticed that it works only when the destination has also an image in the header. I clear the destination file HeadersFooters using

sectA.ClearHeadersFooters();

before adding the new header. When it already have an image header, the replacement works fine, but if it has only text header or no header at all, it generates the error.

My users should create their own templates, but the Header and Footer must be specified by the application (company logo, department, address, registration number etc).

Hi

Thank you for additional information. I still cannot reproduce the problem on my side. Header and footer successfully added to Blank.doc.

Please make sure you are using the latest version of Aspose.Words.

Best regards.

Hello, try this one (attached) as a template source for the destination document header. My client use this header fol all documents. I have the same error. It is something related to the images in the header. If I delete all the images and put a small one, or no image, it works.

Thank you for your prompt answer!

Hi

Thank you for additional information, but I still cannot reproduce the problem on my side. Could you please provide me your code? Maybe there is some small difference between your code and my code that causes the problem.

Best regards.

Hello again, I have attached the source code, with comments. The scenario is the following: users can create doc templates themselves, stored into the database. This templates could be (at the users choise):

  1. with some predefined mergefields and without header & footer; in this case the header&footer is copy from a template stored into the database, at a department level, or organization level;

  2. with some predefined mergefields and withs own header & footer; in this case there is no need to copy the header&footer.

The header & footer could also have some mergefields (compartment/organization address, phone, fax etc).

The problem is that for some header&footer (like the last one I have sent to you) the operation doesn’t go well, the resulting document is not well formed, and could not be open with word. But if I replase the images in the header&footer with a small one, it works. Maybe is something related to the images, I don’t know.

Anothe question: wich is the maximum number of mergefields/document? There is a limit? I want to offer the users a list of predefined fields names they could use in their templates.

Tnank you very much!

Hi

Thank you for additional information. Unfortunately, I still have no luck to reproduce this problem. Maybe you can create simple application that will demonstrate the issue.

Regarding number of merge fields per document, it seems to be there is no limit.

Best regards.

Now I realised that I forgot to mention a “small detail”: I open the document with WebDAV. When I try to open it with the “normal” way (without WebDAV) in browser, it works, so Asposo do his job well. But with WebDAV, it doesn’t allways work: it depends of the images in the document header. If I make the images smaller (in size) it works. It was not from Aspose, it was a webDAV problem.

Thank you for your prompt support!

Hi Alexey Noskov,

Using the below code i am able to copy the header and footer information from one document to another document. But if I want to copy only header information [don’t copy Footer information] from docA to docB how can i do.

Can you please help me.

//Open A document
Document docA = new Document(@"Test042\a.doc");
DocumentBuilder builderA = new DocumentBuilder(docA);
//Open B document
Document docB = new Document(@"Test042\b.doc");
//Loop through all sections in the B document
foreach (Section sectB in docB.Sections)
{
    Section sectA = null;
    int sectBIndex = docB.Sections.IndexOf(sectB);
    //Check whether document A conteins section with same index
    if (docA.Sections.Count > sectBIndex)
    {
        //Get correcponding section
        sectA = docA.Sections[sectBIndex];
    }
    else
    {
        //Insert empty section
        builderA.MoveToDocumentEnd();
        builderA.InsertBreak(BreakType.SectionBreakContinuous);
        sectA = builderA.CurrentSection;
    }
    //Loop throught all Headers/Footers in the B document
    foreach (HeaderFooter hfB in sectB.HeadersFooters)
    {
        //Check whether current section from docA conteins 
        //Header/Footer with the same type as h/f from docB
        if (sectA.HeadersFooters[hfB.HeaderFooterType] != null)
        {
            //Append content from h/f B to h/f A
            foreach (Node childB in hfB.ChildNodes)
            {
                //Import node
                Node childA = docA.ImportNode(childB, true, ImportFormatMode.KeepSourceFormatting);
                //Appent node to h/f
                sectA.HeadersFooters[hfB.HeaderFooterType].AppendChild(childA);
            }
        }
        else
        {
            //Copy whole h/f
            Node hfA = docA.ImportNode(hfB, true, ImportFormatMode.KeepSourceFormatting);
            //Insert h/f
            sectA.HeadersFooters.Add(hfA);
        }
    }
}
//Save output document
docA.Save(@"Test042\out.doc");

Hi Kalyan,

Thanks for your inquiry. To skip copying content of all Footers from one document to another, please insert the following checks in your code:

…
...
//Loop throught all Headers/Footers in the B document
foreach (HeaderFooter hfB in sectB.HeadersFooters)
{
    if (!(hfB.HeaderFooterType.Equals(HeaderFooterType.FooterFirst) ||
        hfB.HeaderFooterType.Equals(HeaderFooterType.FooterPrimary) ||
        hfB.HeaderFooterType.Equals(HeaderFooterType.FooterEven)))
{
    //Check whether current section from docA conteins
    //Header/Footer with the same type as h/f from docB
    if (sectA.HeadersFooters[hfB.HeaderFooterType] != null)
    {
        ...
...

I hope, this helps.

Best regards,