So I have PDF that contains the contents of a label. I need to print the label information to a label printer, so the information that is in the PDF that does not pertain to the label needs to be cropped out. I have tried using SIPS to create a PNG from the PDF, but when I do so, it drops all the annotations that are in the PDF, and that just happens to be where all the important information is kept for the label. For those that are unaware, a PDF form stores all the information that is filled in, in the annotations fields.
In doing research I have found some 10 year old posts where Shane explained that turning a PDF into a raster image was not a trivial task using asobjc. I am just wondering if anyone has actually gotten this to work in asobjc, and if so, does the conversion keep the annotations in place when the PDF is converted to a raster image.
[del]Any guidance on this would be much appreciated![/del]
Sorry I should have done more research. I found some posts by Fredrik71 that showed that yes indeed setBounds:forBox can be used via asobjc. Thanks Fredrik71!
Model: 14" MacBook Pro M1 Pro
Browser: Safari 605.1.15
Operating System: macOS 12
Well this code “works” but it has the opposite problem. Now only the annotations show up in the newly created PDF page. Any ideas what I did incorrectly here?
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use framework "Foundation"
use framework "Quartz"
property mediabox : a reference to current application's kPDFDisplayBoxMediaBox
set theDate to do shell script "date"
set {saveTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {":"}}
set textDate to text items of theDate
set AppleScript's text item delimiters to {"-"}
set newDate to textDate as string
set AppleScript's text item delimiters to {""}
set thePath to POSIX path of (choose file)
set theURL to current application's |NSURL|'s fileURLWithPath:thePath
set pdfDoc to current application's PDFDocument's alloc()'s initWithURL:theURL
set firstPage to (pdfDoc's pageAtIndex:0)
set boundingBox to firstPage's boundsForBox:(current application's kPDFDisplayBoxMediaBox)
set pageSize to firstPage's boundsForBox:mediabox
set item 1 of item 2 of pageSize to 400
set item 2 of item 2 of pageSize to 400
firstPage's setBounds:pageSize forBox:mediabox
set destFolder to "/Users/chris/Desktop/"
set destPosixPath to (destFolder & newDate & "-cropped.pdf") as string
set outNSURL to current application's class "NSURL"'s fileURLWithPath:destPosixPath
(pdfDoc's insertPage:firstPage atIndex:0)
(pdfDoc's writeToURL:outNSURL)
chrisndeca. I’m a bit late and perhaps you’ve found another solution, but my script at the following location may do what you want. I tested it with annotated PDF documents, and the cropped PDF included the annotations.