当前位置: 首页>>技术教程>>正文


重新编号PDF页面

,

问题描述

我想编辑扫描的PDF的元数据,以将自定义页码分配给不同的页面。例如,现在我可能要呼叫i,ii和iii的第1-3页,什么是我要呼叫1-7的第4-10页。我不想更改页面的实际顺序。

是否有A)使用免费工具完全做到这一点的方法; B)一种执行此操作的方法”in batch”(因此,不必手动重新编号每个页面)。

最佳办法

这里是一个基于LaTeX的解决方案。它使用pdfpages包来包含扫描的PDF(此处称为scan.pdf)。您可以使用启用了pdfpagelabels选项的hyperref软件包来设置所需的PDF页面标签。它使用普通的\thepage宏作为标签,可以将其定义为小写罗马数字。然后,页面计数器将重置并变回普通数字。

\documentclass[a4paper]{article}% or use 'letterpaper'
\usepackage{pdfpages}
\usepackage[pdfpagelabels]{hyperref}
\begin{document}
% Set lower case roman numbers (\Roman would be upper case):
\renewcommand{\thepage}{\roman{page}}
\includepdf[pages=1-3]{scan.pdf}
% Back to normal (arabic) numbers:
\renewcommand{\thepage}{\arabic{page}}
% Reset page counter to 1:
\setcounter{page}{1}
\includepdf[pages=4-]{scan.pdf}
\end{document}

将上面的代码放入文件(例如scan_mod.tex)中,并使用pdflatex进行编译:

# pdflatex scan_mod

这将产生scan_mod.pdf。但是,包括任何特殊注释。超链接将消失。扫描的PDF应该不会有任何问题。

如果更经常需要此脚本,则可以编写一个脚本,该脚本接受罗马编号的页数和文件名作为参数,并使用上述代码创建一个临时文件,其中名称和数字为变量,然后将其编译。

次佳办法

您可以使用文本编辑器执行此操作。

如答案所示,用文本编辑器打开一个PDF文件,搜索/Catalog条目,然后附加一个名为/PageLabels的条目,如下所示:

/PageLabels << /Nums [
0 << /P (cover) >> % labels 1st page with the string "cover"
1 << /S /r >> % numbers pages 2-6 in small roman numerals
6 << /S /D >> % numbers pages 7-x in decimal arabic numerals
]
>>

请注意,页面索引(物理页面编号)以0开头。

当然,您可以使用脚本语言自动执行此操作。

PDF Standards – Page Labels具有详细规格。

第三种办法

jPDF Tweak是一个开放源代码图形实用程序,提供页码编号(正确的术语为”page labeling”)以及许多其他初学者到高级PDF编辑功能。它可以在Ubuntu和其他操作系统上运行。

Documentation页面提供了step-by-step指令。

第四种办法

有一个名为PDF Mod的工具,它是一个免费工具,可以重新排列PDF页面。

可以从Ubuntu 10.10及更高版本中的Ubuntu软件中心安装。

要在Ubuntu 9.10或10.04中安装:

安装将ppa ppa:pdfmod-team/ppa添加到您的软件源(Here’s how to do that)并从软件中心安装pdfmod

改编自:http://www.webupd8.org/2011/03/edit-pdf-documents-in-linux-with-pdf.html

祝你好运:D

第五种办法

刚刚找到一个可以为此使用ghostscript的指针,在这里:pdftk – Add and edit bookmarks to pdf – Unix and Linux – Stack Exchange #18600;它指的是链接:

但是,以上内容处理书签-而不处理逻辑分页。从pdfmarkReference.pdf得出,所需的”command”为’/Label‘(或’/PAGELABEL‘)-并进一步参考PDFReference.pdf的第8.3.1 “Page Labels”章。不幸的是,该章不必解释如何将pdfmarks与页面标签一起使用-但这篇文章确实做到了:

The /PAGELABEL pdfmark does not have any /Page key, so one can set the label for the ‘current’ page only (and, as a consequence, only for one page at a time). Since you call it at the very beginning, it’s expected to set a label for the 1st page and only for it.

Multiple /PAGELABELs for the same page: the pdfmark reference says the last one takes effect, so the result of your 1st commandline is OK. Note the /Page key is ignored.

How to set page labels from PostScript? I can think of 2 methods:

(A) The 100% documented way:

Issue a /PAGELABEL as part of each page.

(B) The less documented way: …

gswin32c -sDEVICE=pdfwrite -sOutputFile=50pages.pdf -dNOPAUSE

GS>[/_objdef {pl} /type /dict /OBJ pdfmark
GS>[{pl} <</Nums [0 <</P (Page ) /S /r /St 10>> 2 <<>>]>> /PUT pdfmark
GS>[{Catalog} <</PageLabels {pl}>> /PUT pdfmark
GS>50 { showpage } repeat
GS>quit

…并进一步在该线程中:

As to making this work; since the original file is a PDF file, you can run each page from the file individually. So you can set the PAGELABEL pdfmark for page 1, run page 1 from the original file, set the PAGELABEL for page 2, run page 2 from the original file and so on.

Because the label is (as SaGS) said applied to the current page, this should correctly set the labels for each page in the output PDF file. (caveat: I haven’t actually tried this)

编辑:仅显示此内容-如果您将此另存为pdfmarks文件:

[ /Label (-1) /PAGELABEL pdfmark
showpage
[ /Label (0) /PAGELABEL pdfmark
showpage
[ /Label (1) /PAGELABEL pdfmark
showpage

…然后您致电:

gs -q -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=outfile.pdf infile.pdf pdfmarks

…然后您将在infile.pdf的末尾附加三个空白页,分别标记为-1、0和1 🙂

好吧,这也许会在某个时候帮助您获得一个更简单的gs脚本来重新编号页面:)

EDIT2:知道了,我想-使用与上面相同的gs命令-下面是pdfmarks脚本的内容,它将重新编号infile.pdf,所以它以-1、0、1开头…基本上是修改后的PDF参考中的示例(有关更多信息,请参见注释):

% Type name (Optional) The type of PDF object that this dictionary describes; if present, must be PageLabel for a page label dictionary.
% S name (Optional) The numbering style to be used for the numeric portion of each page label:
%       D Decimal arabic numerals
%       R Uppercase roman numerals
%       r Lowercase roman numerals
%       A Uppercase letters (A to Z for the first 26 pages, AA to ZZ for the next 26, and so on)
%       a Lowercase letters (a to z for the first 26 pages, aa to zz for the next 26, and so on)
% P text string (Optional) The label prefix for page labels in this range.
% St integer (Optional) The value of the numeric portion for the first page label in the range. Subsequent pages will be numbered sequentially from this value, which must be greater than or equal to 1. Default value: 1.

% renumber first 25 pages - push each by 10, and add prefix:
% [/_objdef {pl} /type /dict /OBJ pdfmark
% [{pl} <</Nums [0 <</P (Page ) /S /D /St 10>> 25 <<>>]>> /PUT pdfmark
% [{Catalog} <</PageLabels {pl}>> /PUT pdfmark

[/_objdef {pl} /type /dict /OBJ pdfmark
[{pl} <</Nums [ 0 << /P (-1) >>         % just label -1 (no style) for pg 0;
                1 << /P (0) >>          % just label  0 (no style) for pg 1;
                2 << /S /D /St 1 >>     % decimal style, start from 1, for pg2 and on.
                ]>> /PUT pdfmark
[{Catalog} <</PageLabels {pl}>> /PUT pdfmark

第六种办法

有一个小的Python脚本可以完成这项工作:https://github.com/lovasoa/pagelabels-py

在您的情况下,请致电:

./addpagelabels.py --delete file.pdf
./addpagelabels.py --startpage 1 --type 'roman lowercase' file.pdf
./addpagelabels.py --startpage 4 --type arabic file.pdf

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/11692.html,未经允许,请勿转载。