You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by David Luu <ma...@gmail.com> on 2013/04/02 02:49:31 UTC

How to get PDF page width & height?

I can send an example test PDF if needed. But wanted to know in general how
to get PDF page size (width & height of page) using PDFBox.

In terms of page size, I'm referring to the value reported by Adobe Acrobat
Reader Document Properties like "Page Size: 7.25 x 10.25 in" as reported in
Adobe Reader X.

I don't mind if the value extracted is not in inches as long as I can
convert the unit from x to inches.

I wasn't able to existing code for it right off the bat. The closest I came
was in the user guide:

http://pdfbox.apache.org/userguide/

PDPage page = ...;
PDRectangle mediaBox = page.getMediaBox();
System.out.println( "Width:" + mediaBox.getWidth() );

but that only gave the width & is based on page width defined by media box.
What about height? And is media box usually the component within the PDF
that defines page width & height? If not what else to check for?

Thanks,
David

Re: How to get PDF page width & height?

Posted by Thomas Chojecki <in...@rayman2200.de>.
As Gilad already say, using the MediaBox or CropBox is the right way. 
Most pages does not have a CropBox so if you call findCropBox you will 
get the MediaBox instead.

 From the rectangle you can grab the width and height and convert it to 
your purpose. The 72 is the dpi value that you divide and optional you 
can multiply it by 2.54 to obtain cm.

You can take a look at [1] for more informations about the boxes

Best regards
Thomas

[1] http://www.prepressure.com/pdf/basics/page-boxes

Am 02.04.2013 10:32, schrieb Gilad Denneboom:
> - A PDRectangle object has a getHeight() method as well as a 
> getWidth()
> method... It's documented here:
> 
> http://pdfbox.apache.org/apidocs/org/apache/pdfbox/pdmodel/common/PDRectangle.html
>
> - The results of these methods are floats, representing the page size 
> in
> PostScript points.
> To convert them to inches you need to divide them by 72.
>
> - A PDF does not have a single height and width definition. It 
> contains
> multiple pages boxes, each one used for different purposes.
> So there isn't a single answer to the question: What is the page 
> height and
> width of a PDF? Usually, the default property used by most 
> application is
> the Crop box, though.
>
> PS - I recommend using the findMediaBox (or findCropBox, or whatever)
> methods over the getMediaBox ones.
>
>
> Gilad
>
>
>
> On Tue, Apr 2, 2013 at 2:49 AM, David Luu <ma...@gmail.com> wrote:
>
>> I can send an example test PDF if needed. But wanted to know in 
>> general how
>> to get PDF page size (width & height of page) using PDFBox.
>>
>> In terms of page size, I'm referring to the value reported by Adobe 
>> Acrobat
>> Reader Document Properties like "Page Size: 7.25 x 10.25 in" as 
>> reported in
>> Adobe Reader X.
>>
>> I don't mind if the value extracted is not in inches as long as I 
>> can
>> convert the unit from x to inches.
>>
>> I wasn't able to existing code for it right off the bat. The closest 
>> I came
>> was in the user guide:
>>
>> http://pdfbox.apache.org/userguide/
>>
>> PDPage page = ...;
>> PDRectangle mediaBox = page.getMediaBox();
>> System.out.println( "Width:" + mediaBox.getWidth() );
>>
>> but that only gave the width & is based on page width defined by 
>> media box.
>> What about height? And is media box usually the component within the 
>> PDF
>> that defines page width & height? If not what else to check for?
>>
>> Thanks,
>> David
>>

Re: How to get PDF page width & height?

Posted by Gilad Denneboom <gi...@gmail.com>.
- A PDRectangle object has a getHeight() method as well as a getWidth()
method... It's documented here:
http://pdfbox.apache.org/apidocs/org/apache/pdfbox/pdmodel/common/PDRectangle.html

- The results of these methods are floats, representing the page size in
PostScript points.
To convert them to inches you need to divide them by 72.

- A PDF does not have a single height and width definition. It contains
multiple pages boxes, each one used for different purposes.
So there isn't a single answer to the question: What is the page height and
width of a PDF? Usually, the default property used by most application is
the Crop box, though.

PS - I recommend using the findMediaBox (or findCropBox, or whatever)
methods over the getMediaBox ones.


Gilad



On Tue, Apr 2, 2013 at 2:49 AM, David Luu <ma...@gmail.com> wrote:

> I can send an example test PDF if needed. But wanted to know in general how
> to get PDF page size (width & height of page) using PDFBox.
>
> In terms of page size, I'm referring to the value reported by Adobe Acrobat
> Reader Document Properties like "Page Size: 7.25 x 10.25 in" as reported in
> Adobe Reader X.
>
> I don't mind if the value extracted is not in inches as long as I can
> convert the unit from x to inches.
>
> I wasn't able to existing code for it right off the bat. The closest I came
> was in the user guide:
>
> http://pdfbox.apache.org/userguide/
>
> PDPage page = ...;
> PDRectangle mediaBox = page.getMediaBox();
> System.out.println( "Width:" + mediaBox.getWidth() );
>
> but that only gave the width & is based on page width defined by media box.
> What about height? And is media box usually the component within the PDF
> that defines page width & height? If not what else to check for?
>
> Thanks,
> David
>