isLandscape() Does Not Return Correct Value

Hello,

Using jar: aspose-pdf-25.2-jdk17.jar

I don’t know why this is happening, but no matter what I do and what PDF document I open, the value I receive for isLandscape() is always “false”, even in cases where it is a true.

What am I doing wrong?

Here is my code:

import com.aspose.pdf.*;
import java.io.File;

public class IsLandscapeProblem {
    public static void main(String[] args) {
        System.out.println("Starting");
        try {
            File[] inputFiles = {
                    new File("test_width.pdf"),
            };


            for (File file : inputFiles) {
                System.out.println("opening: " + file.getName());
                String inputFile = file.getAbsolutePath();

                Document doc = null;
                try {
                    doc = new Document(inputFile);
                } catch (Exception e) {
                    System.out.println("Error opening document: " + e.getMessage());
                    continue;
                }

                Page page = doc.getPages().get_Item(1);

                double pageWidth = page.getPageInfo().getWidth();
                double pageHeight = page.getPageInfo().getHeight();
                boolean isLandscape = page.getPageInfo().isLandscape();

                System.out.println("pageWidth: " + pageWidth + " pageHeight: " + pageHeight + " isLandscape: " + isLandscape);
                doc.close();
            }

        } catch (Exception e) {
            System.out.println("Error: " + e.getMessage());
        }

        System.out.println("Done.");
    }
}

And it generates the following output:

Starting
opening: test_width.pdf
pageWidth: 595.0 pageHeight: 842.0 isLandscape: false
Done.

Attached is the PDF file. This is a landscape document.

Thanks a lot,
Sharon
test_width.pdf (8.3 KB)

@sharonez

Do you want to keep using above property to check if width is larger than the height? Because you can get original height and width using the code like below:

double originalWidth = page.getPageRect(true).getWidth();
double originalHeight = page.getPageRect(true).getHeight();

In case isLandscape() method is required by your code and logic routine, please let us know.

I want to know the width of the page. That’s all. In the file I attached, I still get 595 as width, although the real width is 842. That is why I invoked isLandscape(), in order to know whether I need to swap between them.

@sharonez

We checked with the code snippet shared above and noticed that the Width it is extracting was 792. Can you please share how you are able to see that the page width is 842? Can you please share a screenshot?

Where did you see that? This is the output I received:

Starting
opening: test_width.pdf
pageWidth: 595.0 pageHeight: 842.0 isLandscape: false
Done.

It’s true that your solution does the job, and brings about a better value of 792 points.

@sharonez

Below is the code how we are getting this value and we are testing with 25.3 version of the API:

private void GetPageHeightWidthOriginal() {
 Document doc = new Document(dataDir + "test_width.pdf");
 Page page = doc.getPages().get_Item(1);
 double originalWidth = page.getPageRect(false).getWidth();
 double originalHeight = page.getPageRect(false).getHeight();
 System.out.println(originalWidth);
}