Friday, October 22, 2021

#1 2021-10-11 08:14:39 am

From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1089

PDF to PNG Script

This script creates individual PNG files from one or several PDF files, each of which can contain one or multiple pages. The operation of this script requires no explanation except to note:

* Significant portions of the pdfToPng handler were obtained from a script written by Shane (see post 12 in the thread linked below).

* The name of the PNG file is the same as that of the PDF file but with a page number appended and a png extension. If the PDF contains only one page, the page number is omitted.

* Existing PNG files are overwritten without prompting the user.

* The user can set the desired resolution of the PNG image at the top of the script.

* The script includes a confirmation dialog which is easily disabled.

* This script is intended for use with PDF's that contain a relatively small number of pages, although it did successfully (if a bit slowly) create PNG files at 300 dpi for Shane's 159-page ASObjC book.

* If the user wants to convert just a few PDF's to PNG format, Preview is a simple alternative.


-- revised 2021.10.12

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

on main()
   set theDPI to 72
   set theFiles to (choose file of type "com.adobe.pdf" with multiple selections allowed)
   repeat with aFile in theFiles
       pdfToPng(POSIX path of aFile, theDPI)
   end repeat
end main

on pdfToPng(pdfPath, theDPI)
   set thePdf to (current application's |NSURL|'s fileURLWithPath:pdfPath)
   set pdfDoc to current application's PDFDocument's alloc()'s initWithURL:thePdf
   set pdfPages to pdfDoc's pageCount()
   set pdfName to thePdf's lastPathComponent() -- disable this code section if desired
   if pdfPages = 1 then
       set dialogText to quote & pdfName & quote & " contains 1 page"
       set dialogText to quote & pdfName & quote & " contains " & pdfPages & " pages"
   end if
   set dialogResult to button returned of (display alert dialogText message "Select " & quote & "OK " & quote & "to create a PNG file for each page" buttons {"Cancel", "Skip", "OK"} default button 3)
   if dialogResult = "Skip" then return
   if dialogResult = "Cancel" then error number -128
   set pdfPath to current application's NSString's stringWithString:pdfPath
   set pdfPath to pdfPath's stringByDeletingPathExtension()
   repeat with i from 0 to (pdfPages - 1)
       if pdfPages > 1 then
           set thePng to (pdfPath's stringByAppendingString:(" (" & (i + 1) & ")"))
           set thePng to (thePng's stringByAppendingPathExtension:"png")
           set thePng to (pdfPath's stringByAppendingPathExtension:"png")
       end if
       set aPage to (pdfDoc's pageAtIndex:i)
       set pageSize to (aPage's boundsForBox:(current application's kPDFDisplayBoxMediaBox))
       set pageWidth to current application's NSWidth(pageSize)
       set pageHeight to current application's NSHeight(pageSize)
       set pixelWidth to (pageWidth * theDPI / 72) div 1
       set pixelHeight to (pageHeight * theDPI / 72) div 1
       set theImageRep to (current application's NSPDFImageRep's imageRepWithData:(aPage'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)
       current application's NSGraphicsContext's saveGraphicsState()
       (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))
       current application's NSGraphicsContext's restoreGraphicsState()
       (newRep's setSize:{pageWidth, pageHeight})
       set theData to newRep's TIFFRepresentation()
       set newRep to (current application's NSBitmapImageRep's imageRepWithData:theData)
       set theData to (newRep's representationUsingType:(current application's NSPNGFileType) |properties|:(missing value))
       (theData's writeToFile:thePng atomically:true)
   end repeat
end pdfToPng


The thread that contains Shane's script is:

Last edited by peavine (2021-10-12 03:11:52 pm)

2018 Mac mini - macOS Catalina - Script Debugger 8



Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)