Based on code from Shane Stanley and Yvan Koenig I have put together an Applescript which splits a PDF document with defined resolution into TIFF files.
The script worked well for me until I stumbled on a PDF document were several pages had been rotated.
Shane Stanley was so kind giving me advice how to find out if a page inside a PDF document has been rotated. The solution then is quite easy in swapping width and height so this page can be exported successfully.
Now I want to share the so far finished and working applescript with all forum readers.
I would be glad if the code help others in their projects.
But I am also open in any advice in making the code more robust and/or efficient. But be aware I am just a retired hobbyist who loves to create stuff.
Zebramusik
-- Credits to Shane Stanley and Yvan Koenig - thank you guys.
use scripting additions
use framework "Foundation"
use framework "AppKit"
use framework "Quartz"
use framework "QuartzCore"
-- aPDF: PDF file as POSIX path
-- aResolution: Dpi
-- aFolder: Target folder as POSIX path, must have a slash at the end
on run {aPDF, aResolution, aFolder}
set aResolution to (aResolution / 1)
set aURL to (current application's |NSURL|'s fileURLWithPath:aPDF)
set aPDFdoc to current application's PDFDocument's alloc()'s initWithURL:aURL
-- get page count
set pCount to aPDFdoc's pageCount()
--process each page
repeat with i from 0 to (pCount - 1)
set thisPage to (aPDFdoc's pageAtIndex:(i))
-- get dimensions
set pageSize to (thisPage's boundsForBox:(current application's kPDFDisplayBoxMediaBox))
set pageWidth to current application's NSWidth(pageSize)
set pageHeight to current application's NSHeight(pageSize)
--swap width/height if page has been rotated
set theRotation to thisPage's |rotation|()
if theRotation is not 0 then
set temp to pageWidth
set pageWidth to pageHeight
set pageHeight to temp
end if
-- do size calculations
set pixelWidth to (pageWidth * aResolution / 72) div 1
set pixelHeight to (pageHeight * aResolution / 72) div 1
-- make bitmaps
set theImageRep to (current application's NSPDFImageRep's imageRepWithData:(thisPage's dataRepresentation()))
set newRep to (current application's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:pixelWidth pixelsHigh:pixelHeight bitsPerSample:8 samplesPerPixel:4 hasAlpha:yes isPlanar:false colorSpaceName:(current application's NSDeviceRGBColorSpace) bytesPerRow:0 bitsPerPixel:32)
-- store the existing graphics context
current application's NSGraphicsContext's saveGraphicsState()
-- set graphics context to new context based on the new bitmapImageRep
(current application's NSGraphicsContext's setCurrentContext:(current application's NSGraphicsContext's graphicsContextWithBitmapImageRep:newRep))
(theImageRep's drawInRect:{origin:{x:0, y:0}, |size|:{width:pixelWidth, height:pixelHeight}} fromRect:(current application's NSZeroRect) operation:(current application's NSCompositeSourceOver) fraction:1.0 respectFlipped:false hints:(missing value))
-- restore state
current application's NSGraphicsContext's restoreGraphicsState()
-- make new image and page
(newRep's setSize:{pageWidth, pageHeight})
set theData to newRep's TIFFRepresentation()
set newRep to (current application's NSBitmapImageRep's imageRepWithData:theData)
set targData to (newRep's representationUsingType:(current application's NSTIFFFileType) |properties|:{NSTIFFCompressionNone:1})
-- write TIFF file
set outPath to (aFolder & add_leading_zeros(i + 1, 6) & ".tiff")
(targData's writeToFile:outPath atomically:true)
end repeat
end run
on add_leading_zeros(this_number, max_leading_zeros)
return text (max_leading_zeros * -1) thru -1 of ("00000000000000000" & this_number)
end add_leading_zeros