OL Learn

Workflow Script to rotate landscape pages in PDF

This is the script I use to rotate Landscape pages to Portrait for PDFs with mixed plex. It works, but on large files it seems to get exponentially slower as it goes. On a particular job with 21,000 pages it is taking HOURS to complete. Is there a faster way to do this:

dim myPDF, myRotatedPDF, myRect
dim x, pageHeight, pageWidth, tempright, myRotatedPDFName

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(Watch.GetJobFilename())
tempFileName = objFSO.GetParentFolderName(objFile) & "\" & objFSO.GetTempName

Set myPDF = Watch.GetPDFEditObject
myPDF.open Watch.GetJobFileName,false

Set myRotatedPDF = Watch.GetPDFEditObject
myRotatedPDF.create tempFileName

Set myRect = CreateObject("AlambicEdit.PdfRect")

for x = 0 to myPDF.Pages.Count-1
  Set myRect = myPDF.Pages(x).size
  pageWidth = myRect.Right
  pageHeight = myRect.Top

  if (pageWidth < pageHeight) then
    myRect.Bottom = 0
    myRect.Left = 0
    myRect.Top = 792
    myRect.Right = 612
    myRotatedPDF.Pages.insert x, myRect
    myRotatedPDF.Pages(x).merge2 myPDF.Pages(x),0,0,0,1
  else
    tempright = myRect.Right
    myRect.Right = myRect.Top
    myRect.Top = tempright
    myRotatedPDF.Pages.insert x, myRect
    myRotatedPDF.Pages(x).merge2 myPDF.Pages(x),0,myRect.Top,90,1
  end if
next

myPDF.Close
myRotatedPDF.save true
myRotatedPDF.Close

objFSO.DeleteFile Watch.GetJobFileName()
objFSO.MoveFile tempFileName, Watch.GetJobFileName

set myRect = nothing
set myPDF = nothing
set objFile = nothing
set objFSO = nothing

The Merge() and Merge2() methods are costly: they stamp an existing PDF page onto another while applying rotation. You should therefore only use them when absolutely needed.

One way to save a lot of time is to use InsertFrom2() instead of Merge2() when rotation is not required.
So in the first part of your condition that handles portrait pages, you should use:

if (pageWidth < pageHeight) then
   myRotatedPDF.Pages.InsertFrom2 myPDF,x,1,x
else
  ...

Granted, if ALL your pages are landscape, then this won’t improve performance at all, but judging from your other post in which you raised the issue, it seems there’s a mix of portrait and landscape pages.

I ran your script on a PDF with about 4600 pages that had an equal number of landscape and portrait pages. Your original script ran in a little over 7 minutes, while the modified one took less than 4 minutes.

One additional way of improving the performance would be to modify the script so it groups the Merge2() and InsertFrom() operations when contiguous pages with the same orientation are encountered. Obviously, this would only improve performance if the original PDF contains contiguous series of landscape/portrait pages.

Hope that helps.