21 Replies Latest reply on Nov 28, 2017 12:55 PM by gwjohnson3

    Can I set the MediaBox to origin 0/0 using javascript?

    gwjohnson3

      I am working on a script which both resizes pdfs and adds notations.  I'm running into problems with regards to the origin point.  Even if I can assume that the origin initially coincides with the MediaBox (and that is not always the case with the files I'm working with), the resizing I need to do requires me to adjust the MediaBox.  Once I do that, I'm unsure as to how to obtain the origin point in order to properly place my notations.  Is there a way to reset the origin within javascript?  If not, is there a way to return coordinate values of the existing origin as they relate to the page boxes?

        • 1. Re: Can I set the MediaBox to origin 0/0 using javascript?
          Thom Parker Adobe Community Professional

          The page boxes are tricky, but once you understand the coordinate systems it gets easier.

           

          Here is an old article I wrote on getting the page boxes, it comes with a sample file that you may find useful, but it doesn't discuss the coordinate systems.

          https://acrobatusers.com/tutorials/finding-page-boundaries

           

          Here's one on placing Annotations that has a better explaination

          https://acrobatusers.com/tutorials/auto_placement_annotations

           

          The key thing to understand is the lower left corner of the Crop box is always (0,0) in rotated space. This does not change. The lower left corner of the Media box is always (0,0) in user space. This does not change.So you have to be aware of which coordinate space the functions are operating in, and how the space changes when you change a box.

           

          So for example, if both spaces are the same, i.e. the page has not been rotated or sized, then you set the crop box left side to 10.  The doc.getPageBox("Crop") will return 0 for for left side coordinate. You may well ask, "How is this? I just set it to 10".  The answer is that the bottom left corner is alway at 0,0, and changing the left side just shifts the media box, which now has a left side of -10.And that's how it works.

           

          You'll find a free page resizing tool here:

          Free Acrobat Automation Tools

           

          I've also written a much more complete explanation of how coordinates work here (but it's for members only):

          pdfscripting.com

          • 2. Re: Can I set the MediaBox to origin 0/0 using javascript?
            Test Screen Name Most Valuable Participant

            Bear in mind that changing the origin of the media box will cause all the contents of the page to shift. There is no JavaScript method to avoid this.

            • 3. Re: Can I set the MediaBox to origin 0/0 using javascript?
              gwjohnson3 Level 1

              So there is no way to mimic the functionality of the "Set MediaBox to origin" preflight?  Or am I misunderstanding what that preflight does?

               

              Is there a way to invoke preflight fixups via javascript?  I don't have much experience with preflights via javascript and can't recall seeing a fixup used in this manner before.

              • 4. Re: Can I set the MediaBox to origin 0/0 using javascript?
                Test Screen Name Most Valuable Participant

                I haven't used it but I imagine it does much more than set the mediabox. Otherwise all content would shift. Probably it normalizes all content so it fits with an origin of 0,0, which is a complex process (especially for patterns) none of which is the least bit accessible to JavaScript.

                 

                What you need to do is FIND OUT the origin and work with it, which is pretty much the job of any app, plug-in or script that adds content.

                • 5. Re: Can I set the MediaBox to origin 0/0 using javascript?
                  try67 MVP & Adobe Community Professional

                  It is possible to run Preflight Profiles using JavaScript, by the way.

                  • 6. Re: Can I set the MediaBox to origin 0/0 using javascript?
                    Thom Parker Adobe Community Professional

                    What is it that you are trying to do? Are you placing Annotations on the page? 

                     

                    All geometry operations in JavaScript are relative to the Crop box or the Media box.  The relative differences between these boxes are all that matters. The real origin is irrelevant. 

                     

                    Also changing the media box does not shift the contents of the page. The only page box requirement is that the Media box is outside the Crop Box. I can change the media box all day long and as long is it does not interfere with the Crop box, the page view does not change.

                     

                    Please explain the specific issue youare having.

                    • 7. Re: Can I set the MediaBox to origin 0/0 using javascript?
                      Thom Parker Adobe Community Professional

                      To "Test Screen Name".  Changing the Media box does not shift the contents of the page. This only happens if you make the media box smaller than the crop box. It is the "Crop Box" that controls the user's view.

                      • 8. Re: Can I set the MediaBox to origin 0/0 using javascript?
                        Test Screen Name Most Valuable Participant

                        You're absolutely right; the crop box wins. So adding a crop box if there wasn't one before is a way of preserving appearance while altering the media box origin.

                         

                        However, the problem here is most likely that positions of annotation, form fields etc. dynamically added are not where they are expected to be. Here this is unlikely to help because the origin that matters is the origin of the crop box. And let's not even start about rotated pages, though at some point this bites every who tries to script and has a view that the coordinate system is a nice simple thing...

                        • 9. Re: Can I set the MediaBox to origin 0/0 using javascript?
                          gwjohnson3 Level 1

                          I'm trying to draw lines on a pdf relative to the crop box.  Here's a portion of my script which is intended to draw a horizontal line across the width of the pdf offset .75" down from the top.  Apologies if the code is a little clunky.  This is cobbled together from a few different things (your articles in particular are a big help to me), but it does highlight the problem I'm having.

                           

                           

                          for(var i=0 ; i<this.numPages; i+=1){

                             

                              var rCrop = this.getPageBox("Crop", i);

                              var mxFromRot = (new Matrix2D).fromRotated(this,this.pageNum);

                              var rAnnot = mxFromRot.transform(rCrop);

                             

                              var xt1 = rAnnot[0];

                              var yt1 = rAnnot[1]-54;

                             

                              var pgWidth = rCrop[2] - rCrop[0];

                             

                              var ang = 0;

                              ang = ang/180.0 * Math.PI;

                             

                              var pointsT = new Array();

                              pointsT[0] = [xt1, yt1];

                             

                              var xt2 = xt1 + (pgWidth)*Math.cos(ang);

                              var yt2 = yt1 + (pgWidth)*Math.sin(ang);

                             

                              pointsT[1] = [xt2, yt2];

                             

                              this.addAnnot({

                                  type: "Ink",

                                  page: i,

                                  gestures: [pointsT] ,

                                  strokeColor: color.red,

                                  width: .5

                              })

                          }

                           

                           

                          This works perfectly on my test file.  However, the test file in question has a crop box which is offset 1/2" in from the media box on all four sides and I would like the media box to match the crop box (margin controls for the crop box should read zero on all sides).  When I add the following line to the beginning of my script, it throws off the placement of the annotation.

                           

                          this.setPageBoxes({ cBox: "Media", nStart: i, rBox: this.getPageBox("Crop", i)});

                           

                          If I take that same file after the media box has been altered and run the "Set MediaBox to origin" preflight profile, the script works properly again.  Am I not understanding something with regards to the hierarchy of the boxes?

                          • 10. Re: Can I set the MediaBox to origin 0/0 using javascript?
                            Test Screen Name Most Valuable Participant

                            Consider this: the crop box origin is surely not 0. It's possibly 36,36 (half an inch). So the effect of your code is not to set the media origin to zero but to whatever the  crop box origin is. Thus not doing the same as preflight.

                             

                            Why do you want the media box to match the crop box, and also to be zero? Is this to be tidy or to solve a specific issue? I sometimes set the media box to zero, but have to accept that all annotations are now incorrectly placed.

                            • 11. Re: Can I set the MediaBox to origin 0/0 using javascript?
                              Thom Parker Adobe Community Professional

                              You've got some ordering issues with your code.

                               

                              Follow this method. I believe this is outlined in the article on placing annotations I linked in my first post.

                               

                              1.  Get the Crop box

                              2.  Calculate placement points based on the crop box as it is. this is the view of the PDF that the user is seeing, so if you want things to appear to the user, then calculate them with the crop.

                               

                              3. Convert the placement points to un-roated space with the matrix transform op.

                              4. place the line annotation.

                               

                              Here's the line points

                              // Create points as flat array for transformation

                              var yPoint = rCrop[1]-54;

                              var pointsR = [0,yPoint, rCrop[2],yPoint];

                               

                              // Convert to un-rotated space

                              var pointsT = mxFromRot.transform(pointsR);

                              // break into two points for application

                              pointsT = [[pointsT[0],pointsT[1]].[pointsT[2],pointsT[3]]]

                               

                               

                              Why the trigonometry?  It doesn't seem to do anything. Besides, if you want to perform a rotation, use the Matrix object, that's what it's best for.  Here is a sample file that demonstrates advanced matrix operations.

                              Free Sample PDF Files with scripts

                              Look for the 2D Matrix Calculator

                              • 12. Re: Can I set the MediaBox to origin 0/0 using javascript?
                                gwjohnson3 Level 1

                                To Test Screen Name:

                                 

                                Some of the files I'll be using this script on may have already had the media box set to match the crop box (or had who knows what else done to them for all I know) and since I have no idea whether or not that is the case on any given file, I need to be able to "normalize them" so to speak.  I'm also just trying to get a better understanding of the origin and how it relates to the various boxes in general as it would be useful knowledge for other potential projects.

                                 

                                To Thom:

                                 

                                I tried reordering the code and, again, it worked fine on the original file, but not on the same file after the media box had be set to match the crop box.  The line appears offset 1/2" down and to the left.

                                 

                                As for the trig, that is a remnant of a much larger script that this snippet is pulled from.  It involves user entered angles for the annotation lines as well as a bunch of other functionality that's not really pertinent to the problem I'm having.  Once I figure out the coordinate stuff, I'll be going through and cleaning up a lot of the clunkiness.

                                • 13. Re: Can I set the MediaBox to origin 0/0 using javascript?
                                  Test Screen Name Most Valuable Participant

                                  Why do you need to normalize at all? I write code that does really complicated things to PDF files without doing this.

                                  • 14. Re: Can I set the MediaBox to origin 0/0 using javascript?
                                    gwjohnson3 Level 1

                                    I guess I don't need to normalize them.  I just know that my script works ok on some files I have, and on others (specifically ones where the boxes has been futzed around with) the script doesn't work.  Running the "set mediabox to origin" preflight fixes whatever problem I'm having with the annotation placement, so I'm trying to figure out how to do whatever it is that preflight is doing within javascript.

                                    • 15. Re: Can I set the MediaBox to origin 0/0 using javascript?
                                      Thom Parker Adobe Community Professional

                                      The code should always work to put the line in the correct location. However, if you then modify the page boxes, of course there is the possibility that objects will change location.

                                       

                                      So, are you saying the line was put into the wrong location by the code when it was originally applied to the PDF? or are you saying the line changed position after you modified the page boxes?

                                       

                                      And is there any reason you are changing the media box to match the crop box?

                                      • 16. Re: Can I set the MediaBox to origin 0/0 using javascript?
                                        Thom Parker Adobe Community Professional

                                        Post a file where the script is not working

                                        • 17. Re: Can I set the MediaBox to origin 0/0 using javascript?
                                          gwjohnson3 Level 1

                                          The script places the line correctly when I run it on the original file (media box is 1/2" larger than the crop box on all sides).  When I run the same script on the same file after is has had the media box set to match the crop box, the line is not in the correct location.

                                           

                                          I am only changing the media box to match the crop box because some of the files I will need this script for may have already had the boxes monkeyed with by others before I get them and I'm trying to account for all scenarios.

                                           

                                          How do I post files?  I'm not seeing where I can do that.

                                          • 18. Re: Can I set the MediaBox to origin 0/0 using javascript?
                                            Thom Parker Adobe Community Professional

                                            You can use dropbox, or send me a message with the attached file.

                                             

                                            I'm very keen on solving this issue because I have published a great deal of material on correctly calculating geometry. 

                                            The whole point of using the "Matrix2D" object to transform the coordinates from Rotated to User space is to deal with differences between the crop and the media boxes. If the transformation is not working, then there is something very wrong.

                                            • 19. Re: Can I set the MediaBox to origin 0/0 using javascript?
                                              Thom Parker Adobe Community Professional

                                              Ok, so after looking over your documents I remember this one. The Acrobat JS DOM is misinterpreting the page geometry and translating coordinates incorrectly by the amount the media origin is shifted.  I investigated this a long time ago and I'm pretty sure I filed a bug report. I'll file another one. I have not seen it as an issue for a very long time, so it must be rare.  Why are you seeing it so often?

                                               

                                              I do not believe there is any way to detect this situation from JavaScript, but you are correct that the preflight "Set Media Box Origin" fixup does the trick. It shifts all page content, including annotations by the origin offset. 

                                               

                                              So you can execute a preflight profile in JavaScript using the "doc.preflight()" function.  However, to do this you have to first create a new named Preflight Profile that has the media box fixup as it's operation.  The preflight function is blocking, so the addAnnot() functions can be called after and should work.  You should create this all as either a folder level automation, or a Acrobat DC Command

                                              • 20. Re: Can I set the MediaBox to origin 0/0 using javascript?
                                                Thom Parker Adobe Community Professional

                                                BTW: can I use your sample files in the bug report?

                                                • 21. Re: Can I set the MediaBox to origin 0/0 using javascript?
                                                  gwjohnson3 Level 1

                                                  Thank you so much.  Not that I'm excited about the bug, but I'm glad to have an answer.  Please feel free to use the file I sent you.

                                                   

                                                  To answer your question about why I see this so often, it's because we have a third party pdf resizing tool that we use that automatically resizes all the boxes to match the crop box.  I get the same results using your resizing tool you link to in your first response if I include the media box.