Apply rotation on existing content

Hi,


Is it possible to apply a rotation to all the content inside a page given an arbitrary angle (using a Matrix). If so, how am i suppose to proceed?

What i need is to rotate the content itself, not the page. So using page.setRotation() doesn’t suits me.

Regards

Hi Nicolas,


Thanks for contacting support.

When rotating the page, the contents inside it are also rotated. However you may also consider changing/updating page dimensions so that after rotation, contents are not truncated.

Rotate page with its contents.

[Java]
com.aspose.pdf.Document doc = new
com.aspose.pdf.Document(myDir+“result.pdf”);
System.out.println(doc.getPages().get_Item(1).getPageInfo().isLandscape());
doc.getPages().get_Item(1).setRotate(Rotation.on270);

doc.save(myDir+“LandScapeOutput.pdf”);


In order to change page size you can set MediaBox of the page in the following way.

[Java]

Document doc = new Document(“PdfWithText.pdf”);<o:p></o:p>

for (int pageCount = 1; pageCount <= doc.getPages().size(); pageCount++)<o:p></o:p>

{<o:p></o:p>

Page page= doc.getPages().get_Item(pageCount);<o:p></o:p>

com.aspose.pdf.Rectangle r = page.getMediaBox();<o:p></o:p>

double newHeight = r.getWidth();<o:p></o:p>

double newWidth = r.getHeight() * r.getHeight() / r.getWidth();<o:p></o:p>

double newLLX = r.getLLX();<o:p></o:p>

//we must to move page upper in order to compensate 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 lover edge upper on difference between old and new height.<o:p></o:p>

double newLLY = r.getLLY() + (r.getHeight() - newHeight);<o:p></o:p>

page.setMediaBox(new com.aspose.pdf.Rectangle(newLLX, newLLY, newLLX + newWidth, newLLY + newHeight));<o:p></o:p>

//sometimes we also need to set CropBox (if it was set in original file)<o:p></o:p>

page.setCropBox(new com.aspose.pdf.Rectangle(newLLX, newLLY, newLLX + newWidth, newLLY + newHeight));<o:p></o:p>

}<o:p></o:p>

doc.save(“36115.pdf”);


Furthermore, you may consider using PdfPageEditor as it may help you to change the page orientation without truncating page data.


[Java]

Document doc = new Document(myDir+“ipnut.pdf”);<o:p></o:p>

com.aspose.pdf.Rectangle r = doc.getPages().get_Item(1).getRect();<o:p></o:p>

PdfPageEditor ppe = new PdfPageEditor();<o:p></o:p>

ppe.bindPdf(doc);<o:p></o:p>

ppe.setZoom((float)(r.getWidth() / r.getHeight()));<o:p></o:p>

ppe.setPageSize(new com.aspose.pdf.PageSize((float)r.getHeight(), (float)r.getWidth()));<o:p></o:p>

ppe.save(myDir+“output_ppe.pdf”);

Hi and thank you for your answer.


I think i didn’t made my question explicit enough:

I need to apply an arbitrary rotation to the content itself (page.setRotate isn’t what i seek sadly).
What i’m asking for is how to concatenate a matrix to the content stream so that i can rotate the content to any rotation? (ie. rotate all the content from a page by 45°)

I know this question seems a bit strange but i need to know this nonetheless :slight_smile:

Hi Nicolas,


Thanks for sharing the details.

Can you please share the input PDF document and desired output file, which can help us in better understanding the requirement.

Hi,


Here some example of what i’m trying to achieve.

sample.pdf is the original document, sample_45.pdf is the same but where the content is rotated and translated.

Hi Nicolas,


Thanks for sharing the resource files.

I am afraid currently Aspose.Pdf for Java does not support the feature to rotate existing contents of PDF file. However for the sake of implementation, I have logged this requirement as PDFNEWJAVA-35203 in our issue tracking system. We will further look into the details of this requirement and will keep you posted on the status of correction. Please be patient and spare us little time. We are sorry for your inconvenience.

Thank you. Sorry for the inconvenience as i know this is as a very specific request.

Hi Nicolas,


Thanks for your patience.

As we recently have noticed this requirement, so we need little time to investigate and figure out the solution for this requirement. As soon as we have some definite updates, we will let you know.