Page orientation not maintained after converting docx -> xml -> docx

I am using aspose-words-18.5-jdk16 api for java. I want to maintain the page orientation (portrait or landscape)after converting docx to xml. And again xml to docx
(i.e docx -> xml -> docx)

Scenario:

I’ve a docx having page orientation as follow:

1st Page : Portrait.
2nd Page : Landscape.
3rd Page : Portrait.

I have attached project please check it.

com.aspose.words.zip (884.4 KB)

@ngshinde999,

Thanks for your inquiry. The third page of “sample.docx” has orientation landscape. Other pages of document have orientation portrait. The Document.appendDocument method does not change the orientation of pages. We suggest you please do not use following line of code in your project.

srcDoc.getFirstSection().getPageSetup().setOrientation(dstDoc.getFirstSection().getPageSetup().getOrientation());

If you still face problem, please create a simple Java application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing. We will investigate the issue on our side and provide you more information.

As suggested by you, without using following line of code in project. It is not working

srcDoc.getFirstSection().getPageSetup().setOrientation(dstDoc.getFirstSection().getPageSetup().getOrientation());

If The Document.appendDocument method does not change the orientation of pages. Then what we can use to resolve this problem.

I am still facing problem, I have created Java application (source code without compilation errors). Please check it.

com.aspose.words.zip (1.9 MB)

@ngshinde999,

Thanks for sharing the detail. The XML documents have page orientation as portrait. This is the reason you are not getting the expected output.

When you extract the content from document and save it to FlatOpc, the page orientation is portrait. This is the default value. You need to set the orientation of first section of dstDoc.

Document dstDoc = ExtractContents.generateDocument(doc, extractedNodes);

In ExtractPara.java, please use the following code snippet to set the orientation of destination document.

Document dstDoc = ExtractContents.generateDocument(doc, extractedNodes);
dstDoc.getFirstSection().getPageSetup().setOrientation(((Section)bookmarkStart.getAncestor(NodeType.SECTION)).getPageSetup().getOrientation());
dstDoc.save("Append\\XML\\" + bm + ".xml",SaveFormat.FLAT_OPC);

Thanks for your help.

I have tried this logic with our document, it is not working.

If the section,for example, TEST started in Portrait mode and one of its page is in Landscape mode then it is not working.

I have attached the docx, please check it.

sample.zip (434.2 KB)

@ngshinde999,

Thanks for your inquiry. The content of section 3 are exported in Landscape mode. Please check the attached output documents. We have saved them as DOCX. output.zip (128.9 KB)

You can change the orientation of output document according to your requirement. If content of your desired output are in Portrait and Landscape mode, what orientation of page you want to set. Please check the attached image. page orientation.png (52.8 KB)

Please share your expected output documents for “Section 3”. We will then provide you more information about your query.

Thanks for reply,

If you check Section 4 in source docx, it is in both portrait and landscape mode, but I convert it into XML and then to DOCX, resulting Section 4 is only in Portrait mode. It does not retain landscape mode.

Section 4.1.2 should be in portrait and landscape mode, as in source docx.

I have attached Source and Output docx, please check it.

output.zip (812.5 KB)

@ngshinde999,

Thanks for your inquiry. The extractContent method does not extract the section breaks. We are working over your query and will get back to you with code example.

@ngshinde999,

Please use the following code example to extract the content with section breaks and page orientation. Hope this helps you.

private static void extractContentsWithSectionBreak() throws Exception
{
    // Load in the document
    Document doc = new Document(MyDir + "source.docx");
    DocumentBuilder builder = new DocumentBuilder(doc);

    int bm = 1;
    for(Section section : doc.getSections())
    {
        if (doc.getFirstSection() == section)
            continue;

        builder.moveTo(((Section) section.getPreviousSibling()).getBody().getLastParagraph());
        int orientation = section.getPageSetup().getOrientation();
        if (section.getPageSetup().getSectionStart() == SectionStart.CONTINUOUS) {
            builder.startBookmark("BM_BreakC" + bm);
            builder.endBookmark("BM_BreakC" + bm);
            builder.startBookmark(orientation + "Orientation" + bm);
            builder.endBookmark(orientation + "Orientation" + bm);
        }
        if (section.getPageSetup().getSectionStart() == SectionStart.NEW_PAGE) {
            builder.startBookmark("BM_BreakNewPage" + bm);
            builder.endBookmark("BM_BreakNewPage" + bm);
            builder.startBookmark(orientation + "Orientation" + bm);
            builder.endBookmark(orientation + "Orientation" + bm);
        }
        bm++;
    }

    int i = 1;

    NodeCollection nodes = doc.getChildNodes(NodeType.PARAGRAPH, true);
    for (Paragraph para : (Iterable<Paragraph>) nodes) {
        if (para.getParagraphFormat().isHeading()) {
            if (para.getParagraphFormat().isListItem())
            {
                int label = para.getListLabel().getLabelValue();
            }
            Paragraph paragraph = new Paragraph(doc);
            para.getParentNode().insertBefore(paragraph, para);
            builder.moveTo(paragraph);
            builder.startBookmark("bm_extractcontents" + i);
            builder.endBookmark("bm_extractcontents" + i);
            i++;
        }
    }

    builder.moveToDocumentEnd();
    builder.startBookmark("bm_extractcontents" + i);
    builder.endBookmark("bm_extractcontents" + i);
    //doc.updatePageLayout();
    for (bm = 1; bm < i; bm++) {

        BookmarkStart bookmarkStart = doc.getRange().getBookmarks().get("bm_extractcontents" + bm)
                .getBookmarkStart();

        BookmarkStart bookmarkEnd = doc.getRange().getBookmarks().get("bm_extractcontents" + (bm + 1))
                .getBookmarkStart();

         ArrayList extractedNodes = ExtractContents.extractContent(bookmarkStart, bookmarkEnd, false);
         
        Document dstDoc = ExtractContents.generateDocument(doc, extractedNodes);
        dstDoc.getFirstSection().getPageSetup().setOrientation(((Section)bookmarkStart.getAncestor(NodeType.SECTION)).getPageSetup().getOrientation());

        DocumentBuilder dstbuilder = new DocumentBuilder(dstDoc);
        for(Bookmark bookmark : dstDoc.getRange().getBookmarks())
        {
            if (bookmark.getName().contains("BM_BreakC")) {
                dstbuilder.moveTo(bookmark.getBookmarkEnd().getNextSibling());
                dstbuilder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS);
            } else if (bookmark.getName().contains("BM_BreakNewPage")) {
                dstbuilder.moveTo(bookmark.getBookmarkEnd());
                dstbuilder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
            }
            else if (bookmark.getName().contains("Orientation")) {
                if (bookmark.getName().startsWith("1"))
                    ((Section)bookmark.getBookmarkStart().getAncestor(NodeType.SECTION)).getPageSetup().setOrientation(Orientation.PORTRAIT);
                else
                    ((Section)bookmark.getBookmarkStart().getAncestor(NodeType.SECTION)).getPageSetup().setOrientation(Orientation.LANDSCAPE);
            }
        }

        for(Bookmark bookmark : dstDoc.getRange().getBookmarks())
        {
            if (bookmark.getName().contains("BM_BreakC")) {
                bookmark.remove();
            } else if (bookmark.getName().contains("BM_BreakNewPage")) {
                bookmark.remove();
            }
            else if (bookmark.getName().contains("Orientation")) {
                bookmark.getBookmarkStart().getParentNode().remove();
            }
        }
        dstDoc.save(MyDir + "out\\out" + bm + ".xml", SaveFormat.FLAT_OPC);
    }
}