Last active
August 31, 2018 06:42
-
-
Save doraTeX/6f49e9bb0ad9d691fa8c to your computer and use it in GitHub Desktop.
Show page box info of PDF by using Swift and Quartz ( http://doratex.hatenablog.jp/entry/20150909/1441748482 )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// How to compile: | |
// swiftc -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -o pageboxinfo pageboxinfo.swift | |
import Foundation | |
import Quartz | |
extension CGPDFBox { | |
var title: String { | |
switch self { | |
case .mediaBox: | |
return "MediaBox: " | |
case .cropBox: | |
return "CropBox: " | |
case .bleedBox: | |
return "BleedBox: " | |
case .trimBox: | |
return "TrimBox: " | |
case .artBox: | |
return "ArtBox: " | |
} | |
} | |
} | |
struct PageBox { | |
private let pdfPage: PDFPage | |
init(pdfPage: PDFPage) { | |
self.pdfPage = pdfPage | |
} | |
private func bboxString(boxType: CGPDFBox, clipWithMediaBox: Bool, hires: Bool = true) -> String { | |
let pageRef = pdfPage.pageRef! | |
let mediaBoxRect = pageRef.getBoxRect(.mediaBox) | |
var rect = pageRef.getBoxRect(boxType) | |
rect = clipWithMediaBox ? rect.intersection(mediaBoxRect) : rect | |
if hires { | |
return String(format:"%8.2f %8.2f %8.2f %8.2f", | |
rect.origin.x, | |
rect.origin.y, | |
rect.origin.x + rect.size.width, | |
rect.origin.y + rect.size.height) | |
} else { | |
return String(format:"%d %d %d %d", | |
floor(rect.origin.x), | |
floor(rect.origin.y), | |
floor(rect.origin.x) + ceil(rect.size.width), | |
floor(rect.origin.y) + ceil(rect.size.height)) | |
} | |
} | |
private func printBox(_ boxType: CGPDFBox, prefix: String = "", hires: Bool = true) { | |
let rawBox = bboxString(boxType: boxType, clipWithMediaBox: false, hires: hires) | |
let clippedBox = bboxString(boxType: boxType, clipWithMediaBox: true, hires: hires) | |
let title = boxType.title | |
print(prefix + title + rawBox) | |
if rawBox != clippedBox { | |
print(" *clipped " + title + clippedBox) | |
} | |
} | |
func printAllBoxes(prefix: String = "") { | |
printBox(.mediaBox, prefix: prefix) | |
printBox(.cropBox, prefix: prefix) | |
printBox(.bleedBox, prefix: prefix) | |
printBox(.trimBox, prefix: prefix) | |
printBox(.artBox, prefix: prefix) | |
print(prefix + "Rotation: \(pdfPage.rotation)") | |
} | |
} | |
func printPageBoxes(filePath: String) { | |
let url = URL(fileURLWithPath: filePath) | |
guard let doc = PDFDocument(url: url) else { | |
print("ERROR: Invalid file - \(filePath)") | |
return | |
} | |
let pageCount = doc.pageCount | |
let printBar = { print("---------------------------------------------------------") } | |
printBar() | |
(0..<pageCount).forEach { pageIndex in | |
let pageBox = PageBox(pdfPage: doc.page(at: pageIndex)!) | |
pageBox.printAllBoxes(prefix: String(format: "Page %4d ", pageIndex+1)) | |
printBar() | |
} | |
} | |
// Main | |
let arguments = ProcessInfo.processInfo.arguments | |
guard arguments.count == 2 else { | |
print("Usage: \((arguments[0] as NSString).lastPathComponent) <PDF_FILE_NAME>") | |
exit(1) | |
} | |
let pdfPath = arguments[1] | |
guard FileManager.default.fileExists(atPath: pdfPath) else { | |
print("ERROR: \(pdfPath) does not exist.") | |
exit(1) | |
} | |
printPageBoxes(filePath: pdfPath) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment