Rotate a Word DOCX Document from Landscape to Portrait Orientation using C# .NET

Hi Hafeez,

-> One more issue we are facing.
-> We are trying to rotate a word document from landscape to portrait but we are unable to achieve it.

Problem:-

-> In a word file not all the pages are landscape.
-> We need to check if the particular page is a landscape then we need to rotate it to portrait.
-> We are using the below piece of code but it is not working as expected.

Question:- How to achieve the above problem using Aspose.Word 18.5 dll?

Code:-

        Document doc = new Document(@"d:\Landscape Issues\PDFToDOC_out.doc");

        // Loop through all section.
        foreach (Section section in doc.Sections)
        {
            // section.PageSetup.PageHeight
            //MessageBox.Show(section.PageSetup.Orientation.ToString());

            if (section.PageSetup.Orientation.ToString().ToLower() == "landscape")
            {
                //section.PageSetup.TextOrientation = TextOrientation.Upward;
                section.PageSetup.VerticalAlignment = PageVerticalAlignment.Justify;
                section.PageSetup.Orientation = Aspose.Words.Orientation.Portrait;
            }

            // Change page orientation.
            // section.PageSetup.Orientation = Aspose.Words.Orientation.Landscape;
        }

        // Save output document.
        doc.Save(@"d: \Landscape Issues\out.doc");

Note:-

-> We are using Aspose.Word 18.5 dll for .Net.
-> In this situation we can not able to upgrade our dll.

Could you please help us ASAP. We are in a critical situation.

Thanks & Regards,
Santosh Kumar Panigrahi

@santoshp1989,

Please check the following C# code example that will convert all landscape sections in Word document into portrait format:

Document doc = new Document("C:\\Temp\\in.docx");
foreach (Section sec in doc.Sections)
    if (sec.PageSetup.Orientation == Orientation.Landscape)
        sec.PageSetup.Orientation = Orientation.Portrait;
doc.Save("C:\\Temp\\20.8.docx");  

In case the problem still remains, please ZIP and attach the following resources here for testing:

  • Your simplified input Word document
  • Aspose.Words for .NET 20.8 generated output DOCX file showing the undesired behavior
  • Your expected DOCX file showing the desired output. You can create this document manually by using MS Word.

As soon as you get these pieces of information ready, we will start further investigation into your scenario and provide you more information.

Hi Hafeez,

Thanks Hafeez for your quick response.

After using the below code also we are facing issues.

Issues:-

-> One extra blank page is coming.
-> Content is moving to right side, so that we unable to see the content if the content is there in a text box.

Code:-

Document doc = new Document(“C:\Temp\Input.doc”);
foreach (Section sec in doc.Sections)
{
if (sec.PageSetup.Orientation == Orientation.Landscape)
{
sec.PageSetup.Orientation = Orientation.Portrait;
}
}
doc.Save(“C:\Temp\Output.doc”);

-> Could you please find the attached zip file where you can 3 files. 1. Input.doc, 2. Output.doc and 3. Expected_Output.doc
Landscape_issue.zip (27.4 KB)

Note:-
-> We are using Aspose.Word 18.5 dll for .Net.
-> In this situation we can not able to upgrade our dll.

Could you please help us ASAP. We are in a critical situation.

Thanks & Regards,
Santosh Kumar Panigrahi

@santoshp1989,

The issue occurs because there are frames in Word document (see last page for example) which are not getting resized according to the new page width. We have logged this problem in our issue tracking system. Your ticket number is WORDSNET-20927. We will further look into the details of this problem and will keep you updated on the status of the linked issue. Sorry for the inconvenience.