11 Replies Latest reply on Jan 12, 2017 3:03 PM by herob4u

    How to Rotate Stamps or Annotations via Javascript?

    herob4u

      I am trying to automate the rotation of a given stamp that is duplicated across multiple pages. Typically I would attempt to script such procedure, but I lack information regarding the methods I have available. In what ways can you refer to an annotation, and what is the appropriate rotate method called?

       

      The only rotation I managed to achieve was by selecting the stamp, then executing a command along the lines of " this.selectedAnnots[0].rotate = <angle to rotate>".

      However, not only do I not want to select and execute the command (as that would be similar to manually rotating with the grip), but also, the rotation shrinks my stamp for some reason.

       

      Is there a more appropriate way to do this? Note I am using the Standard Version of Acrobat.

       

      Thanks

        • 1. Re: How to Rotate Stamps or Annotations via Javascript?
          George_Johnson MVP & Adobe Community Professional

          To rotate an annotation without resizing, you'd also have to change the rect property, which you'll have to compute based on the rotation value you use. The following tutorial should help with some of the math involved: https://acrobatusers.com/tutorials/auto_placement_annotations

          • 2. Re: How to Rotate Stamps or Annotations via Javascript?
            herob4u Level 1

            I read the article and I think I get the point of it. But it's difficult to see how that affects my rotation. I am rotating a stamp for example, so what property of that stamp am I trying to modify, and what function call allows me to do that?

             

            The mentioned article makes sense to me in the context of auto positioning the stamp with respect to page boundaries, but it doesnt deal with rotation or dealing with an already existing stamp -- what function call do I use to modify the transform of an existing stamp or annotation?

            • 3. Re: How to Rotate Stamps or Annotations via Javascript?
              Test Screen Name Most Valuable Participant

              Let's step back. Why do you reject setting the rotate property, surely the only way to rotate? George's reply indicated the only other property you need to set is text, which you must recalculate to include the revised element boundary.

              • 4. Re: How to Rotate Stamps or Annotations via Javascript?
                Test Screen Name Most Valuable Participant

                Damnable autocorrect. The property I tried to refer to was "rect" not "text".

                • 5. Re: How to Rotate Stamps or Annotations via Javascript?
                  Karl Heinz Kremer Adobe Community Professional

                  You are already on the correct track: You need to set the "rotate" property. In your one line code snippet, you are doing two different things: You are getting the currently selected annotation (actually, the first one of the currently selected annotations in case there are more), and then in a second step (even though it's all in one line), you are taking that annotation and you are setting the rotate property.

                   

                  You want to automate that, so you have to change the first part of that operation, but the second part can remain the same.

                   

                  To get all annotations, you would use a different API method. Do you have access to the Acrobat API documentation, specifically the JavaScript API reference? If not, that is a must, you cannot program for Acrobat without knowing what API functions are available, and how they need to be used. Take a look here as Doc.getAnnots(): Acrobat DC SDK Documentation

                   

                  This method will return either all or a subset of annotations in a document or on a page. The annotations are returned as a JavaScript array. This means that once you call this method, you have to process that array and set the rotate property for each array element. Iterating over the array is just basic JavaScript.

                   

                  As far as the change in size goes, this is a result of the rectangle that surrounds the annotation being unchanged by rotating the actual annotation, and to fit something that is not square into that rectangle, the whole annotation will be scaled down. You will also have to change the "rect" property of your annotation and, based on which corner of the annotation should remain fixed, change the lower left and upper right corner that makes up this rectangle. See here for more information: Acrobat DC SDK Documentation - you will see a reference to "default user space" in this article, which is a term you've also found in the article that George referenced above.

                  • 6. Re: How to Rotate Stamps or Annotations via Javascript?
                    herob4u Level 1

                    Oh! I see, I just never thought that it uses a bounding box to contain the annotation. I will get back to reading the documentation once I get back from work, however...one quick question I still have:

                     

                    What stops me from OVERscaling that containing rectangle? Say I write a lazy piece of code that somehow manages to upsize the box to the page's size, would that imply the other annotations will be hidden due to the large bounding box overshadowing them?

                     

                    And to reassure, I never did Javascripting, and programming was never my forte - I felt like there wasnt explicit resources regarding the classes and function calls for relatively new programmers.

                    • 7. Re: How to Rotate Stamps or Annotations via Javascript?
                      Karl Heinz Kremer Adobe Community Professional

                      You can do the same thing with your mouse and keyboard: Annotations have a certain order, and if you overlay or partially overlay them, that order is used to determine where on that "stack" of annotations a certain annotation will be, and how it will be covered or partially covered up by other annotations that are closer to the top of that stack.

                      • 8. Re: How to Rotate Stamps or Annotations via Javascript?
                        Karl Heinz Kremer Adobe Community Professional
                        And to reassure, I never did Javascripting, and programming was never my forte - I felt like there wasnt explicit resources regarding the classes and function calls for relatively new programmers.

                         

                        You are posting in the JavaScript forum

                         

                        I understand that the step from being just a user of an application to having to program it in a strange language involves quite a learning curve. The good news is that the basics of JavaScript are pretty easy to learn, that does not mean that it's a simple language, there is quite a bit of depth to the language, but you don't have to know all that in order to be able to write small scripts for Acrobat. Take a look here about how to get started with JavaScript programming: Learning to Program JavaScript for Adobe Acrobat - KHKonsulting LLC

                         

                        There are two documents that Adobe released about JavaScript programming, I already mentioned the API reference. That's pretty dry material, and not something you should read without some background in JavaScript. Otherwise you will have a hard time following the examples. There is a second document titled "Developing Acrobat Applications Using JavaScript": Acrobat DC SDK Documentation  This is an introduction into what you can do with JavaScript in Acrobat, but I would also recommend that you first learn about the syntax of the core language.

                        • 9. Re: How to Rotate Stamps or Annotations via Javascript?
                          herob4u Level 1

                          Thanks for the elaborate response. I managed to sneak some time and gave the API document another chance. After your clarification regarding the rect object, things began to clear up. Although I still didnt achieve my desired rotation, I took a step back and successfully made a simple script that automatically places my desired stamp.

                           

                          I am facing one challenge now, and I believe solving it will help in the long run. I can hard code my stamp position for a specific paper size. However, documents varying in size will always fail to adjust. What property could I use to generalize the position of my rectangle corners? Currently I am using the link George provided, perhaps I could go from the page bounding box and introduce a proportional offset variable. Can you advise in case there is a convenient option?

                           

                          Thanks!

                          • 10. Re: How to Rotate Stamps or Annotations via Javascript?
                            Karl Heinz Kremer Adobe Community Professional

                            When you look at the page boxes, you will find that the crop box is very likely what you want. Take a look at the Doc.getPageBox() method:

                             

                            Acrobat DC SDK Documentation

                             

                            That and the information about annotation placement should help you to place an annotation on any page size in e.g. the upper right hand corner (or anywhere else for that matter relative to the page's edges). If you run into problems, please post your script here with some information about what you were trying to accomplish, what the script actually does, and any potential error messages displayed on the JavaScript console (Ctrl-J or Cmd-J to display the console window).

                            • 11. Re: How to Rotate Stamps or Annotations via Javascript?
                              herob4u Level 1

                              I understand my code isn't efficient, but this is what I blocked out based on my understanding of each command. The script DOES instantiate the stamp correctly along the width of a standard page. The height however sinks way below the page border.

                               

                              //Getting the page border information
                              
                              var rectCrop = this.getPageBox();
                              
                              var rotWidth = rectCrop[2]-rectCrop[0]; // Subtract x and y coordinates of the bounding box corner to get
                              var rotHeight = rectCrop[3]-rectCrop[1];// width and height of rotated user space.
                              
                              var mxFromRot = (new Matrix2D).fromRotated(this,this.pageNum); // Taken from recommended article - I do not know how this object works.
                              
                              var rectDef = mxFromRot.transform(rectCrop); //Page box relative to default coordinates -- using mxFromRot to TRANSFORM from userspace to default space.
                              
                              var defWidth = rectDef[2]-rectDef[0]; // Define width and height relative to default coordinate space
                              var defHeight = rectDef[3]-rectDef[1]; 
                              
                              //Calculating Margin/Offset 
                              var offw = defWidth-rotWidth; //difference between width relative to default space and rotational user space
                              var offh = defHeight-rotHeight; // Same
                              
                              //Identifying Corners of Annotation Rectangle
                              var x1 = ((2/3)*rotWidth)+offw;  //Placing first corner 2/3 distance of page document width.
                              var x2 = rotWidth+offw; // Placing second corner at the rightmost end of the document
                              var y1 = offh;  // Placing first corner on the floor of the document (0 in rotational space)
                              var y2 = ((1/3)*rotHeight))+offh; // Placing second corner 1/3 the height of the document
                              
                              var annotBox = [x1,y1,x2,y2];
                              var annot = this.addAnnot({page:0,type"Stamp",rect:annotBox,AP:"etc..."});
                              

                               

                              Aside from the improper height placement, I have to add that the script totally falls apart when the page is rotated. If I can make the script work for the standard portrait page I am testing on, then I might probably use the "getPageRotation" command to flip images back to portrait, stamp, and flip back as lazy fix. Let me know if this is expected to fail.

                               

                              Thanks.