Tuesday, September 27, 2022

#1 2022-08-30 12:34:24 am

chrisndeca
Member
From:: Oakland, CA
Registered: 2005-06-28
Posts: 36

Need to crop a PDF for printing, but loosing annotations with SIPS

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.

I guess my next question would be, is it possible and or any easier to crop a PDF using asobjc? I see the in objective-c there is a setBounds:forBox: method, but I am unsure if that can be called by asobjc, and if so how it might be called.
https://developer.apple.com/documentati … guage=objc

Any guidance on this would be much appreciated!

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

Last edited by chrisndeca (2022-08-30 12:56:57 am)

Offline

 

#2 2022-08-30 01:31:51 am

chrisndeca
Member
From:: Oakland, CA
Registered: 2005-06-28
Posts: 36

Re: Need to crop a PDF for printing, but loosing annotations with SIPS

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?

Applescript:

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)

Last edited by chrisndeca (2022-08-30 01:32:58 am)

Offline

 

#3 2022-09-04 08:08:53 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1458

Re: Need to crop a PDF for printing, but loosing annotations with SIPS

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.

https://macscripter.net/viewtopic.php?id=49176

Last edited by peavine (2022-09-05 09:50:47 am)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)