本帖最后由 77七 于 2024-1-13 13:25 编辑
回复 44# feiyan
批处理调用pdftk 给无书签的pdf文件添加一个 文件名书签
- @echo off
- rem 批处理保存为utf-8编码格式
- chcp 65001 >nul
- for /f "delims=" %%i in ('dir /b /a-d *.pdf') do (
- pdftk "%%i" dump_data_utf8 output |find "BookmarkTitle:" 1>nul
- if errorlevel 1 (
- >report.txt (
- echo BookmarkBegin
- echo BookmarkTitle: %%~ni
- echo BookmarkLevel: 1
- echo BookmarkPageNumber: 1
- )
- pdftk "%%i" update_info_utf8 report.txt output $.pdf
- move $.pdf "%%i"
- )
- )
- del report.txt
复制代码
2024年1月13号更新
- @echo off
- rem 批处理保存为utf-8编码格式
- chcp 65001 >nul
- rem 仅给无书签的pdf文件添加文件名作为书签(part) ,所有文件添加(all)
- set "c=all"
-
- for /f "delims=" %%i in ('dir /b /a-d *.pdf') do (
- pdftk "%%i" dump_data_utf8 output >$
- setlocal
- if /i "%c%" equ "all" (
- call :1 "%%i"
- ) else if /i "%c%" equ "part" (
- find "BookmarkTitle:" <$ 1>nul || call :1 "%%i"
- )
- endlocal
- )
- del $;report.txt 2>nul
- pause & exit
-
- :1
- for /f "tokens=1* delims=[]" %%a in ('find /n "NumberOfPages: " ^<$') do (
- set #%%a=1
- )
- (for /f "tokens=1* delims=[]" %%a in ('find /n /v "" ^<$') do (
- echo=%%b
- if defined #%%a (
- echo BookmarkBegin
- echo BookmarkTitle: %~n1
- echo BookmarkLevel: 1
- echo BookmarkPageNumber: 1
- )
- ))>report.txt
- pdftk "%~1" update_info_utf8 report.txt output $.pdf
- move $.pdf "%~1"
- exit /b
复制代码
|