+class DocFile
+ def initialize(filename, page)
+ @filename = filename
+ @page = page
+ end
+ def convert()
+ return if @swfname
+ @swfname = @filename.gsub(/.pdf$/i,"")+".swf"
+ $tempfiles += [@swfname]
+ `pdfinfo #{@filename}` =~ /Page size:\s*([0-9]+) x ([0-9]+) pts/
+ width,height = $1,$2
+ dpi = (72.0 * 612 / width.to_i).to_i
+ output = `pdf2swf -f --flatten -s zoom=#{dpi} -p #{@page} #{@filename} -o #{@swfname} 2>&1`
+ #output = `pdf2swf -s zoom=#{dpi} --flatten -p #{@page} #{@filename} -o #{@swfname} 2>&1`
+ raise ConversionFailed.new(output,@swfname) unless File.exists?(@swfname)
+ end
+ def render()
+ return if @img
+ convert()
+ @pngname = @filename.gsub(/.pdf$/i,"")+".png"
+ begin
+ output = `swfrender --legacy #{@swfname} -o #{@pngname} 2>&1`
+ raise ConversionFailed.new(output,@pngname) unless File.exists?(@pngname)
+ @img = Magick::Image.read(@pngname).first
+ ensure
+ `rm -f #{@pngname}`
+ end
+ end
+ def get_text(x1,y1,x2,y2)
+ self.convert()
+ #puts "swfstrings -x #{x1} -y #{y1} -W #{x2-x1} -H #{y2-y1} #{@swfname}"
+ #puts `swfstrings -x #{x1} -y #{y1} -W #{x2-x1} -H #{y2-y1} #{@swfname}`
+ `swfstrings -x #{x1} -y #{y1} -W #{x2-x1} -H #{y2-y1} #{@swfname}`.chomp
+ end
+ def get_links(x1,y1,x2,y2)
+ self.convert()
+ t = `swfdump -a #{@swfname}`
+ links = t.scan(/GetUrl2? URL:"([^"]*)"/).inject([]) do |a,u| a + u end
+ t.scan(/name "url:([^"]*)"/).inject(links) do |a,u| a + u end
+ end
+ def get_area(x1,y1,x2,y2)
+ self.render()
+ data = @img.export_pixels(x1, y1, x2-x1, y2-y1, "RGB")
+ Array.new(data.size/3) { |i| data.slice(i*3,3) }
+ end
+ def area_at(x1,y1,x2,y2)
+ return Area.new(x1,y1,x2,y2,self)
+ end
+ def width()
+ self.render()
+ return @img.columns
+ end
+ def height()
+ self.render()
+ return @img.rows
+ end
+ def pixel_at(x,y)
+ self.render()
+ data = @img.export_pixels_to_str(x, y, 1, 1, "RGB")
+ return Pixel.new(x,y,data)
+ end
+end