Copy FOOTER_PRIMARY to FOOTER_FIRST in same section

For our client we need to convert a word document (in doc format) to docx format and we need to adjust the footer. The original document has only 1 section with a primary footer that contains a table (2 cells, 1 row)… The docx should contain a different Footer on page 1 (only page number in cell 2 should be removed from the table). I want to copy the complete table to the FOOTER_FIRST from the FOOTER_PRIMARY and then go to documentBuilder.moveToCell(0,0,1,0), to remove the content… But it seems that this is not possible ? If I move to FOOTER_FIRST and try a documentBuilder.insertNode(myTable), he gives an error: This node can not be inserted at this place).

HeaderFooter footer = currentSection.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY);
Table table = footer.getTables().get(0);
pageSetup.setDifferentFirstPageHeaderFooter(true);
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_FIRST);
builder.insertNode(table);

table is a valid object, when I go in debug mode…

Is there a way to copy my complete table from FOOTER_PRIMARY to FOOTER_FIRST

Hi,

Thanks for your inquiry. Please try running the following snippet to be able to copy the content from FooterPrimary of source document to the FooterFirst of destination document:

Document srcDoc = new Document(@"C:\Temp\in.doc");
Document dstDoc = new Document();
dstDoc.FirstSection.PageSetup.DifferentFirstPageHeaderFooter = true;
DocumentBuilder dstBuilder = new DocumentBuilder(dstDoc);
HeaderFooter footer = new HeaderFooter(dstDoc, HeaderFooterType.FooterFirst);
dstDoc.FirstSection.HeadersFooters.Add(footer);
foreach (Node srcNode in srcDoc.FirstSection.HeadersFooters[HeaderFooterType.FooterPrimary].ChildNodes)
{
    Node dstNode = dstDoc.ImportNode(srcNode, true, ImportFormatMode.KeepSourceFormatting);
    footer.AppendChild(dstNode);
}
dstBuilder.MoveToHeaderFooter(HeaderFooterType.FooterFirst);
dstBuilder.MoveToCell(0, 0, 1, 0);
dstBuilder.Write("New content at the start ...");
dstDoc.Save(@"C:\Temp\out.docx");

I hope, this helps.

Best Regards,

Many thanks for this code snippet. The copy of the Footer works fine, but in my dstDoc, all content has been lost.

Is there an easy way to move all content of the document from srcDoc to dstDoc ?

Many thanks.

I’ve found the following code:

Section srcSection = srcDoc.getFirstSection();
dstDoc.getFirstSection().appendContent(srcSection);

but then the format of some text is changed…

Attached you can find the In.doc and Out.docx

Is there a way to have a clean copy, with no change in format ?

Many thanks

Hi,

Thanks for your inquiry. The code suggested in my previous post just copies the content from FooterPrimary of source document to the FooterFirst of destination document. However, it doesn’t copy the page setup attributes of a section (left margin, bottom margin, paper size, and so on). To be able to achieve 100% fidelity, in your case, please take these attributes into account.

For example, in your ‘in.docx’, the ‘page margins’ and ‘Header/Footer positions’ are as follows:

Page Margins: Top: 1.57", Bottom: 1.44", Left: 1.23" and Right: 0.79"

Header/Footer positions: Header from top: 0.51" and Footer from bottom: 0.24"

If you change the values in your ‘out.docx’ to the values mentioned above, then this will improve the final output.

Please let me know if I can be of any further assistance.

Best regards,

Hi,

Thanks for your reply.

I now have the following code, to copy the Footer and maintain the content of the document:

Document srcDoc = new Document(docUri);
Document dstDoc = new Document();
dstDoc.removeAllChildren();
dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
dstDoc.getFirstSection().removeChild(dstDoc.getFirstSection().getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_FIRST));
dstDoc.getFirstSection().removeChild(dstDoc.getFirstSection().getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY));
dstDoc.getFirstSection().removeChild(dstDoc.getFirstSection().getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_EVEN));
dstDoc.getFirstSection().getPageSetup().setDifferentFirstPageHeaderFooter(true);
dstDoc.getFirstSection().getPageSetup().setOddAndEvenPagesHeaderFooter(true);
DocumentBuilder dstBuilder = new DocumentBuilder(dstDoc);
dstBuilder.moveToHeaderFooter(HeaderFooterType.HEADER_FIRST);
String imageFileName = "C:\Word\Logo1.gif";
dstBuilder.insertImage(imageFileName, RelativeHorizontalPosition.PAGE, 32.5, RelativeVerticalPosition.PAGE, 25, 233.3333333333334, 50, WrapType.THROUGH);
dstBuilder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
imageFileName = "C:\Word\Logo2.gif";
// dstBuilder.insertImage(imageFileName, 40, 40);
dstBuilder.insertImage(imageFileName, RelativeHorizontalPosition.PAGE, 32.5, RelativeVerticalPosition.PAGE, 25, 40, 40, WrapType.THROUGH);
HeaderFooter footer = new HeaderFooter(dstDoc, HeaderFooterType.FOOTER_FIRST);
dstDoc.getFirstSection().getHeadersFooters().add(footer);
Node srcNode;
Node dstNode;
for (Iterator iterator = srcDoc.getFirstSection().getHeadersFooters().iterator(); iterator.hasNext() {
    HeaderFooter type = (HeaderFooter)iterator.next();
    if (type.getHeaderFooterType() == HeaderFooterType.FOOTER_PRIMARY)
    {
        for (Iterator iterator2 = type.getChildNodes().iterator(); iterator2.hasNext() {
            srcNode = (Node)iterator2.next();
            dstNode = dstDoc.importNode(srcNode, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
            footer.appendChild(dstNode);
        }
    }
}
footer = new HeaderFooter(dstDoc, HeaderFooterType.FOOTER_PRIMARY);
dstDoc.getFirstSection().getHeadersFooters().add(footer);
for (Iterator iterator = srcDoc.getFirstSection().getHeadersFooters().iterator(); iterator.hasNext() {
    HeaderFooter type = (HeaderFooter)iterator.next();
    if (type.getHeaderFooterType() == HeaderFooterType.FOOTER_PRIMARY)
    {
        for (Iterator iterator2 = type.getChildNodes().iterator(); iterator2.hasNext() {
            srcNode = (Node)iterator2.next();
            dstNode = dstDoc.importNode(srcNode, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
            footer.appendChild(dstNode);
        }
    }
}
footer = new HeaderFooter(dstDoc, HeaderFooterType.FOOTER_EVEN);
dstDoc.getFirstSection().getHeadersFooters().add(footer);
for (Iterator iterator = srcDoc.getFirstSection().getHeadersFooters().iterator(); iterator.hasNext() {
    HeaderFooter type = (HeaderFooter)iterator.next();
    if (type.getHeaderFooterType() == HeaderFooterType.FOOTER_PRIMARY)
    {
        for (Iterator iterator2 = type.getChildNodes().iterator(); iterator2.hasNext() {
            srcNode = (Node)iterator2.next();
            dstNode = dstDoc.importNode(srcNode, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
            footer.appendChild(dstNode);
        }
    }
}
dstBuilder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
dstBuilder.moveToCell(0, 0, 1, 0);
dstBuilder.insertField("PAGE", "");
dstBuilder.write("/");
dstBuilder.insertField("NUMPAGES", "");
dstBuilder.moveToHeaderFooter(HeaderFooterType.FOOTER_EVEN);
dstBuilder.moveToCell(0, 0, 1, 0);
dstBuilder.insertField("PAGE", "");
dstBuilder.write("/");
dstBuilder.insertField("NUMPAGES", "");
dstDoc.save(docUri.substring(0, docUri.length() - 4) + "_result.docx");

The only problem I still have, is that below my table (which is in my footer) he adds an paragraph. This paragraph is not in my srcDoc, nor in my PrimaryFooter. But it appears in the FOOTER_FIRST and FOOTER_EVEN in the dstDoc. Because of this paragraph, you can see that the last page from the srcDoc is now on 2 pages in the dstDoc… Is there any way to get rid of this ?

Attached you can find the srcDoc and dstDoc, the logo’s I add in the different headers, are placed hard coded in my C:\Word for now.

Many thanks for your reply, it really feels that I’m close to a complete solution for our costumer.

Regards

Hi,

Thanks for your inquiry. I think in your case you can just remove those paragraphs from the end of FooterPrimary and FooterEven. You can do this by using the following code snippet:

dstBuilder.moveToHeaderFooter(HeaderFooterType.FOOTER_EVEN);
dstBuilder.moveToCell(0, 0, 1, 0);
dstBuilder.insertField("PAGE", "");
dstBuilder.write("/");
dstBuilder.insertField("NUMPAGES", "");
HeaderFooter footerPrimary = dstDoc.getFirstSection().getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY);
footerPrimary.getLastParagraph().remove();
HeaderFooter footerEven = dstDoc.getFirstSection().getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_EVEN);
footerEven.getLastParagraph().remove();
dstDoc.save(docUri.substring(0, docUri.length() - 4) + "_result.docx");

I hope, this helps.

Best regards,