Get pdf document margin info

Is it possible to read the margin information from a pdf document ?
I can see a PdfPadding struct: PdfPadding Struct | GemBox.Pdf
, but could not work out how to get it from an open pdf document. e.g. I might have expected to see a PdfDocument.Padding property or something similar.

(Note: I am aware there is a PdfPage.CropBox property which can be used to get the page size, but I’m after the page margins, which are typically like 1 inch etc around the edges of the page).

Thanks.

Hi,

I’m afraid that there is no such thing in PDF pages.
You see, you can write the text elements anywhere on the page and in any order that you want.
In other words, there are no margins that would limit where a text can be written.

What exactly would you like to achieve?
Perhaps you can retrieve the desired information by checking the position of the first and the last element, or better yet checking the highest and the lowest coordinates of all the text elements on the page.

Regards,
Mario

Thanks Mario,
I’m trying to port over some code that merges pages of PDF documents, and adds some additional text/shapes to the bottom of each page in the new merged file. The old code is written using another library (“iText7”), and in that library there are methods like: Document.GetRightMargin(), Document.GetLeftMargin(). (Not sure how these methods work under the covers). So I was hoping there was something similar in GemBox PDF. I can get most of it working ok, based on the merging examples on GemBox PDF website, but stuck on getting the page margins. Maybe I can just hard code the (conceptual) margin sizes - it will probably work ok for the majority of the documents.

Hi,

By looking at the source code of the iText7 Document.GetLeftMargin(), it seems that it simply returns the hardcoded value of 36 if the value was not set (see Document.GetDefaultProperty method), so you can use the same value with GemBox.Pdf.

Regards,
Stipo

1 Like