Thread: Cropping PDFs
View Single Post
Old 08-14-2006, 11:07 AM   #9
harryo began at the beginning.
Posts: 34
Karma: 10
Join Date: Aug 2006
Device: Palm Tungsten TX / Iliad
Originally Posted by ali
What did you do, and how?
I don't know how ali did it, but I used this piece of Ruby code
cropLeft   = ARGV[0].to_i
cropRight  = ARGV[1].to_i
cropTop    = ARGV[2].to_i
cropBottom = ARGV[3].to_i

originalPath = ARGV[4]
croppedPath  = originalPath.sub(/\.pdf$/, " (cropped).pdf"), "rb") do |input|
  pdf =

  pdf.gsub!(/\/MediaBox \[\s*(\d*)\s*(\d*)\s*(\d*)\s*(\d*)\s*\]/) do
      n1 = $1.to_i + cropLeft
      n2 = $2.to_i + cropTop
      n3 = $3.to_i - cropRight
      n4 = $4.to_i - cropBottom

      "/MediaBox [#{n1} #{n2} #{n3} #{n4}]"
  end, "wb") { |output| output.write pdf }
I don't have a reference for the /MediaBox parameters, so I did it by trial and error. In reality, I didn't take much care, just ran the program and fiddled the four values until I got what looked good for each document.

I just wanted to get something working. I'll clean it up later, once I have the details worked out a bit better.
harryo is offline   Reply With Quote