Wednesday, June 17, 2009

How to crop a PDF file using QTP.

See main post here.

'Here I am cropping a PDF file named abc.pdf and after cropping it saving it as tt.pdf. While cropping a PDF file make sure you keep left and bottom values as 0 as shown in the code below. For more understanding see the screenshots below.

strFileName = "C:\abc.pdf"
Set AcroApp = CreateObject("AcroExch.App")
AcroApp.Show
Set AcroAVDoc = CreateObject("AcroExch.AVDoc")
AcroAVDoc.Open strFileName,""
Set AcroAVDoc = AcroApp.GetActiveDoc
Set AcroPDDoc = AcroAVDoc.GetPDDoc
For i = 0 To AcroPDDoc.GetNumPages - AcroPDDoc.GetNumPages
Set PageNumber = AcroPDDoc.AcquirePage(i)
Set acropoint = PageNumber.GetSize()
msgbox acropoint.y
Set rect = CreateObject("AcroExch.Rect")

rect.Left = 0
rect.Right = 612
rect.Top = 720 'it was 792 originally
rect.Bottom = 0
a=PageNumber.CropPage(rect)

Next

b = AcroPDDoc.save (1,"C:\tt.pdf")
AcroPDDoc.close
AcroAVDoc.Close True
AcroApp.Exit
Set AcroDoc = Nothing
Set AcroApp = Nothing



Also See:

Acrobatusers

Example:

As you can see below, initially the values for right and top coordinates are 8.50 and 11.00 (Inches) or (612 X 792 Points) respectively. If you want to see the dimensions (top, right, left, bottom) in Points instead of Inches, follow the below steps in section "How to check coordinates in Points instead of Inches?"



Below is the screenshot after cropping the file by giving the values to rect.right as 612 and to rect.top as 700 in the code above:






How to check coordinates in Points instead of Inches?

Open any PDF document and click on Pages on the left side.



Right click on any page and select Crop Pages...



Crop Pages dialog box opens. From the Units dropdown select Points.



On the right hand side you will see the dimensions in Points.