2

I wrote a piece of code that crops a pdf page and then re-insert this page in the global pdf using the Adobe Acrobat 10.0 Type Library for Excel VBA. The code works fine on my computer but crops too much on the one of my co-worker. I think it might come from the resolution (1440x900 for mine, 1600x900 for my co-worker) but I just don't see where the resolution might interfer in the code.

Dim acroRect, jso, page As Object
Dim pdf1 As Acrobat.CAcroPDDoc
Dim nameFile, s, exportCroppedPDF As String

Set acroRect = CreateObject("AcroExch.Rect")
Set pdf1 = CreateObject("AcroExch.PDDoc")

nameFile = "namefile.pdf"

If pdf1.Open(nameFile) Then
    Set jso = pdf1.GetJSObject
    Set page = pdf1.AcquirePage(pdf1.GetNumPages() - 1)

    'These values were found from some tests I did, there is no logic behind them
    acroRect.bottom = 22
    acroRect.Left = 35      
    acroRect.Right = 785    
    acroRect.Top = 589     

    page.CropPage (acroRect)

    exportCroppedPDF = "pathAndNamefile.pdf"

    s = jso.extractPages(0, pdf1.GetNumPages() - 1, exportCroppedPDF)

Else
    Debug.Print ("Can't open the file!")
End If

pdf1.Close
Set pdf1 = Nothing
Set acroRect = Nothing
Set jso = Nothing
Set page = Nothing

Debug.Print ("Crop successful")

I am not cumfortable at all with this library (the code comes from pieces of code I found on the Internet) so I might have wrote some wrong lines (but it initialy works). Thanks a lot for your help!

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
Chris
  • 124
  • 8
  • Note that `Dim nameFile, s, exportCroppedPDF As String` will only define the last variable as `String` and the first 2 remain of type `Variant`. You must declare a type for **every** variable: `Dim nameFile As String, s As String, exportCroppedPDF As String`. – Pᴇʜ Jun 20 '18 at 07:18
  • @Peh oh, I wasn't aware of that, thanks for the information – Chris Jun 20 '18 at 07:20

1 Answers1

0

According to the documentation CropPages has 4 arguments where acroRect should be the last one.

returnValue = Object.CropPages( nStartPage, nEndPage, nEvenOrOddPagesOnly, acroRect ) 

Parameters:

  • nStartPage: First page that is cropped. The first page in a PDDoc object is page 0.
  • nEndPage: Last page that is cropped.
  • nEvenOrOddPagesOnly Value indicating which pages in the range are cropped. Must be one of the following:
    • 0 means crop all pages in the range
    • 1 means crop only odd pages in the range
    • 2 means crop only even pages in the range
  • acroRect An AcroExch.Rect specifying the cropping rectangle, which is specified in user space.
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
  • @Peh, nice, but I don't see a way to crop a page no matter the resolution of the screen (if it is the problem) - I'll try something and will let you know, thanks for your time! – Chris Jun 20 '18 at 07:36
  • @Chris well I think it should not be a matter of screen resolution. I think you used the parameters the wrong way and it should work with `CropPage (0,0,0,acroRect)`. Can you test this? – Pᴇʜ Jun 20 '18 at 07:41
  • @Peh on my computer, the crop action works perfectly, but not on my co-worker's desk who has a different resolution than me. That's why I don't see anything but the resolution, but you are right, based on the documentation I don't think either it is a resolution matter. I'll test the function a bit later this morning and we'll keep you in touch for sure! Thanks! – Chris Jun 20 '18 at 07:46
  • @Peh Hello, weirdly, without any changes, the cropped PDFs worked fine on my co-worker's computer this morning. It seems that a reboot was enought. Plus, I tested the CropPages method today, and it also worked with the first 3 parameters in it. Thanks a lot for your time ! – Chris Jun 21 '18 at 08:14