Highlighted

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

New Here ,
Nov 20, 2017

Copy link to clipboard

Copied

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?

Adobe Community Professional
Correct answer by 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

TOPICS
Acrobat SDK and JavaScript

Views

438

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

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

New Here ,
Nov 20, 2017

Copy link to clipboard

Copied

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?

Adobe Community Professional
Correct answer by 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

TOPICS
Acrobat SDK and JavaScript

Views

439

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Nov 20, 2017 0
Adobe Community Professional ,
Nov 20, 2017

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 20, 2017 0
Most Valuable Participant ,
Nov 20, 2017

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 20, 2017 0
New Here ,
Nov 27, 2017

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 27, 2017 0
Most Valuable Participant ,
Nov 27, 2017

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 27, 2017 0
Adobe Community Professional ,
Nov 27, 2017

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 27, 2017 0
Most Valuable Participant ,
Nov 27, 2017

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 27, 2017 0
Adobe Community Professional ,
Nov 27, 2017

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 27, 2017 0
New Here ,
Nov 27, 2017

Copy link to clipboard

Copied

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?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 27, 2017 0
Adobe Community Professional ,
Nov 27, 2017

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 27, 2017 0
Most Valuable Participant ,
Nov 27, 2017

Copy link to clipboard

Copied

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...

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 27, 2017 0