PDFKitでPDFの綴じ方向、見開き時の表紙設定を取得してセットする

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 dictionaryDirectionを参照。 https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdf_reference_archive/pdf_reference_1-7.pdf

PDFの綴じ方向をセット

PDFKitではPDFViewdisplayRTLYESを指定することで右綴じにできる。
*macOSの場合は10.13以上でのみサポートされている。

developer.apple.com

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 dictionaryPageLayoutを参照。 https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdf_reference_archive/pdf_reference_1-7.pdf

PDFの単ページ、見開き設定をセット

単ページ、見開き設定についてはPDFViewdisplayModeに値をセットする。

developer.apple.com

見開き時に1ページ目を表紙として表示する設定についてはPDFViewdisplayAsBookYESをセットする。

developer.apple.com