We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Pdf page margin Get and Set issue

Hi,
I am facing issue in setting and getting page margin of existing PDF file. Whenever i try to extract page margin, I got (72.0,72.0,90.0,90.0) for top,bottom, left and right receptively.
After setting margin of PDF, I again got same dimension.
Please find the below implemented code and sample file for the reference.
Document document=new Document(“C:\Users\saurav.saxena\Documents\Original File.pdf”);
//Extract page margin pdf page
for(Page pdfPage:document.getPages())
{
System.out.println(pdfPage.getPageInfo().getMargin().getTop()+","
+pdfPage.getPageInfo().getMargin().getBottom()+","+
pdfPage.getPageInfo().getMargin().getLeft()+","+
pdfPage.getPageInfo().getMargin().getRight());
}
//Set margin with below values
for(Page pdfPage:document.getPages())
{
pdfPage.getPageInfo().setMargin(new MarginInfo(18, 36, 54, 72));
}
//document.save(“C:\Users\saurav.saxena\Documents\Processed File.pdf”);

Please check the issue and let me know.
Thanks and Regards
Saurav Saxena
pdf.pdf (423.8 KB)
Processed File.pdf (424.1 KB)

@sauravjava

Please note that PageInfo properties are used for PDF generation only (for convenience of API users). PDF format has not page margin as entity but, you can try to calculate what you need, by analysis such entities as Artbox , BleedBox , CropBox , MediaBox , TrimBox and Rect property for existing PDF documents.

Please, get more information and code snippets here: https://docs.aspose.com/display/pdfjava/Get+and+Set+Page+Properties

@asad.ali
Thank you for your support.

I need to find page margin of existing PDF file. I follow the link you have shared and read about mentioned entities. I am trying to calculate margin from subtracting values of MediaBox with TrimBox, but not get correct result.
I have getting same values from all entities.
So, please let me know how can we calculate page margin from these entities or by some other way?
Thanks and Regards
Saurav Saxena

I am using below code. Please review and let me know.
#Code:
Page page=document.getPages().get_Item(1);
Rectangle media=page.getMediaBox();
Rectangle trim=page.getTrimBox();
Rectangle crop=page.getCropBox();
Rectangle art=page.getArtBox();
Rectangle bleed=page.getBleedBox();

	System.out.println(media.getLLX()+"-"+media.getLLY()+"-"+media.getURX()+"-"+media.getURY());
	System.out.println(trim.getLLX()+"-"+trim.getLLY()+"-"+trim.getURX()+"-"+trim.getURY());
	System.out.println(crop.getLLX()+"-"+crop.getLLY()+"-"+crop.getURX()+"-"+crop.getURY());
	System.out.println(art.getLLX()+"-"+art.getLLY()+"-"+art.getURX()+"-"+art.getURY());
	System.out.println(bleed.getLLX()+"-"+bleed.getLLY()+"-"+bleed.getURX()+"-"+bleed.getURY());

#Output
0.0-0.0-595.32-841.92
0.0-0.0-595.32-841.92
0.0-0.0-595.32-841.92
0.0-0.0-595.32-841.92
0.0-0.0-595.32-841.92

@sauravjava

You can use ContentBBox as it returns the rectangle containing contents without visible margins.

Rectangle contentBox = pdfPage.calculateContentBBox();
System.out.println("LLX = " + contentBox.getLLX() + ", LLY = " + contentBox.getLLY() + ", URX = " + contentBox.getURX() + ", URY = " + contentBox.getURY());
System.out.println("left = " + (contentBox.getLLX() - pdfPage.getRect().getLLX()) + ", bottom = " + (contentBox.getLLY() -pdfPage.getRect().getLLY()) + 
", right = " + (pdfPage.getRect().getURX() - contentBox.getURX()) + ", top = " + (pdfPage.getRect().getURY() - contentBox.getURY()));

In case you need further assistance, please feel free to let us know.