LoginSignup
4
4

More than 5 years have passed since last update.

Swift と CoreFoundation

Posted at

何となく CoreFoundation にある CGPDF* まわりを swift でさわってみた。動くかどうかは知らないよ ;-p

PDFKit の PDFDocument クラスにあるコンストラクタの使い方がわからないから、CoreFoundationを使ってこうなったわけじゃないよ。

//
//  main.swift
//  swtest
//
//  Created by Atsushi Yasui on 2014/06/17.
//  Copyright (c) 2014年 N/A. All rights reserved.
//

import Foundation
import CoreFoundation

var path = "file:///Users/yasui/Documents/develop/swtest/swtest/PDFKitGuide.pdf"
var url  = NSURL.URLWithString(path) as CFURLRef
var pdfref = CGPDFDocumentCreateWithURL(url)

//
NSLog("Path: \(path)")

// PDF の情報書き出し
let page_count :UInt = CGPDFDocumentGetNumberOfPages(pdfref)
NSLog("ページ数: \(page_count)")

var maVersion: CInt = 0
var miVersion: CInt = 0
CGPDFDocumentGetVersion(pdfref, &maVersion, &miVersion)
NSLog("Version: \(maVersion).\(miVersion)")

let allowCoping: CBool = CGPDFDocumentAllowsCopying(pdfref)
NSLog("Can Copy ? => \(allowCoping)")

let allowPrint: CBool = CGPDFDocumentAllowsPrinting(pdfref)
NSLog("Can Print ? => \(allowPrint)")

let isEncrypted: CBool = CGPDFDocumentIsEncrypted(pdfref)
NSLog("Is Encrypted ? => \(isEncrypted)")

let isUnLocked: CBool = CGPDFDocumentIsUnlocked(pdfref)
NSLog("Is UnLocked ? => \(isUnLocked)")

// 1pずつ見ていく
for var i: UInt = 1; i <= page_count; i++ {
    var page:CGPDFPage = CGPDFDocumentGetPage(pdfref, i)

    // 用紙サイズとか
    let mediaBox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox)
    let cropBox  = CGPDFPageGetBoxRect(page, kCGPDFCropBox)
    let bleedBox = CGPDFPageGetBoxRect(page, kCGPDFBleedBox)
    let trimBox  = CGPDFPageGetBoxRect(page, kCGPDFTrimBox)
    let artBox   = CGPDFPageGetBoxRect(page, kCGPDFArtBox)

    NSLog("Load Page \(i)")
    NSLog("[Page:\(i)] mediaBox: \(mediaBox)")
    NSLog("[Page:\(i)] cropBox : \(cropBox)")
    NSLog("[Page:\(i)] bleedBox: \(bleedBox)")
    NSLog("[Page:\(i)] trimBox : \(trimBox)")
    NSLog("[Page:\(i)] artBox  : \(artBox)")
}

// CGPDFDictionary 周りは CGPDFDictionaryApplyFunction が使えないから、Keyがわからんオブジェクトは swift では無理っぽい?
// Catalog の抽出。Catalog てなんぞ?
//var Catalog : CGPDFDictionary = CGPDFDocumentGetCatalog(pdfref)
//
// Infoの抽出。PDFの製作者情報とかかしら
//var Info : CGPDFDictionary = CGPDFDocumentGetInfo(pdfref)



CGPDFDocumentRelease(pdfref)
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4