Unlink Headers Footers to Previous Section in Word Document using Java | Add Clone of Header Footer with Table to Landscape Section

Hi there,

When adding tables in document headers, landscape page orientation retains the portrait format for the header and footer on even pages. In fact, tables nested in headers/footers on even pages are not resized correctly in landscape sections.

Please find attached an example demonstrating the problem.

Landscape-v3-20200930_202324.docx.zip (52.1 KB)

Thanks,
Behrouz

@behrouz,

You can fix the problem by using the following code:

Document doc = new Document("C:\\Landscape-v3-20200930_202324.docx\\Landscape-v3-20200930_202324.docx");

for (Section sec : doc.getSections()) {
    if (sec.getPageSetup().getOrientation() == Orientation.LANDSCAPE) {
        sec.getHeadersFooters().linkToPrevious(false);
        for (HeaderFooter hf : sec.getHeadersFooters())
            hf.remove();

        for (HeaderFooter hf : ((Section) sec.getPreviousSibling()).getHeadersFooters())
            sec.getHeadersFooters().add(hf.deepClone(true));
    }
}

doc.save("C:\\Landscape-v3-20200930_202324.docx\\awjava-20.9.docx");

@awais.hafeez

Here is the code we implemented on our side. Do you know what would be wrong with this code?

    this.moveToSection(1);
    Section currentSection = this.getCurrentSection();
    while (currentSection != null) {
        Section previousSection = (Section) currentSection.getPreviousSibling();

        if (currentSection.getPageSetup().getOrientation() != previousSection.getPageSetup().getOrientation()) {
            Section previousHeaderSection = previousSection;
            HeaderFooter previousHeader = null;
            while (previousHeader == null && previousHeaderSection != null) {
                previousHeader = previousHeaderSection.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_PRIMARY);
                previousHeaderSection = (Section) previousHeaderSection.getPreviousSibling();

            }

            Section previousFooterSection = previousSection;
            HeaderFooter previousFooter = null;
            while (previousFooter == null && previousFooterSection != null) {
                previousFooter = previousFooterSection.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY);
                previousFooterSection = (Section) previousFooterSection.getPreviousSibling();

            }

            currentSection.getHeadersFooters().linkToPrevious(false);
            currentSection.getHeadersFooters().clear();
            if (previousHeader != null) {
                currentSection.getHeadersFooters().add(previousHeader.deepClone(true));
            }

            if (previousFooter != null) {
                currentSection.getHeadersFooters().add(previousFooter.deepClone(true));
            }

            for (HeaderFooter headerFooter : currentSection.getHeadersFooters()) {
                // Resize tables width to use all available space
                TableCollection tables = headerFooter.getTables();
                for (com.aspose.words.Table table : tables) {
                    table.setPreferredWidth(PreferredWidth.fromPercent(100));
                    table.setAlignment(TableAlignment.CENTER);
                }

                // Resize width of lines to use all available space
                NodeCollection<Shape> shapes = headerFooter.getChildNodes(NodeType.SHAPE, true);
                for (Shape shape : shapes) {
                    if (ShapeType.LINE == shape.getShapeType() || shape.isHorizontalRule() || shape.isSignatureLine()) {
                        shape.setWidth(
                                currentSection.getPageSetup().getPageWidth() - currentSection.getPageSetup().getLeftMargin()
                                        - currentSection.getPageSetup().getRightMargin());
                        shape.setHorizontalAlignment(HorizontalAlignment.LEFT);
                        shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.MARGIN);
                    }
                }
            }
        }
        currentSection = (Section) currentSection.getNextSibling();
    }

@behrouz,

The problem occurs because the Table is actually apart of previous Section which has Portrait Orientation and the Landscape Section links all headers and footers from the previous Section. To workaround this problem, you need to unlink headers and footers by using linkToPrevious method and then copy the actual content of headers and footers in the Landscape Section. Please see the code in my previous post for more details.

@awais.hafeez

I’ll double check your code and get back to you again.

Thanks