Hi,
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,
Hi,
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?
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.
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:
- 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");
- 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");
- 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.
Hi,
Hi,
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,
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,