4 Replies Latest reply on May 4, 2011 1:23 PM by MCLMasterKey

    Center PDF Page Content Using JS?

    MCLMasterKey Level 1

      Hi all,

       

      I have many many pages of PDFs where the content for the page has been aligned to the top left corner of the page.

       

      Is there a way using the JavaScript API that I can "move" the page content so that it is more centered vertically/horizontally on the page.

       

      I have done some searches and have tried using the get/set PageBoxes method, I think I am on the right path but I just can't figure it out.

       

      Any advice would be greatly appreciated.

       

      Thank you!

        • 1. Re: Center PDF Page Content Using JS?
          George_Johnson MVP & Adobe Community Professional

          Yes, you should be able to increase the page size by setting the BBox and then set the crop box so you get the results you want.

          1 person found this helpful
          • 2. Re: Center PDF Page Content Using JS?
            MCLMasterKey Level 1

            Hi George,

             

            Thanks for your quick response.

             

            I understand what you are saying, however I can't seem to get this working.


            1) According to the API reference, BBox is a READ ONLY attribute. So, I tried using Media instead based on some research I did.

             

            2) I have run some tests using a smilar script as below:

             

            var arrMedia = this.getPageBox({cBox:"Media", nPage:0});


            arrMedia[0] -= 90; // Adjust Left Side
            arrMedia[1] += 108; // Adjust Top Side
            arrMedia[2] += 0; // Adjust Right Side
            arrMedia[3] -= 0; // Adjust Bottom Side


            this.setPageBoxes({cBox:"Media", nStart:0, nEnd:this.numPages-1, rBox:arrMedia});

             

            But, it never seems to make my page bigger. I can modify the script and make the page smaller (verify by opening PDF and actualy seeing the smaller page). But, when I try to make it bigger, the PDF seems to just look the same as originally.

             

            Thanks again for all your help!

            • 3. Re: Center PDF Page Content Using JS?
              George_Johnson MVP & Adobe Community Professional

              Yes, I meant Media, not BBox. If you make it larger, you also have to set the crop box to the same for the change to become visible.

              1 person found this helpful
              • 4. Re: Center PDF Page Content Using JS?
                MCLMasterKey Level 1

                Thanks George.

                 

                I must have stumbled upon that idea right when you were typing your response to me.

                 

                If you change the Media box and the immediately change the Crop box you can then see the "shift" on the page.

                 

                ----------------------------------------------------------------------------------

                 

                Code below for fellow developers:

                 

                // Get the media box for the page
                var arrMedia = this.getPageBox({cBox:"Media", nPage:0});

                 

                // Set the new media area of the box (in pixels)
                arrMedia[0] -= 90;  // Adjust Left Side - this moves it out 1.25 inches
                arrMedia[1] += 108; // Adjust Top Side - this moves it up 1.5 inches
                arrMedia[2] += 0;   // Adjust Right Side
                arrMedia[3] += 0;   // Adjust Bottom Side

                 

                // Apply the new area to the media box of all the pages
                this.setPageBoxes({cBox:"Media", nStart:0, nEnd:this.numPages-1, rBox:arrMedia});

                 

                // Get the crop box for the page
                var arrCrop = this.getPageBox({cBox:"Crop", nPage:0});

                 

                // Set the new crop area of the box (in pixels)
                arrCrop[0] -= 90;  // Adjust Left Side - this moves it out 1.25 inches
                arrCrop[1] += 108; // Adjust Top Side - this moves it up 1.5 inches
                arrCrop[2] -= 90;  // Adjust Right Side - this moves it in 1.25 inches
                arrCrop[3] += 108; // Adjust Bottom Side - this moves it up 1.5 inches

                 

                // We are doing the opposite on the right and bottom so that we
                // still maintain an 8.5 x 11 sheet of paper in the end

                 

                // Apply the new crop area to the crop box of all the pages
                this.setPageBoxes({cBox:"Crop", nStart:0, nEnd:this.numPages-1, rBox:arrCrop});

                 

                ----------------------------------------------------------------------------------

                 

                Thanks again George!