These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Cropping pages from VBA

jjrrww
Registered: Jan 26 2009
Posts: 9
Answered

I am trying to script Acrobat from VBA for Word to crop the inside edge of a 2-up layout (where the staple goes) to remove the bleed. I'm close.

I get Type mismatches no matter HOW I call setPageBoxes

Code as follows:

Sub acrobatTest()

Dim ac As New AcroApp
Dim acDoc As New AcroPDDoc
Dim jso As Object

Dim myCrop As Variant
Dim numPages As Variant

Dim cropsize As Integer
cropsize = 27 ' 0.375 inch

Dim i As Variant

ac.Show
ac.Maximize 1
acDoc.Open "C:\1982764.pdf"

Set jso = acDoc.GetJSObject

numPages = jso.numPages
For i = 0 To numPages - 1
myCrop = jso.getPageBox()
jso.setPageBoxes
myCrop = jso.getPageBox("Crop", i)
myTrim = jso.getPageBox("Trim", i)
If i Mod 2 <> 0 Then
myCrop(2) = myCrop(2) - cropsize ' Crop Left pages
Else
myCrop(0) = myCrop(0) + cropsize ' Crop Right pages
End If

jso.setPageBoxes "Crop", i, i, myCrop

Next i

End Sub

My Product Information:
Acrobat Pro Extended 9.0, Windows
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Croping pages from a VB script using the JSO is possible, but the best way to do it is to put all the functionality into a folder level JavaScript, as a function. Test this script from Acrobat to make sure it works the way you want. Then use the JSO in VB to call the function. There are several data type differences between JavaScript and VB and I think this is what you are seeing. Putting all the functionality in an Acrobat JavaScript function gets around the problem.

However, there is already a crop function in the IAC (AcroExch.PDPage.CropPage). Why not use it instead of going to JavaScript.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

jjrrww
Registered: Jan 26 2009
Posts: 9
(Warning: Serious acrobat newbie alert)

My instinct said to go with the javascript solution, but I couldn't figure out how to invoke it from JSO - can you point me appropriate docs? I have the Javascript guide (no version 9 yet?), but what doc talks about JSO?

If I go the AcroExch.PDPage.CropPage option - what doc should I be tapping for that? The object model for AcroExch is rather a mystery to me... I'm guessing at docs.

- Thanks

John
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
If you are going to be controling Acrobat from a VB application, then it's best to do what you can from the IAC. And for what you want to do you can do it all in the IAC. You'll find the IAC reference and the IAC Guide here:

http://www.adobe.com/devnet/acrobat/interapplication.php

There are examples and explainations in both documents. Everything you need.

If you are more comfortable with a JavaScript solution then you should do the bulk of the solutions in pure JavaScript. Here's an article that will help to get you started.

http://www.acrobatusers.com/tutorials/2006/folder_level_scripts

Choose one. Look up the info and give it a try. Then ask more questions here if you get stuck.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

jjrrww
Registered: Jan 26 2009
Posts: 9
(long delay - was waiting for client)

getting closer, but still no effect.
It seems VERY straightforward, the crop claims to succeed, but no visual indication that it did.

The code opens a file, ostensibly crops odd and even pages differently, deletes a page and saves.
Everything seems fine except the crop has no effect. The function returns true as though it has succeeded. Code below:


Sub acrobatTest()

Dim acApp As Acrobat.acroApp
Dim acDoc As Acrobat.AcroPDDoc
Dim cropRect As Acrobat.AcroRect

Dim cropSize As Integer
cropSize = 27 ' 0.375 inch

Set acApp = New acroApp
acApp.Show
acApp.Maximize 1

Set acDoc = New AcroPDDoc
acDoc.Open "C:\Test.pdf"
acDoc.OpenAVDoc "Working Doc"

Set cropRect = New AcroRect
cropRect.Top = 0
cropRect.Left = cropSize
cropRect.Right = 666
cropRect.bottom = 846
' Crop even pages
i = acDoc.CropPages(0, acDoc.GetNumPages, 2, cropRect)

cropRect.Top = 0
cropRect.Left = 0
cropRect.Right = 666 - cropSize
cropRect.bottom = 746
' Crop odd pages
i = acDoc.CropPages(0, acDoc.GetNumPages, 1, cropRect)

'delete the 2nd page
acDoc.DeletePages 1, 1
acDoc.Save PDSaveFull + PDSaveCollectGarbage, "C:\Test.pdf"
acDoc.Close
Set acDoc = Nothing

acApp.CloseAllDocs
acApp.Exit
Set acApp = Nothing

End Sub
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
There are a couple of things that I'm not sure will work, or at least might screw up the crop. First, the end page is (acDoc.GetNumPages-1), since the page numbers are zero based. Second, I'm not sure what this line will produce "cropSize = 27 ' 0.375 inch". Page Coords in PDF are measured from the bottom left corner, So the bottom and left sides are zero and increase to the top and right. I think you should get the page rectangle from the JavaScript function doc.getPageBox() first, before setting up the crop rectangle in the VB script.

I think what happened was that the code passed a valid rectangle object into the crop fucntion. But its coords and possibly the page numbers, were so far off from what was acceptable that Acrobat ignored the operation. Obviously Acrobat should have returned an error. So maybe this is a bug.

Here's an artile on Page Coordinates for JavaScript. I think the IAC page Corrds should be the same.

http://www.acrobatusers.com/tutorials/2006/page_bounds

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

jjrrww
Registered: Jan 26 2009
Posts: 9
Bingo.

Nonsensical coordinates - I was orienting myself at top right for the origin.
("Think Cartesian.") Agreed - it should at least whine that it failed. Probably, mathematically, it did what I asked - crop off nothing.

BTW This - cropSize = 27 ' 0.375 inch - is just the amount I want to crop off. I have facing pages (2-up) and need to remove the 3/8" bleed (27 points) on the inside edge - left edge for odd pages, right edge for even.