Confused over CropBox and MediaBox

PDF-XChange Viewer SDK for Developer's
(ActiveX and Simple DLL Versions)

Moderators: PDF-XChange Support, Daniel - PDF-XChange, Chris - PDF-XChange, Sean - PDF-XChange, Vasyl - PDF-XChange, Ivan - Tracker Software, Stefan - PDF-XChange

DSSavant
User
Posts: 216
Joined: Thu Jul 08, 2004 7:29 pm

Confused over CropBox and MediaBox

Post by DSSavant »

I'm trying to place a Note Annotation in the center of the viewable area shown in the viewer for the Active page. Obviously, there is a bit of logic involved here like knowing what part of the active page is visible and which is not, if there are multiple pages showing, etc.

I already know how to add an annotation at a given mouse coordinate. What I'm having issues with is finding the center point of the viewable area for the active page.

When I run the following routine on a one page PDF:

Code: Select all

object top;
object bottom;
object left;
object right;

// Media Box
Viewer.DoDocumentVerb(ActiveDocumentId, "Pages[0].MediaBox.Top", "get", null, out top);
Viewer.DoDocumentVerb(ActiveDocumentId, "Pages[0].MediaBox.Bottom", "get", null, out bottom);
Viewer.DoDocumentVerb(ActiveDocumentId, "Pages[0].MediaBox.Left", "get", null, out left);
Viewer.DoDocumentVerb(ActiveDocumentId, "Pages[0].MediaBox.Right", "get", null, out right);
Debug.WriteLine(string.Format("Media Box - Top: {0}, Bottom: {1}, Left: {2}, Right: {3}.", top, bottom, left, right));

// Crop Box
Viewer.DoDocumentVerb(ActiveDocumentId, "Pages[0].CropBox.Top", "get", null, out top);
Viewer.DoDocumentVerb(ActiveDocumentId, "Pages[0].CropBox.Bottom", "get", null, out bottom);
Viewer.DoDocumentVerb(ActiveDocumentId, "Pages[0].CropBox.Left", "get", null, out left);
Viewer.DoDocumentVerb(ActiveDocumentId, "Pages[0].CropBox.Right", "get", null, out right);
Debug.WriteLine(string.Format("Crop Box - Top: {0}, Bottom: {1}, Left: {2}, Right: {3}.", top, bottom, left, right));

// Pane Width and Height...
object pageHeight;
object pageWidth;
Viewer.DoDocumentVerb(ActiveDocumentId, "Pages[0].Height", "get", null, out pageHeight);
Viewer.DoDocumentVerb(ActiveDocumentId, "Pages[0].Width", "get", null, out pageWidth);
Debug.WriteLine(string.Format("Page Width: {0}, Height: {1}.", pageWidth, pageHeight));

// Viewer Pane Width and Height...
object viewPaneWidth;
object viewPaneHeight;
Viewer.GetDocumentProperty(ActiveDocumentId, "Pages.Width", out viewPaneWidth, 0);
Viewer.GetDocumentProperty(ActiveDocumentId, "Pages.Height", out viewPaneHeight, 0);
Debug.WriteLine(string.Format("View Pane Width: {0}, View Pane Height: {1}.", viewPaneWidth, viewPaneHeight));
I get the following output:
Media Box - Top: 792, Bottom: 0, Left: 0, Right: 612.
Crop Box - Top: 792, Bottom: 0, Left: 0, Right: 612.
Page Width: 612, Height: 792.
View Pane Width: 397, View Pane Height: 244.
I get the same output for the given document no matter if I'm zoomed in 1000% or zoomed out to 6%. I, incorrectly, assumed that CropBox would give me the coordinates for the visible part of the active page within the viewer and that MediaBox would give me the full area required by the active page (really, its width and height).

Any clarification as to what these methods provide and what methods I actually need to use would be appreciated.
Corwin - Tracker Sup
User
Posts: 664
Joined: Tue Nov 14, 2006 12:23 pm

Re: Confused over CropBox and MediaBox

Post by Corwin - Tracker Sup »

Hello DSSavant,

I'm afraid that there is no way to get visible rect of document page. As an alternative you may get center screen coordinates and translate them to document points.
DSSavant
User
Posts: 216
Joined: Thu Jul 08, 2004 7:29 pm

Re: Confused over CropBox and MediaBox

Post by DSSavant »

So is there a way to determine if an Annotation is currently visible in the viewer?

Visible meaning that the annotation does not have its hidden bit set or anything like that. I simply want to know if the user can see the annotation or if I need to zoom the page out so that they can see it then.
User avatar
Vasyl - PDF-XChange
Site Admin
Posts: 2445
Joined: Thu Jun 30, 2005 4:11 pm

Re: Confused over CropBox and MediaBox

Post by Vasyl - PDF-XChange »

Hi, DSSavant.

For your case you will be able to use new features with the new build (upcoming):

Code: Select all

int pagesViewWidthInPx = 0;
int pagesViewHeightInPx = 0;
ctrl.GetDocumentProperty(iDocID, "Pages.Width", out pagesViewWidthInPx);
ctrl.GetDocumentProperty(iDocID, "Pages.Height", out pagesViewHeightInPx);
int annotZeroBasedIndexOnThePage = 0;
variantObj annotRectObj;
ctrl.DoDocumentVerb(iDocID, "Pages[0]", "GetAnnotRect", annotZeroBasedIndexOnThePage, out annotRectObj);
ctrl.DoDocumentVerb(iDocID, "Pages[0]", "PagePointsToViewPoints", annotRectObj, out annotRectObj); // translate annot rect from page coord. system to screen
Rectangle annotRect;
annotRect.left = annotRectObj[0];
annotRect.top = annotRectObj[1];
annotRect.right = annotRectObj[2];
annotRect.bottom = annotRectObj[3];
Rectangle viewRect(0,0,pagesViewWidthInPx, pagesViewHeightInPx);
bool annotIsVisible = annotRect.HasIntersectionWith(viewRect);
Note: it is pseudocode...

Best
Regards.
PDF-XChange Co. LTD (Project Developer)

Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
DSSavant
User
Posts: 216
Joined: Thu Jul 08, 2004 7:29 pm

Re: Confused over CropBox and MediaBox

Post by DSSavant »

Great, thanks. And as a bonus, I didn't have to go to JavaScript to get the annotation rectangle ;) I'll try coding that up when the next build comes out.
User avatar
Paul - PDF-XChange
Site Admin
Posts: 7362
Joined: Wed Mar 25, 2009 10:37 pm

Re: Confused over CropBox and MediaBox

Post by Paul - PDF-XChange »

hi DSSavant

do be sure to let us know how that works out for you.

:-)
Best regards

Paul O'Rorke
PDF-XChange Support
http://www.pdf-xchange.com
DSSavant
User
Posts: 216
Joined: Thu Jul 08, 2004 7:29 pm

Re: Confused over CropBox and MediaBox

Post by DSSavant »

Sorry for being a complete idiot but I'm getting use to it ;)

I'm having a very hard time making the "GetAnnotRect" working. My code throws with a type exception.

** Assuming I have a one page document with at least one annotation on it.

object dataOut = new object();
Viewer.DoVerb("Documents[0].Pages[0]", "GetAnnotRect", 0, out dataOut, 0);

The DoVerb throws. Something like "specified array was not of the expected type". I've tried several other examples only to fail miserably in the process. Any _real_ C# coding examples for getting a Safe Array to work with this call would be greatly appreciated.
User avatar
Vasyl - PDF-XChange
Site Admin
Posts: 2445
Joined: Thu Jun 30, 2005 4:11 pm

Re: Confused over CropBox and MediaBox

Post by Vasyl - PDF-XChange »

Hi, DSSavant.

You may use our helper to obtaining (or setting) the safearray:

Code: Select all

object dataOut = new object();
axCoPDFXCview1.DoVerb("Documents[0].Pages[0]", "GetAnnotRect", 0, out dataOut, (int)PDFXCviewAxLib.PXCVA_Flags.PXCVA_OutArgs);
PDFXCviewAxLib.IPDFXCargs annotRect = (PDFXCviewAxLib.IPDFXCargs)dataOut;
double left = (double)annotRect[0];
double top = (double)annotRect[1];
double right = (double)annotRect[2];
double bottom = (double)annotRect[3];
I don't known exactly how to obtain the safearray directly - I'm not a C# programmer, but I believe - the method exists, see:
http://msdn.microsoft.com/en-us/library ... 71%29.aspx

Best
Regards.
PDF-XChange Co. LTD (Project Developer)

Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
DSSavant
User
Posts: 216
Joined: Thu Jul 08, 2004 7:29 pm

Re: Confused over CropBox and MediaBox

Post by DSSavant »

Thanks Vasyl, I was not using the proper flag on the end of the call. That was the trick.

The method for seeing if an annotation is visible or not seems to be working very well, many thanks!
User avatar
Paul - PDF-XChange
Site Admin
Posts: 7362
Joined: Wed Mar 25, 2009 10:37 pm

Re: Confused over CropBox and MediaBox

Post by Paul - PDF-XChange »

:)
Best regards

Paul O'Rorke
PDF-XChange Support
http://www.pdf-xchange.com