Page Orientation Settings for com.aspose.pdf.Document

Hi,


Is there anyway to setup the page orientation for PDF document. In my requirement, i am not creating any new PDF document. I am loading the existing PDF document into com.aspose.pdf.Document object to apply pagesetup

Regards

Hi Diane,

Thanks for contacting support.

In order to set/change the page orientation, setLandscape(true) of the PageInfo class can be used. But I am afraid the recent release of Aspose.Pdf for Java has some issues while setting/updating the page orientation of a PDF file. For your consideration and correction, the problem is logged in our issue tracking system as PDFNEWJAVA-34362. We will investigate this issue in detail and keep you updated on the status of a correction. We apologize for your inconvenience.

Java:

com.aspose.pdf.Document doc = new com.aspose.pdf.Document("c:/pdftest/result.pdf");
System.out.println(doc.getPages().get_Item(1).getPageInfo().isLandscape());
doc.getPages().get_Item(1).getPageInfo().setLandscape(true);
doc.save("c:/pdftest/LandScapeOutput.pdf");

Hi,

Is there any update on this ?
We are using latest aspose PDF version 9.3.1 and we still have this issue.

Hi,


Thanks for using our API.

As shared in earlier post, the API is having some issue while setting page orientation and problem has already been logged in our issue tracking system. The development team will further look into the details of this problem and will keep you posted on the status of correction. Please be patient and spare us little time. We are apologize for this inconvenience.

I am having a similar issue in .NET, does this issue exist for .NET as well?


Hi,

Do we have any SLA for this issue. We are moving to production soon. We need to see whether it can be fixed prior to that.

hapyfishrmn:
I am having a similar issue in .NET, does this issue exist for .NET as well?
Hi,

Thanks for contacting support.

Aspose.Pdf for .NET is also having similar issue and for the sake of correction, we already have logged it as PDFNEWNET-36115 in our issue tracking system. We will further look into the details of this problem and will keep you posted on the status of correction. Please be patient and spare us little time. We are sorry for this inconvenience.

Please note that Aspose.Pdf for Java is ported from Aspose.Pdf for .NET, so this problem will first be fixed in Aspose.Pdf for .NET and same fix will be ported to Aspose.Pdf for Java.
nielsenusa:
Do we have any SLA for this issue. We are moving to production soon. We need to see whether it can be fixed prior to that.
Hi,

Currently this issue has low priority and will be fixed as per its schedule. However I has shared your concerns with development team and as soon as we have some further updates, we will let you know.

Your patience and comprehension is greatly appreciated in this regard.

The issues you have found earlier (filed as PDFNEWNET-36115) have been fixed in Aspose.Pdf for .NET 9.6.0.

Hi Diane,

Four solutions are given below to manage PDF content size orientation:

  1. Swap the height and width to change the orientation to landscape
Document doc = new Document("PdfWithText.pdf");

foreach (Page page in doc.Pages)
{
    Aspose.Pdf.Rectangle r = page.MediaBox;

    double newHeight = r.Width;
    double newWidth = r.Height;
    double newLLX = r.LLX;

    double newLLY = r.LLY + (r.Height - newHeight);

    page.MediaBox = new Aspose.Pdf.Rectangle(newLLX, newLLY, newLLX + newWidth, newLLY + newHeight);

    page.CropBox = new Aspose.Pdf.Rectangle(newLLX, newLLY, newLLX + newWidth, newLLY + newHeight);
}

doc.Save("36115.pdf");
  1. Keeping height intact and increasing width proportionally

Collected Code Sample:

foreach (Page page in doc.Pages)
{
    Aspose.Pdf.Rectangle r = page.MediaBox;

    double newHeight = r.Height;
    double newWidth = r.Height * r.Height / r.Width;

    double newLLX = r.LLX;
    double newLLY = r.LLY;

    page.MediaBox = new Aspose.Pdf.Rectangle(newLLX, newLLY, newLLX + newWidth, newLLY + newHeight);

    page.CropBox = new Aspose.Pdf.Rectangle(newLLX, newLLY, newLLX + newWidth, newLLY + newHeight);
}

doc.Save("36115.pdf");
  1. Option using PdfPageEditor facade

Solution:

Document doc2 = new Document("PdfWithText.pdf");
Aspose.Pdf.Rectangle r2 = doc2.Pages[1].Rect;

PdfPageEditor ppe = new PdfPageEditor();
ppe.BindPdf("PdfWithText.pdf");

ppe.Zoom = (float)(r2.Width / r2.Height);

ppe.PageSize = new Aspose.Pdf.PageSize((float)r2.Height, (float)r2.Width);

ppe.Save("36115-1.pdf");

We will share Java code for the purpose with you soon.

Best Regards,

Does the isLandscape (.NET) / setLandscape (Java) have any function for actually setting the orientation of the page?


hapyfishrmn:
Does the isLandscape (.NET) / setLandscape (Java) have any function for actually setting the orientation of the page?
Hi,

Thanks for contacting support.

Yes your understanding is correct. The property in .NET and method in Java perform same operation. Please try using our API's and the event of any further query, please feel free to contact.

Thank you for the update. I am using the latest build of 9.6 and tried your original response using the isLandscape() and found it not changing the document from portrait to landscape.


Aspose.Pdf.Document doc = new Aspose.Pdf.Document(“c:/test/Portrait.pdf”);
doc.Pages[1].PageInfo.IsLandscape = true;
doc.Save(“c:/test/Landscape.pdf”);

Is there something I am missing?

Hi,


We are sorry for the inconvenience caused. Please note IsLandscape property works if you are creating a PDF document from scratch. However for changing orientation of existing PDF page, please use above suggested options. Please feel free to contact us for any further assistance.

Best Regards,

Hi,

Suggested options above for landscape issue is given only for dot net. We are looking for a java solution. Can you please help.

Hi Diane,

Thanks for your inquiry. Please note Aspose.Pdf for Java is migrated from Aspose.Pdf for .NET and Java code is analogous. Please check the following code snippet for the purpose. Hopefully, it will help you accomplish the task.

Document doc = new Document("PdfWithText.pdf");

for (int pageCount = 1; pageCount <= doc.getPages().size(); pageCount++) {
    Page page = doc.getPages().get_Item(pageCount);
    com.aspose.pdf.Rectangle r = page.getMediaBox();
    double newHeight = r.getWidth();
    double newWidth = r.getHeight() * r.getHeight() / r.getWidth();
    double newLLX = r.getLLX();

    // We must move the page upper in order to compensate for the changing page size
     //(lower edge of the page is 0,0 and information is usually placed from the top of the page.
     //That's why we move the lower edge up by the difference between the old and new height.
    double newLLY = r.getLLY() + (r.getHeight() - newHeight);
    page.setMediaBox(new Rectangle(newLLX, newLLY, newLLX + newWidth, newLLY + newHeight));

    // Sometimes we also need to set CropBox (if it was set in the original file)
    page.setCropBox(new Rectagle(newLLX, newLLY, newLLX + newWidth, newLLY + newHeight));
}

doc.save("36115.pdf");

Please feel free to contact us for any further assistance.

Best Regards,

Hi,


we are actually editing the existing PDF which is portrait mode. The code snippet shared is not changing the layout alone instead its crops the page and renders it in the landscape mode

Hi Diane,

Thanks for your feedback. Please use PdfPageEditor, it will help you to change the page orientation without truncating page data. Hopefully it will help you to accomplish the task.

Document doc = new Document(myDir + "ipnut.pdf");
com.aspose.pdf.Rectangle r = doc.getPages().get_Item(1).getRect();

PdfPageEditor ppe = new PdfPageEditor();
ppe.bindPdf(doc);
ppe.setZoom((float)(r.getWidth() / r.getHeight()));
ppe.setPageSize(new com.aspose.pdf.PageSize((float)r.getHeight(), (float)r.getWidth()));
ppe.save(myDir + "output_ppe.pdf");

Please feel free to contact us for any further assistance.

Best Regards,