Hello there,
im currently having trouble resizing the content of a PDF to fit A4 without getting cut(after concatenating multiple PDFs). I made a simple picture to show the actual result i want, attached to this post.
Could someone shar a code snippet on how this could work? Thanks.
Im using Aspose.PDF 10.6.2 for Java.
no one can help?
Hi there,
We are sorry for the delayed response. You can resize PDF page contents as per your requirement. Please check following sample code to resize a PDF document to A4 size for your reference. However if the issue persist then please share your sample PDF document here, we will test the scenario and will guide you accordingly.
Document pdfDocument = new Document(“input.pdf”);
// Resize contents of resultant PDF
int[] page_cnt1 = new int[pdfDocument.getPages().size()];
for (int i = 0; i < pdfDocument.getPages().size(); i++)
page_cnt1[i] = i + 1;
PdfFileEditor pfe = new PdfFileEditor();
pfe.resizeContents(pdfDocument, page_cnt1,PdfFileEditor.ContentsResizeParameters.pageResize(com.aspose.pdf.PageSize.getA4().getWidth(), com.aspose.pdf.PageSize.getA4().getHeight()));
// Save output as PDF format
pdfDocument.save("output.pdf");
Best Regards,
Thanks for your answer!
Unfortunately i dont have access to “ContentsResizeParameters”.
1. There is no PdfPageEditor.ContentsResizeParameters
2. I found ContentsResizeParameters here: com.aspose.pdf.facades.APdfFileEditor.ContentsResizeParameters
but "The type com.aspose.pdf.facades.APdfFileEditor is not visible"
Maybe the version is not campatible?
Hi there,
Sorry i actually mean PdfFileEditor.ContentsResizeParameters and not PdfPageEditor.
I even updated to 11.1.0 thats how far our Aspose license works.
I attached a picture where you see, theres no ContentResizeParameter available.
Hi there,
Thanks for your feedback. I am afraid even I am unable to notice issue with Aspose.Pdf for Java 11.1.0. It seems you are inspecting the methods/properties directly in the class instead its instance. Please create an object of PdfFileEditor and check the available methods/properties. Please copy paste following code in your IDE and share the results.
com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document("input.pdf");
// Resize contents of resultant PDF
int[] page_cnt1 = new int[pdfDocument.getPages().size()];
for (int i = 0; i < pdfDocument.getPages().size(); i++)
page_cnt1[i] = i + 1;
com.aspose.pdf.facades.PdfFileEditor pfe = new com.aspose.pdf.facades.PdfFileEditor();
pfe.resizeContents(pdfDocument, page_cnt1, PdfFileEditor.ContentsResizeParameters.pageResize(com.aspose.pdf.PageSize.getA4().getWidth(), com.aspose.pdf.PageSize.getA4().getHeight()));
// Save output as PDF format
pdfDocument.save("output.pdf");
Best Regards,
Hey once again,
sorry for my late response. I still cant get it to work.
I attached the Demo pdf and my wanted result as an image.
How do i accomplish this result?
Thank you very much.
Hi there,
Thank you! I’m looking forward for a solution
Hi there,
Thanks for your patience. We have investigated the issue and and noticed that it is not bug. Your shared document is rotated to 90 degrees. You may use following code snippet for the document, hopefully it will help you to accomplish the task.
com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document("D:/Downloads/demo.pdf");
com.aspose.pdf.facades.PdfFileEditor pfe = new com.aspose.pdf.facades.PdfFileEditor();
PdfFileEditor.ContentsResizeParameters params = PdfFileEditor.ContentsResizeParameters.pageResize(
com.aspose.pdf.PageSize.getA4().getHeight(), com.aspose.pdf.PageSize.getA4().getWidth());
for (int i = 1; i < pdfDocument.getPages().size() + 1; i++) {
if (pdfDocument.getPages().get_Item(1).getRotate() == Rotation.on90) {
double newHeight = pdfDocument.getPages().get_Item(i).getRect().getWidth()
* com.aspose.pdf.PageSize.getA4().getWidth()
/ pdfDocument.getPages().get_Item(i).getRect().getHeight();
double margin = (com.aspose.pdf.PageSize.getA4().getHeight() - newHeight) / 2;
// Left and Right margin instead of Top and Bottom as this document was rotated.
params.setLeftMargin(PdfFileEditor.ContentsResizeValue.units(margin));
params.setRightMargin(PdfFileEditor.ContentsResizeValue.units(margin));
// setContentsWidth instead of setContentsHeight as this document was rotated.
params.setContentsWidth(PdfFileEditor.ContentsResizeValue.units(newHeight));
pfe.resizeContents(pdfDocument, new int[]{i}, params);
} else {
System.out.println("Requires some additional calculations...");
}
}
pdfDocument.save(myDir + "output.pdf");
Best Regards,