PDFKitでPDFの以下の設定を取得し、セットする。
- PDFの綴じ方向(右、左)
- PDFの単ページ、見開き設定および見開き時の表紙設定
どちらもPDFKitでは直接はサポートしていないので、CGPDFDocumentRef
から設定を取得して自分でセットする必要がある。
PDFの綴じ方向を取得
以下でPDFDocument
インスタンスからPDFが右綴じかどうかを取得できる。Direction
が存在しない、値が違うなどの理由で以下に該当しない場合は左綴じ。
CGPDFDocumentRef documentRef = pdfDocument.documentRef; CGPDFDictionaryRef catalogDictRef = CGPDFDocumentGetCatalog(documentRef); CGPDFDictionaryRef viewerDictRef; if(CGPDFDictionaryGetDictionary(catalogDictRef, "ViewerPreferences", &viewerDictRef)) { const char* name; if(CGPDFDictionaryGetName(viewerDictRef, "Direction", &name)) { if(strcmp(name, "R2L") == 0){ // 右綴じ } } }
*上記設定については以下の578ページ:TABLE 8.1 Entries in a viewer preferences dictionary
のDirection
を参照。
https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdf_reference_archive/pdf_reference_1-7.pdf
PDFの綴じ方向をセット
PDFKitではPDFView
のdisplayRTL
にYES
を指定することで右綴じにできる。
*macOSの場合は10.13以上でのみサポートされている。
PDF単ページ、見開き設定および見開き時の表紙設定
以下でPDFDocument
インスタンスからPDFの表示設定を取得できる。PageLayout
が存在しない、などの理由で以下に該当しない場合はSinglePage
として扱われるので単ページ表示。
PageLayout
の値の末尾にRight
が付く場合は「奇数番号のページが右側に来る」という意味なので、1ページ目が単ページで表示され、表紙設定になる。
CGPDFDocumentRef documentRef = pdfDocument.documentRef; CGPDFDictionaryRef catalogDictRef = CGPDFDocumentGetCatalog(documentRef); const char* name; if(CGPDFDictionaryGetName(catalogDictRef, "PageLayout", &name)) { if(strcmp(name, "SinglePage") == 0) { // 単ページ } else if(strcmp(name, "OneColumn") == 0) { // 単ページのスクロール表示 } else if(strcmp(name, "TwoColumnLeft") == 0) { // 見開きのスクロール表示 } else if(strcmp(name, "TwoColumnRight") == 0) { // 見開きのスクロール表示で1ページ目が表紙 } else if(strcmp(name, "TwoPageLeft") == 0) { // 見開き } else if(strcmp(name, "TwoPageRight") == 0) { // 見開きで1ページ目が表紙 } }
*上記設定については以下の140ページ:TABLE 3.25 Entries in the catalog dictionary
のPageLayout
を参照。
https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdf_reference_archive/pdf_reference_1-7.pdf
PDFの単ページ、見開き設定をセット
単ページ、見開き設定についてはPDFView
のdisplayMode
に値をセットする。
見開き時に1ページ目を表紙として表示する設定についてはPDFView
のdisplayAsBook
にYES
をセットする。