Ubuntu pdf添加和删除书签(ghostscript)

本文介绍如何在Ubuntu系统上使用Ghostscript为PDF文件添加书签,包括安装Ghostscript、创建包含目录信息的文本文件及使用Ghostscript命令将文本文件与PDF文件结合的过程。此外,还提供了移除PDF书签的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Ghostscript

Create bookmaeks

On my Ubuntu system, it’s impossible to use Adobe Acrobat to add pdf bookmarks. Usually, I substitute it with Foxit Reader but it seems that Foxit is not so compatible with Okular, my favorite pdf viewer. Finally I get the solution in stackoverflow and find the great power of Ghostscript. The references are as follows:

http://milan.kupcevic.net/ghostscript-ps-pdf/#marks
https://stackoverflow.com/questions/30304718/create-bookmarks-into-a-pdf-file-via-command-line

As far as bookmarks are concerned, Ghostscript does a good job. For instance, I would like to add bookmarks in the book called How to Read a Book.

First installation on Ubuntu.

sudo apt install ghostscript

Next I need to create a text file which contains the pdf information, including table of contents. The text file is named after HowtoRead.

[ /Title (How to Read a Book)
  /Author (Mortimer J. Adler & Charles Van Doren)
  /Subject (Reading Tech)
  /Keywords (reading)
  /ModDate (unknown)
  /CreationDate (unknown)
  /Creator (unknown)
  /Producer (unknown)
  /DOCINFO pdfmark
[ /Page 1 /View [/XYZ null null null] /Title (Cover) /OUT pdfmark
[ /Page 3 /View [/XYZ null null null] /Title (Contents) /OUT pdfmark
[ /Page 17 /View [/XYZ null null null] /Title (1 The Activity and Art of Reading) /OUT pdfmark
[ /Page 30 /View [/XYZ null null null] /Title (2 The Levels of Reading) /OUT pdfmark
[ /Page 35 /View [/XYZ null null null] /Title (3 The First Level of Reading: Elementary Reading) /OUT pdfmark
[ /Page 45 /View [/XYZ null null null] /Title (4 The Second Level of Reading: Inspectional Reading) /OUT pdfmark
...
...
[ /Page 248 /View [/XYZ null null null] /Title (16 How to Read History) /OUT pdfmark
[ /Page 269 /View [/XYZ null null null] /Title (17 How to Read Science and Mathematics) /OUT pdfmark
[ /Page 284 /View [/XYZ null null null] /Title (18 How to Read Philosophy) /OUT pdfmark
[ /Page 310 /View [/XYZ null null null] /Title (19 How to Read Social Science) /OUT pdfmark
[ /Page 323 /View [/XYZ null null null] /Title (20 The Fourth Level of Reading: Syntopical Reading) /OUT pdfmark
[ /Page 351 /View [/XYZ null null null] /Title (21 Reading and the Growth of the Mind) /OUT pdfmark
[ /Page 361 /View [/XYZ null null null] /Title (APPENDIX A A Recommended Reading List) /OUT pdfmark
[ /Page 377 /View [/XYZ null null null] /Title (APPENDIX B Exercises and Tests at the Four Levels of Reading) /OUT pdfmark

Then combine the file with a PostScript or a PDF file, using Ghostscript command.

gs -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=withmarks.pdf \
    HowtoRead.pdf HowtoRead

And withmarks.pdf is the new pdf that has bookmarks. Maybe the creation of the text file is boring and a little complex, but it indeed helps me. If you are using Vim editor, things become simple. As for Ghostscript command, man ghostscript or Official Documentation. Details about pdfmark can be found in pdfmark reference

Remove bookmarks

To remove unnecessary and stupid bookmarks, you can use the following commands on Ubuntu if Ghostscript has been installed:

pdf2ps filename.pdf

Remove the earlier pdf file and convert this .ps to .pdf.

ps2pdf filename.ps

Combine pdf

gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dQUIET -sOutputFile=combined.pdf sub1.pdf sub2.pdf
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值