Acrobat BookMark

PDFのしおり(BookMarkまたはoutline)の一覧取得

A:Acrobatを使う
B:PDFKitを使う
C:Pythonを使う



より詳細に
リンク先の『場所』まで取得できるのはPDFkit


A:Acrobatを使う
[Acrobat]bookmark しおりの一覧取得 タブ区切りテキスト保存
https://quicktimer.cocolog-nifty.com/icefloe/2024/05/post-6f2f90.html
[Acrobat]bookmark しおりの一覧取得 JSコンソール版
https://quicktimer.cocolog-nifty.com/icefloe/2024/05/post-2650be.html
AcrobatのXMLからブックマーク(しおり)の要素を取得して一覧にする
https://quicktimer.cocolog-nifty.com/icefloe/2024/05/post-27071f.html


B:PDFKitを使う
カテゴリー
https://quicktimer.cocolog-nifty.com/icefloe/cat76054237/index.html
[PDFkit]しおり(BOOKMARK)の一覧取得(Acrobat不要タイプ)途中
https://quicktimer.cocolog-nifty.com/icefloe/2024/05/post-116814.html


C:Pythonを使う
PdfReaderのoutlineを利用するが、ネストされたBookMarkの場合
タイトルすらまともに取得できないので、現時点では対象外
[pypdf] PDFのbookmarkしおり=outlineのコピー(簡易版)
https://quicktimer.cocolog-nifty.com/icefloe/2024/04/post-f6aa82.html
[pypdf] PDFのドキュメント内 リンクオブジェクトを収集する
https://quicktimer.cocolog-nifty.com/icefloe/2024/04/post-b51b7f.html

|

[Acrobat]bookmark しおりの一覧取得 JSコンソール版

こちらのページのJSコンソール実行版
あくまでも参考にしてください

サンプルソース(参考)
行番号ソース
001function getbkmNamesAndPages(bkm) {
002  bkm.execute();
003  var output = '{\"' + bkm.name + '\", ' + (this.pageNum + 1) + '}';
004  console.println(bkm.name + '\t' + (this.pageNum + 1));
005  if (bkm.children != null) {
006    for (var i = 0; i < bkm.children.length; i++)
007      output += ', ' + getbkmNamesAndPages(bkm.children[i]);
008  }
009  return output;
010}
011'{' + getbkmNamesAndPages(this.bookmarkRoot) + '}';
AppleScriptで生成しました

|

AcrobatのXMLからブックマーク(しおり)の要素を取得して一覧にする

リンク先の要素のIDが取得できる方法
202405060543531576x962
別名で保存からが良い 書き出しの『com.adobe.acrobat.xml-1-00』はエラーになることが多い
あくまでも参考にしてください

【スクリプトエディタで開く】 |

サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003(*
004com.cocolog-nifty.quicktimer.icefloe
005*)
006----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
007use AppleScript version "2.8"
008use framework "Foundation"
009use framework "AppKit"
010use scripting additions
011property refMe : a reference to current application
012
013set appFileManager to refMe's NSFileManager's defaultManager()
014
015###ダイアログを前面に出す
016set strName to (name of current application) as text
017if strName is "osascript" then
018  tell application "Finder" to activate
019else
020  tell current application to activate
021end if
022#デフォルトロケーション
023set appFileManager to refMe's NSFileManager's defaultManager()
024set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
025set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
026set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias
027#
028set listUTI to {"public.xml"}
029set strMes to ("ファイルを選んでください") as text
030set strPrompt to ("ファイルを選んでください") as text
031try
032  set aliasFilePath to (choose file strMes with prompt strPrompt default location (aliasDefaultLocation) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias
033on error
034  log "エラーしました"
035  return "エラーしました"
036end try
037###入力ファイル
038set strFilePath to (POSIX path of aliasFilePath) as text
039set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
040set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
041set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
042
043### XML読み込み
044set ocidOption to (refMe's NSXMLDocumentTidyXML)
045set listResponse to refMe's NSXMLDocument's alloc()'s initWithContentsOfURL:(ocidFilePathURL) options:(ocidOption) |error| :(reference)
046if (item 2 of listResponse) ≠ (missing value) then
047  log (item 2 of listResponse)'s localizedDescription() as text
048  return "読み込みに失敗しました"
049else if (item 2 of listResponse) = (missing value) then
050  set ocidReadXMLDoc to (item 1 of listResponse)
051end if
052
053#最終的に保存するテキスト
054set ocidSaveString to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
055ocidSaveString's setString:("タイトル\tID\n")
056#値取得用のDICT
057set ocidSaveDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
058
059###ROOT
060set ocidRootElement to ocidReadXMLDoc's rootElement()
061#bookmark-tree
062set ocidBkmTreeArray to ocidRootElement's elementsForName:("bookmark-tree")
063set ocidBkmTree to ocidBkmTreeArray's firstObject()
064set ocidBkmArray to ocidBkmTree's elementsForName:("bookmark")
065set numCntBkmRoot to (count of ocidBkmArray) as integer
066#ルート要素の数だけ繰り返し
067repeat with itemNo from 0 to (numCntBkmRoot - 1) by 1
068  #取り出して
069  set ocidBkmItem to (ocidBkmArray's objectAtIndex:(itemNo))
070  #値を取得して
071  set ocidTitleValue to (ocidBkmItem's attributeForName:("title"))'s stringValue()
072  set ocidDistArray to (ocidBkmItem's elementsForName:("destination"))
073  set ocidDist to ocidDistArray's firstObject()
074  set ocidDistValue to (ocidDist's attributeForName:("structID"))'s stringValue()
075  #出力用に保存していく
076  (ocidSaveString's appendString:("▼"))
077  (ocidSaveString's appendString:(ocidTitleValue))
078  (ocidSaveString's appendString:("\t"))
079  (ocidSaveString's appendString:(ocidDistValue))
080  (ocidSaveString's appendString:("\n"))
081  (ocidSaveDict's setValue:(ocidDistValue) forKey:(ocidTitleValue))
082  #子要素があるか?調べて
083  set ocidBkmSubArray to (ocidBkmItem's elementsForName:("bookmark"))
084  set numCntCild to (count of ocidBkmSubArray) as integer
085  #子要素があれば子要素取得に回す
086  if numCntCild = 0 then
087    log "子要素無"
088  else
089    log "子要素有"
090    set listResponse to doGetBkmValue({ocidBkmSubArray, ocidSaveString, ocidSaveDict})
091    log class of listResponse
092    set ocidSaveString to (item 1 of listResponse)
093    set ocidSaveDict to (item 2 of listResponse)
094  end if
095end repeat
096
097############
098###子要素取得
099to doGetBkmValue({argBkmSubArray, argSaveString, argSaveDict})
100  #子要素の数を数えて
101  set numCntCild to (count of argBkmSubArray) as integer
102  #要素の数だけ繰り返し
103  repeat with itemChildNo from 0 to (numCntCild - 1) by 1
104    #値を取得して
105    set ocidBkmItem to (argBkmSubArray's objectAtIndex:(itemChildNo))
106    set ocidTitleValue to (ocidBkmItem's attributeForName:("title"))'s stringValue()
107    set ocidDistArray to (ocidBkmItem's elementsForName:("destination"))
108    set ocidDist to ocidDistArray's firstObject()
109    set ocidDistValue to (ocidDist's attributeForName:("structID"))'s stringValue()
110    #保存用データに渡す
111    (argSaveString's appendString:(ocidTitleValue))
112    (argSaveString's appendString:("\t"))
113    (argSaveString's appendString:(ocidDistValue))
114    (argSaveString's appendString:("\n"))
115    (argSaveDict's setValue:(ocidDistValue) forKey:(ocidTitleValue))
116    #子要素があるか確認して
117    set ocidBkmSubArray to (ocidBkmItem's elementsForName:("bookmark"))
118    set numCntCild to (count of ocidBkmSubArray) as integer
119    #子要素がなければ親要素に戻る
120    if numCntCild = 0 then
121      return ({argSaveString, argSaveDict})
122    else
123      log "Childあり"
124      ##子要素があれば子要素取得する
125      doGetBkmValue({ocidBkmSubArray, argSaveString, argSaveDict})
126    end if
127  end repeat
128end doGetBkmValue
129
130##############################
131#保存先URL
132set ocidBaseFilePathURL to ocidFilePathURL's URLByDeletingPathExtension()
133set ocidTsvFilePathURL to ocidBaseFilePathURL's URLByAppendingPathExtension:("bkm.tsv")
134
135#テキスト保存
136set listDone to ocidSaveString's writeToFile:(ocidTsvFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference)
137if (item 1 of listDone) is true then
138  log "正常終了"
139else if (item 1 of listDone) is false then
140  log (item 2 of listDone)'s localizedDescription() as text
141  return "保存に失敗しました"
142end if
143
144return
145
146#PLIST保存 必要があれば
147set ocidPlistFilePathURL to ocidBaseFilePathURL's URLByAppendingPathExtension:("plist")
148set ocidFormat to (refMe's NSPropertyListXMLFormat_v1_0)
149set listResponse to refMe's NSPropertyListSerialization's dataWithPropertyList:(ocidSaveDict) format:(ocidFormat) options:0  |error| :(reference)
150if (item 2 of listResponse) ≠ (missing value) then
151  log (item 2 of listResponse)'s localizedDescription() as text
152  return "データ変換に失敗しました"
153else if (item 2 of listResponse) = (missing value) then
154  set ocidPlistData to (item 1 of listResponse)
155end if
156set ocidOption to (refMe's NSDataWritingAtomic)
157set listDone to ocidPlistData's writeToURL:(ocidPlistFilePathURL) options:(ocidOption) |error| :(reference)
158if (item 2 of listResponse) ≠ (missing value) then
159  log (item 2 of listResponse)'s localizedDescription() as text
160  return "保存に失敗しました"
161else if (item 2 of listResponse) = (missing value) then
162  return "正常終了"
163end if
164
165###開く
166set aliasFilePath to (ocidTsvFilePathURL's absoluteURL()) as alias
167tell application "Finder"
168  open location aliasFilePath
169end tell
AppleScriptで生成しました

|

[Acrobat]bookmark しおりの一覧取得 タブ区切りテキスト保存


あくまでも参考にしてください

【スクリプトエディタで開く】 |

サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#  com.cocolog-nifty.quicktimer.icefloe
004# BaseScript
005# https://www.macscripter.net/t/adobe-acrobat-summoning-bookmark-properties-results-in-error-1708/75844/5
006#
007# Acroabt Readerでは動作しません
008----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
009use AppleScript version "2.8"
010use framework "Foundation"
011use scripting additions
012
013property refMe : a reference to current application
014
015tell application "Adobe Acrobat"
016  tell front PDF Window
017    set strName to name as text
018  end tell
019end tell
020##PDFドキュメントを開いてから
021if strName is "" then
022  return "PDFを開いていません Acrobatで開いてください"
023end if
024
025tell application "Adobe Acrobat"
026  tell active doc
027    set aliasFilePath to file alias as alias
028    set strFileName to name as text
029  end tell
030end tell
031##入力ファイル
032set strFilePath to (POSIX path of aliasFilePath) as text
033set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
034set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
035##出力先 タブ区切りテキスト
036set ocidBaseFilePath to ocidFilePath's stringByDeletingPathExtension()
037set ocidSaveFilePath to ocidBaseFilePath's stringByAppendingPathExtension:("tsv")
038##########################
039##本処理
040##########################
041set strJavascript to "function getBmkNamesAndPages(bmk) {\n\tbmk.execute();\n\tvar output = '{\"' + bmk.name + '\", ' + (this.pageNum + 1) + '}';\n\tif (bmk.children != null) {\n\t\tfor (var i = 0; i < bmk.children.length; i++)\n\t\t\toutput += ', ' + getBmkNamesAndPages(bmk.children[i]); \n\t}\n\treturn output ;\n}\n'{' + getBmkNamesAndPages(this.bookmarkRoot) + '}';"
042#JS実行
043tell application "Adobe Acrobat"
044  tell active doc to set strResult to do script strJavascript
045end tell
046#戻り値をリストに
047set listBookMark to (run script strResult)'s rest
048##########################
049#リストの数
050set numCntBookMark to (count of listBookMark) as integer
051#出力用のテキスト
052set ocidSavestring to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
053#列タイトル
054ocidSavestring's setString:("NO\t名称\tリンク先ページ\n")
055#リストの数だけ繰り返し
056repeat with itemNo from 1 to (numCntBookMark) by 1
057  #3桁ゼロパディング
058  set numNo to itemNo as integer
059  set ocidFormatter to refMe's NSNumberFormatter's alloc()'s init()
060  (ocidFormatter's setMinimumIntegerDigits:(3))
061  (ocidFormatter's setMaximumIntegerDigits:(3))
062  set ocidDecStr to (ocidFormatter's stringFromNumber:(numNo))
063  (ocidSavestring's appendString:(ocidDecStr))
064  #値の取得
065  set listItemList to (item itemNo of listBookMark) as list
066  set strName to (item 1 of listItemList) as text
067  set strGotoNO to (item 2 of listItemList) as text
068  (ocidSavestring's appendString:("\t"))
069  (ocidSavestring's appendString:(strName))
070  (ocidSavestring's appendString:("\t"))
071  (ocidSavestring's appendString:(strGotoNO))
072  (ocidSavestring's appendString:("\n"))
073end repeat
074
075#保存
076set listDone to ocidSavestring's writeToFile:(ocidSaveFilePath) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference)
077if (item 1 of listDone) is true then
078  return "正常終了"
079else if (item 1 of listDone) is false then
080  log (item 2 of listDone)'s localizedDescription() as text
081  return "保存に失敗しました"
082end if
083
084
085
086
AppleScriptで生成しました

|

[Acrobat]bookmark しおりの取得 (ROOTのみ 途中)


あくまでも参考にしてください

【スクリプトエディタで開く】 |

サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003# com.cocolog-nifty.quicktimer.icefloe
004----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
005use AppleScript version "2.6"
006use scripting additions
007
008
009tell application "Adobe Acrobat"
010  tell active doc
011    set numCntBkmRoot to (do script ("this.bookmarkRoot.children.length")) as integer
012    log "BookMarkROOTitems : " & numCntBkmRoot
013  end tell
014  repeat with itemIntNo from 0 to (numCntBkmRoot - 1) by 1
015    tell active doc
016      tell bookmark itemIntNo
017        set strBookMarkName to (do script ("this.bookmarkRoot.children[" & itemIntNo & "].name")) as text
018        log "BookMarkName :" & strBookMarkName
019        do script ("this.bookmarkRoot.children[" & itemIntNo & "].execute();")
020      end tell
021    end tell
022    tell front PDF Window
023      set strPageNO to (page number) as text
024      log "BookMarkGoToPageNO: " & strPageNO
025    end tell
026  end repeat
027end tell
AppleScriptで生成しました

|

その他のカテゴリー

Accessibility Acrobat Acrobat 2020 Acrobat AddOn Acrobat Annotation Acrobat ARMDC Acrobat AV2 Acrobat BookMark Acrobat Classic Acrobat DC Acrobat Dialog Acrobat Distiller Acrobat Form Acrobat JS Acrobat Manifest Acrobat Menu Acrobat Open Acrobat Plugin Acrobat Preferences Acrobat Preflight Acrobat python Acrobat Reader Acrobat SCA Acrobat SCA Updater Acrobat Sequ Acrobat Sign Acrobat Stamps Acrobat Watermark Acrobat Windows Acrobat Windows Reader Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Mobileconfig Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin Support Admin System Information Admin Tools Admin Users Admin Volumes Adobe Adobe FDKO Adobe RemoteUpdateManager AppKit Apple AppleScript AppleScript do shell script AppleScript List AppleScript ObjC AppleScript Osax AppleScript PDF AppleScript Pictures AppleScript record AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript System Events AppleScript System Events Plist AppleScript Video Applications AppStore Archive Attributes Automator BackUp Barcode Barcode QR Barcode QR Decode Bash Basic Basic Path Bluetooth BOX Browser Calendar CD/DVD Choose Chrome CIImage CityCode CloudStorage Color com.apple.LaunchServices.OpenWith Console Contacts CotEditor CURL current application Date&Time delimiters Desktop Device Diff Disk Dock DropBox Droplet eMail Encode % Encode Decode Encode UTF8 Error EXIFData ffmpeg File Finder Firefox Folder FolderAction Fonts GIF github Guide HTML HTML Entity Icon Illustrator Image Events Image2PDF ImageOptim iPhone iWork Javascript Jedit Json Label Leading Zero List locationd LRC lsappinfo LSSharedFileList m3u8 Mail MakePDF Map Math Media Media AVAsset Media AVconvert Media AVFoundation Media AVURLAsset Media Movie Media Music Memo Messages Microsoft Microsoft Edge Microsoft Excel Mouse Music NetWork Notes NSArray NSArray Sort NSBezierPath NSBitmapImageRep NSBundle NSCFBoolean NSCharacterSet NSColor NSColorList NSData NSDecimalNumber NSDictionary NSError NSEvent NSFileAttributes NSFileManager NSFileManager enumeratorAtURL NSFont NSFontManager NSGraphicsContext NSImage NSIndex NSKeyedArchiver NSKeyedUnarchiver NSLocale NSMutableArray NSMutableDictionary NSMutableString NSNotFound NSNumber NSOpenPanel NSPasteboard NSpoint NSPredicate NSPrintOperation NSRange NSRect NSRegularExpression NSRunningApplication NSScreen NSSize NSString NSString stringByApplyingTransform NSStringCompareOptions NSTask NSTimeZone NSURL NSURL File NSURLBookmark NSURLComponents NSURLResourceKey NSURLSession NSUserDefaults NSUUID NSView NSWorkspace Numbers OAuth OneDrive PDF PDFAnnotation PDFAnnotationWidget PDFContext PDFDisplayBox PDFDocumentPermissions PDFImageRep PDFKit PDFnUP PDFOutline perl Photoshop PlistBuddy pluginkit postalcode PostScript prefPane Preview Python QuickLook QuickTime ReadMe Regular Expression Reminders ReName Repeat RTF Safari SaveFile ScreenCapture ScreenSaver SF Symbols character id SF Symbols Entity sips Skype Slack Sound Spotlight sqlite SRT StandardAdditions Swift System Settings TCC TemporaryItems Terminal Text Text CSV Text MD Text TSV TextEdit Tools Translate Trash Twitter Typography UI Unit Conversion UTType valueForKeyPath Video VisionKit Visual Studio Code VMware Fusion Wacom webarchive webp Wifi Windows XML XML EPUB XML OPML XML Plist XML RSS XML savedSearch XML SVG XML TTML XML webloc XML XMP YouTube zoom