Like the title says i'd like to read an existing pdf page size with VB.Net. I've been working with Itext.Sharp, and the Acrobat.dll. Is this possible??
1 Answers
There are a number of different "Boxes" a given page can have:
Media Box (required): The initial page size when printing viewing. Crop Box (optional): Supersedes the media box. Defaults to match the media box. Must be a subset or match the media box.
There's also art/trim/bleed boxes, but they don't matter as much and are much less common.
So, the page size:
PdfReader reader = new PdfReader(myPath); // gets the MEDIA BOX Rectangle pageRect = reader.getPageSize(1); // 1 -> first page
// gets the crop box if present, or the media box if not. Rectangle cropRect = reader.getCropBox(1);
// and finally Rectangle artBox = reader.getBoxSize( 1, "art"); // could be "art", "bleed", "crop", "media", or "trim"
I'd go with getCropBox()
.
I also recommend checking out the JavaDoc for things like this. At the very least you would have come up with getPageSize()
on your own. No, it's not C#. Yes, it's very useful.
Also note that these Rectangles need not be based on 0,0 (which would be the lower left corner on an unrotated page).
Further, you should check the page's rotation, getPageRotation(int)
, and swap height and width if the rotation is 90 or 270. There is getPageSizeWithRotation(int)
, but it only works with the media box, so I'd do it yourself if I were you. It's only a few extra lines of code:
// rotation has to be 0, 90, 180, or 270. "360" isn't kosher IIRC.
if (reader.getPageRotation(pageNum) % 180 != 0) {
float tmp = width;
width = height;
height = tmp;
}

- 15,672
- 3
- 42
- 80