如何将页码添加到Postscript/PDF

Bri*_*unt 30 pdf postscript

如果你在Postscript中有一个大文件(500页+)并想要添加页码,有人知道怎么做吗?

Bri*_*unt 24

根据rcs提出的解决方案,我做了以下事情:

将文档转换为example.pdf并运行pdflatex addpages,其中addpages.tex读取:

\documentclass[8pt]{article}
\usepackage[final]{pdfpages}
\usepackage{fancyhdr}

\topmargin 70pt
\oddsidemargin 70pt

\pagestyle{fancy}
\rfoot{\Large\thepage}
\cfoot{}
\renewcommand {\headrulewidth}{0pt}
\renewcommand {\footrulewidth}{0pt}

\begin{document}
\includepdfset{pagecommand=\thispagestyle{fancy}}
\includepdf[fitpaper=true,scale=0.98,pages=-]{example.pdf}
% fitpaper & scale aren't always necessary - depends on the paper being submitted.
\end{document}
Run Code Online (Sandbox Code Playgroud)

或者,对于双面页面(即页面编号始终在外面):

\documentclass[8pt]{book}
\usepackage[final]{pdfpages}
\usepackage{fancyhdr}

\topmargin 70pt
\oddsidemargin 150pt
\evensidemargin -40pt

\pagestyle{fancy}
\fancyhead{} 
\fancyfoot{} 
\fancyfoot[LE,RO]{\Large\thepage}

\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}

\begin{document}
\includepdfset{pages=-,pagecommand=\thispagestyle{fancy}}
\includepdf{target.pdf}
\end{document}
Run Code Online (Sandbox Code Playgroud)

更改标题边距的简便方法:

% set margins for headers, won't shrink included pdfs
% you can remove the topmargin/oddsidemargin/evensidemargin lines
\usepackage[margin=1in,includehead,includefoot]{geometry}
Run Code Online (Sandbox Code Playgroud)


Din*_*ngo 15

你可以简单地使用

pspdftool

通过这种方式:

pspdftool 'number(x=-1pt,y=-1pt,start=1,size=10)' input.pdf output.pdf
Run Code Online (Sandbox Code Playgroud)

看到这两个例子(用pspdftool 编号和编号为 pdf)

无编号的pdf

http://ge.tt/7ctUFfj2

编号为pdf

http://ge.tt/7ctUFfj2

将此作为第一个命令行参数:

number(start=1, size=40, x=297.5 pt, y=10 pt)
Run Code Online (Sandbox Code Playgroud)


rcs*_*rcs 13

这可能是一个解决方案:

  1. 使用将postscript转换为pdf ps2pdf
  2. 创建一个LaTeX文件并使用pdfpages包插入页面(\includepdf)
  3. 在参数中使用pagecommand={\thispagestyle{plain}}或者来自fancyhdr包的东西\includepdf
  4. 如果需要postscript输出,请将pdflatex输出转换回postscript via pdf2ps


Dar*_*idl 13

我过去常常使用乳胶在我的pdf中添加页码,就像在接受的答案中一样.

现在我发现了一个更简单的方法:使用enscript创建包含页码头空页,然后使用pdftkmultistamp选择把标题上您的文件.

这个bash脚本期望pdf文件是唯一的参数:

#!/bin/bash
input="$1"
output="${1%.pdf}-header.pdf"
pagenum=$(pdftk "$input" dump_data | grep "NumberOfPages" | cut -d":" -f2)
enscript -L1 --header='||Page $% of $=' --output - < <(for i in $(seq "$pagenum"); do echo; done) | ps2pdf - | pdftk "$input" multistamp - output $output
Run Code Online (Sandbox Code Playgroud)

  • 对其他用户的警告:仅自 build 1.43 pdftk 具有 *multistamp* 功能 - 关于代码我可以建议 output="${1%03d.pdf}-header.pdf" 具有零均衡 (2认同)

Jak*_*kob 9

我正在寻找使用 ghostscript 的仅 postscript 解决方案。我需要它来合并多个 PDF 并在每一页上放置一个计数器。我找到的唯一解决方案是旧的 gs-devel 帖子,我对其进行了大量简化:

%!PS
% add page numbers document bottom right (20 units spacing , harcoded below)
% Note: Page dimensions are expressed in units of the default user space (72nds of an inch).
% inspired by https://www.ghostscript.com/pipermail/gs-devel/2005-May/006956.html

globaldict /MyPageCount 1 put % initialize page counter

% executed at the end of each page. Before calling the procedure, the interpreter
% pushes two integers on the operand stack:
% 1. a count of previous showpage executions for this device
% 2. a reason code indicating the circumstances under which this call is being made:
%    0: During showpage or (LanguageLevel 3) copypage
%    1: During copypage (LanguageLevel 2 only)
%    2: At device deactivation
% The procedure must return a boolean value specifying whether to transmit the page image to the
% physical output device.
<< /EndPage {
  exch pop % remove showpage counter (unused)
  0 eq dup { % only run and return true for showpage
    /Helvetica 12 selectfont % select font and size for following operations
    MyPageCount =string cvs % get page counter as string
    dup % need it twice (width determination and actual show)
    stringwidth pop % get width of page counter string ...
    currentpagedevice /PageSize get 0 get % get width from PageSize on stack
    exch sub 20 sub % pagewidth - stringwidth - some extra space
    20 moveto % move to calculated x and y=20 (0/0 is the bottom left corner)
    show % finally show the page counter
    globaldict /MyPageCount MyPageCount 1 add put % increment page counter
  } if
} bind >> setpagedevice
Run Code Online (Sandbox Code Playgroud)

如果将其保存到名为的文件中pagecount.ps,则可以在命令行上使用它,如下所示:

gs \
  -dBATCH -dNOPAUSE \
  -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress \
  -sOutputFile=/path/to/merged.pdf \
  -f pagecount.ps -f input1.pdf -f input2.pdf
Run Code Online (Sandbox Code Playgroud)

请注意,必须首先给出 pagecount.ps(从技术上讲,就在页面计数应该开始的输入文件之前)。

如果你不想使用额外的.ps文件,你也可以使用这样的最小化形式:

gs \
  -dBATCH -dNOPAUSE \
  -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress \
  -sOutputFile=/path/to/merged.pdf \
  -c 'globaldict /MyPageCount 1 put << /EndPage {exch pop 0 eq dup {/Helvetica 12 selectfont MyPageCount =string cvs dup stringwidth pop currentpagedevice /PageSize get 0 get exch sub 20 sub 20 moveto show globaldict /MyPageCount MyPageCount 1 add put } if } bind >> setpagedevice' \
  -f input1.pdf -f input2.pdf
Run Code Online (Sandbox Code Playgroud)

根据您的输入,您可能必须在 if 块的开头/结尾使用gsave/ grestore

  • 另外,如果您希望显示总页码: `gs -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -o "$output" -c "globaldict /My​​PageCount 1 put /concatstrings { exch dup length 2 index length add string dup dup 4 2 roll 复制长度 4 -1 roll putinterval } bind def &lt;&lt; /EndPage {exch pop 0 eq dup {/Helvetica 12 selectfont MyPageCount =string cvs ( / $npages) concatstrings dup stringwidth pop currentpagedevice /PageSize get 0 get exch sub 20 sub 20 moveto show globaldict /My​​PageCount MyPageCount 1 add put } if } 绑定 &gt;&gt; setpagedevice" -f input1.pdf` (2认同)

Joh*_*ney 6

我喜欢使用pspdftool手册页)的想法,但我想要的是x 页面的 y格式以及与页面其余部分相匹配的字体样式。

要了解文档中使用的字体名称:

$ strings input.pdf | grep Font
Run Code Online (Sandbox Code Playgroud)

要获取页数:

$ pdfinfo input.pdf | grep "Pages:" | tr -s ' ' | cut -d" " -f2
Run Code Online (Sandbox Code Playgroud)

使用一些pspdftool命令将其粘合在一起:

$ in=input.pdf; \
out=output.pdf; \
indent=30; \
pageNumberIndent=49; \
pageCountIndent=56; \
font=LiberationSerif-Italic; \
fontSize=9; \
bottomMargin=40; \
pageCount=`pdfinfo $in | grep "Pages:" | tr -s ' ' | cut -d" " -f2`; \
pspdftool "number(x=$pageNumberIndent pt, y=$bottomMargin pt, start=1, size=$fontSize, font=\"$font\")" $in tmp.pdf; \
pspdftool "text(x=$indent pt, y=$bottomMargin pt, size=$fontSize, font=\"$font\", text=\"page \")" tmp.pdf tmp.pdf; \
pspdftool "text(x=$pageCountIndent pt, y=$bottomMargin pt, size=$fontSize, font=\"$font\", text=\"out of $pageCount\")" tmp.pdf $out; \
rm tmp.pdf;
Run Code Online (Sandbox Code Playgroud)

结果如下:

在此输入图像描述


小智 5

除了captaincomic的解决方案之外,我还扩展了它以支持在任何页面上开始页码编号.

需要enscript,pdftk 1.43或更高版本以及pdfjam(用于pdfjoin实用程序)

#!/bin/bash
input="$1"
count=$2
blank=$((count - 1))
output="${1%.pdf}-header.pdf"
pagenum=$(pdftk "$input" dump_data | grep "NumberOfPages" | cut -d":" -f2)
(for i in $(seq "$blank"); do echo; done) | enscript -L1 -B --output - | ps2pdf - > /tmp/pa$$.pdf
(for i in $(seq "$pagenum"); do echo; done) | enscript -a ${count}- -L1 -F Helvetica@10 --header='||Page $% of $=' --output - | ps2pdf - > /tmp/pb$$.pdf
pdfjoin --paper letter --outfile /tmp/join$$.pdf /tmp/pa$$.pdf /tmp/pb$$.pdf &>/dev/null
cat /tmp/join$$.pdf | pdftk "$input" multistamp - output "$output"
rm /tmp/pa$$.pdf
rm /tmp/pb$$.pdf
rm /tmp/join$$.pdf
Run Code Online (Sandbox Code Playgroud)

例如..将它放在/usr/local/bin/pagestamp.sh中并执行如下:

pagestamp.sh doc.pdf 3
Run Code Online (Sandbox Code Playgroud)

这将在第3页开始页码..当你有封面,标题页和目录等时很有用.

不幸的是,enscript的--footer选项被破坏了,所以你无法使用这种方法在底部获得页面编号.