The paragraph align changed after copy headers from second section to first secition

Document word = new Document("C:\\Users\\z_jia\\Desktop\\a\\a.docx");
Document coverWord = new Document("C:\\Users\\z_jia\\Desktop\\a\\b.docx");
word.getFirstSection().getPageSetup().setSectionStart(SectionStart.NEW_PAGE);
coverWord.appendDocument(word, ImportFormatMode.KEEP_SOURCE_FORMATTING);
word = coverWord;
var sections = word.getSections();
if (sections.getCount() > 1)
{
    Section section1 = sections.get(0);

    section1.getPageSetup().setRestartPageNumbering(false);
    Section section2 = sections.get(1);
    HeaderFooterCollection headersFooters2 = section2.getHeadersFooters();
    for (int z = 0; z < headersFooters2.getCount(); z++)
    {
        HeaderFooter headerFooter2 = headersFooters2.get(z);
        Node clonedHeaderFooter = word.importNode(headerFooter2, true);
        section1.getHeadersFooters().add(clonedHeaderFooter);
    }

    section1.getPageSetup().setRestartPageNumbering(false);
    section2.getPageSetup().setRestartPageNumbering(false);

}

word.save("C:\\Users\\z_jia\\Desktop\\a\\out.docx", SaveFormat.DOCX);

b.docx (16.4 KB)

a.docx (57.0 KB)

out.docx (42.6 KB)

The paragraph is at right in a.docx :grinning:

after the appended and copied, they are at left in out.docx:


@GusGus

It seems that after copying headers from the second section to the first section, the paragraph alignment has changed unexpectedly. This can happen due to various reasons related to how headers and footers are managed in the document.

To address this issue, you can ensure that the paragraph formatting in the headers of the first section is explicitly set after copying the headers. Here’s a general approach you can take:

  1. Check Paragraph Alignment: After copying the headers, explicitly set the paragraph alignment for the headers in the first section to match your desired alignment (e.g., right alignment).

  2. Use DocumentBuilder: If you are using DocumentBuilder, you can set the alignment like this:

    Document doc = new Document("C:\\Users\\z_jia\\Desktop\\a\\out.docx");
    DocumentBuilder builder = new DocumentBuilder(doc);
    
    // Move to the header of the first section
    builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
    builder.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT); // Set to right alignment
    
  3. Reapply Formatting: After copying the headers, you may want to loop through the headers in the first section and reapply the desired formatting to ensure consistency.

Here’s a code snippet that demonstrates how to copy headers and set the alignment:

Section section1 = sections.get(0);
Section section2 = sections.get(1);
HeaderFooterCollection headersFooters2 = section2.getHeadersFooters();

for (int z = 0; z < headersFooters2.getCount(); z++) {
    HeaderFooter headerFooter2 = headersFooters2.get(z);
    Node clonedHeaderFooter = word.importNode(headerFooter2, true);
    section1.getHeadersFooters().add(clonedHeaderFooter);
}

// Set alignment for the copied headers
for (HeaderFooter headerFooter : section1.getHeadersFooters()) {
    for (Paragraph paragraph : headerFooter.getChildNodes(NodeType.PARAGRAPH, true)) {
        paragraph.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT); // Set to right alignment
    }
}

By ensuring that the paragraph alignment is explicitly set after copying the headers, you should be able to maintain the desired formatting in your document. If the issue persists, consider checking other formatting settings that might be affecting the alignment.

If you need further assistance, feel free to ask!

I couldn’t used this code, I can not assume all paragraph is all at right.

@GusGus
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSNET-27722

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

@GusGus We have completed analyzing the issue. And we are going to close this issue as Not a Bug.
The output in MS Word and Aspose.Words is the same. The reason of changed alignment is that problematic paragraph has built-in style Footer that has different alignment in source and destination documents.

If you want to preserve source document formatting and layout, then please try our Merger feature:

Document docA = new Document("a.docx");
Document docB = new Document("b.docx");

Document mergedDoc = Merger.Merge(new [] { docB, docA }, MergeFormatMode.KeepSourceLayout);
mergedDoc.Save("merged.docx");