# Linux下PDF书签的 添加/提取

**URL:** <https://forum.freemdict.com/t/topic/26133>\
**Category:** 软件经验交流展望\
**Tags:** 书签, linux, pdf\
**Created:** [2024 年1 月 16 日 06:27 UTC](https://forum.freemdict.com/t/topic/26133 "2024-01-16T06:27:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

作者： ![ylxdxx](https://forumcdn.freemdict.com/letter_avatar_proxy/v4/letter/y/b2d939/32.png) [@ylxdxx](https://forum.freemdict.com/u/ylxdxx)\
发布日期： [2024 年1 月 16 日 06:27 UTC](https://forum.freemdict.com/t/topic/26133/1 "2024-01-16T06:27:07Z")

</div>

在Linux下，一直没有称手的为PDF 添加/导出 书签的小工具，最近基于Pymupdf写了一个

```auto
usage: 
    pdf2outline -b bookmarks.txt -i input.pdf
    pdf2outline -b bookmarks.txt -i input.pdf -extract
    pdf2outline [-h] -b bookmarks.txt [-n 0] [-collapse 1] -i input.pdf [-o output.pdf] [-extract]

一个为PDF文件 添加/提取 书签的小程序
options:
  -h, --help show this help message and exit
  -b bookmarks.txt bookmarks text filename
  -n 0 page bias number, a integer
  -collapse 1 this will collapse all items with a hierarchy level greater than n
  -i input.pdf input PDF filename
  -o output.pdf output PDF filename
  -extract extract input PDF's bookmarks

最终页码 = page + page_bias

bookmarks.txt 说明 每行前面用 Tab 符的个数表示层级，中间亦用 Tab 分隔 每行可以二个或三个参数，如：    
    title1 page1
        title2 page2
        title3 page3 height1
    title4 page4 height2

    其中的height是书签跳转到page页面的位置
预设参数如下:
    页数偏移默认为0 (即 -n 0);
    默认全部标签收起 (即 -collapse 1)；    
    默认height为0；    
    没输出文件名时默认在原文件上修改    

```

脚本如下，需用自取：  
[pdf2outline.tar.gz](https://forum.freemdict.com/uploads/short-url/8GiMYoRDvsj6kBEuD6VOcMChnBT.gz) (2.0 KB)

---

<div class="post-metadata">

作者： ![Vim](https://forumcdn.freemdict.com/user_avatar/forum.freemdict.com/vim/32/8408_2.png) [@Vim](https://forum.freemdict.com/u/Vim)\
发布日期： [2024 年1 月 16 日 10:42 UTC](https://forum.freemdict.com/t/topic/26133/2 "2024-01-16T10:42:46Z")

</div>

PyMuPDF这个库不错，再借助ChatGPT，想要的基本功能聊聊天大都能实现。
