Friday, October 22, 2021

#1 2021-08-07 02:14:18 am

Registered: 2019-10-23
Posts: 901

Get a attributedString from page number to end page in PDF

Here is other example of getting a attributedString from page number to end page number in PDF
and save it to the desktop as rtf data. The approach is not 100% accurate but it give the user the
benefit to convert a PDF with attributes to make a new document from a PDF.


use framework "Foundation"
use framework "Quartz"
use scripting additions

set thePath to POSIX path of (choose file of type {"pdf"})
set theURL to current application's |NSURL|'s fileURLWithPath:thePath

its attributedStringFromPDF:theURL fromPage:4 toPage:5 savePath:(POSIX path of (path to desktop) & "page4And5.rtf")
on attributedStringFromPDF:theURL fromPage:pageBegin toPage:pageEnd savePath:savePath
   set pdfDoc to current application's PDFDocument's alloc()'s initWithURL:theURL
   set saveURL to current application's |NSURL|'s fileURLWithPath:savePath
   set endCharacter to (pdfDoc's pageAtIndex:(pageEnd - 1))'s numberOfCharacters()
   set attributedString to (pdfDoc's selectionFromPage:(pdfDoc's pageAtIndex:(pageBegin - 1)) atCharacterIndex:0 toPage:(pdfDoc's pageAtIndex:(pageEnd - 1)) atCharacterIndex:(endCharacter - 1))'s attributedString()
   set rtfData to attributedString's RTFFromRange:(current application's NSMakeRange(0, attributedString's |length|())) documentAttributes:(current application's NSDictionary's dictionaryWithObject:"NSRTF" forKey:"DocumentType")
   rtfData's writeToURL:saveURL atomically:true
end attributedStringFromPDF:fromPage:toPage:savePath:

Last edited by Fredrik71 (2021-08-07 02:25:37 am)

if you are the expert, who will you call if its not your imagination.



Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)