Aspose.words merged document footer formatting issue

I’m evaluating aspose.words before I commit to purchasing it and i’m testing it for merging the header and footer in a notepaper document into a main document. Sometimes the notepaper document will contain page numbers and I’ve noticed formatting issues when its merged into the main document. The issue is only with page numbers and I’ve been using a generic function for the merge to consider all elements so I don’t think the code is the problem.

I think the aspose.words trial watermark in the footer is whats causing the formatting to mess up and move down the page number element over other text and was wondering if somebody could confirm if this is the case? I don’t want to purchase a license and find I have the same issue.

Thanks for your help

Notepaper footer:
notepaper-footer.PNG (4.3 KB)

Main document footer:
main document-footer.PNG (4.5 KB)

After the notepaper footer is merged into the main document:
merge-footer.PNG (13.0 KB)
The page number is over the text.

@athavant

Please get the 30 days temporary license from the following link and apply it before importing documents into Aspose.Words’ DOM.
Get a Temporary License

If you still face problem, please attach the following resources here for testing:

  • Your input Word documents.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a standalone console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

I got the 30 day license with the watermarks removed and am still having the same issue with the footer page number formatting. I’ve attached a zip file containing everything you’ve asked for. I haven’t included my aspose license since its too big to upload and I assume you would have one to use instead.

aspose-testing.zip (272.2 KB)

If anythings not ok could you let me know, thanks for your help!

@athavant

Please note that Aspose.Words mimics the behavior of MS Word. If you copy the content of header/footer from source to destination document using MS Word, you will get the same output.

I’ve tried copying the footer from the notepaper file to the footer in the report document file manually and i’m not having the same page number formatting issue that I have when doing the merge using aspose. Is the issue being investigated for a solution or is this your final answer?

@athavant

Please open “LETTERHEAD.docx” in MS Word and copy the footer content. Open “Commercial-terms.docx” in MS Word and paste the copied content in the footer of this document. This will reproduce the shared issue using MS Word.

However, in your case, we suggest you please insert the nodes of source document footer before the first node of footer in destination document. In your code, you are appending the nodes to the child nodes of footer. You can use following code example to get the desired output. We have attached the output document with this post for your kind reference. Hope this helps you. out.zip (19.8 KB)

Document srcDoc = new Document(MyDir + "Commercial-terms.docx");
Document dstDoc = new Document(MyDir + "LETTERHEAD.docx");

MergeHeaderFooter(srcDoc, dstDoc, HeaderFooterType.FOOTER_PRIMARY);

dstDoc.save(MyDir + "out.docx", SaveFormat.DOCX);

public static void MergeHeaderFooter(Document srcDoc, Document dstDoc, int headerType) throws Exception
{
    HeaderFooter srcfooter =  srcDoc.getFirstSection().getHeadersFooters().getByHeaderFooterType(headerType);
    HeaderFooter dstfooter =  dstDoc.getFirstSection().getHeadersFooters().getByHeaderFooterType(headerType);

    NodeImporter importer =  new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
    Node insertAfterNode = dstfooter.getFirstParagraph();
    CompositeNode dstStory = dstfooter.getFirstParagraph().getParentNode();

    Node node = dstfooter.getFirstChild();
    for (Node srcNode :  (Iterable<Node>)srcfooter.getChildNodes())
    {
        // Let's skip the node if it is a last empty paragraph in a section.
        if (srcNode.getNodeType() == NodeType.PARAGRAPH)
        {
            Paragraph para = (Paragraph) srcNode;
            if (para.isEndOfSection() && !para.hasChildNodes())
                continue;
        }

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

        // Insert new node after the reference node.
        dstStory.insertAfter(newNode, insertAfterNode);
        insertAfterNode = newNode;
    }
}

Thank you for your help but I need the header and footer from the LETTERHEAD document to be in the Commercial-terms document since I need the body text of the commercial terms to be part of the document aswell. So how can I put the notepaper footer under the commercial-terms footer in the commercial-terms document?

@athavant

As shared in my previous post, this is not an issue with Aspose.Words. You are facing the issue with Shape’s position that contains the page field. You can change the position of shape using Aspose.Words. Please check the following code snippet. Hope this helps you.

for(Shape shape : (Iterable<Shape>)dstDoc.getFirstSection().getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY).getChildNodes(NodeType.SHAPE, true))
{
   
    if(shape.hasChildNodes() && shape.getFirstParagraph().getText().toLowerCase().contains("page"))
    {
        shape.setRelativeVerticalPosition (RelativeVerticalPosition.PARAGRAPH);
        shape.setTop(0);
    }
}
dstDoc.save(MyDir + "out.docx", SaveFormat.DOCX);

Thanks for your help. Since I need a generic method this won’t work effectively. I’ve instead tried to completely remove the page number from the notepaper template before the merge using this code:

for (Field field : notepaper.getRange().getFields()) {
							    if (field.getType() == FieldType.FIELD_PAGE) {
							        Paragraph parentParagraph = (Paragraph) field.getStart().getAncestor(NodeType.PARAGRAPH);
							        parentParagraph.remove();
							    }
}

This code removes the actual page number but not the textbox containing the page number so its still problematic after the merge with the empty textbox still blocking other text. Heres a picture of what the footer looks like:
merge-page-number-removed.PNG (7.1 KB)

Could you tell me how I can completely remove the page number and the textbox that contained it in the footer?

@athavant

Please use the following code snippet to get the desired output.

for (Field field : notepaper.getRange().getFields()) {
    if (field.getType() == FieldType.FIELD_PAGE) {
        Shape shape = (Shape) field.getStart().getAncestor(NodeType.SHAPE);
        if(shape != null)
            shape.remove();
    }
}

Thank you, i’ll settle for this. Appreciate your help!