如何向Postscript/PDF添加页码

如何向Postscript/PDF添加页码,pdf,postscript,Pdf,Postscript,如果你在Postscript中有一个大文档(500页以上),并且想添加页码,有人知道怎么做吗?也许pstops(部分)可以用于此?哦,我已经很久没有使用Postscript了,但是快速浏览一下蓝皮书就会告诉你:)www-cdf.fnal.gov/offline/Postscript/BLUEBOOK.PDF 另一方面,AdobeAcrobat和一点javascript也会创造奇迹;) 或者,我确实发现了这个:,它似乎符合要求(我没有尝试过)这可能是一个解决方案: 使用ps2pdf 创建一个LaT

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

也许pstops(部分)可以用于此?

哦,我已经很久没有使用Postscript了,但是快速浏览一下蓝皮书就会告诉你:)www-cdf.fnal.gov/offline/Postscript/BLUEBOOK.PDF

另一方面,AdobeAcrobat和一点javascript也会创造奇迹;)


或者,我确实发现了这个:,它似乎符合要求(我没有尝试过)

这可能是一个解决方案:

  • 使用
    ps2pdf
  • 创建一个LaTeX文件并使用pdfpages包插入页面(
    \includepdf
  • \includepdf
    的参数中使用
    pagecommand={\thispagestyle{plain}}
    或fancyhdr包中的某些内容
  • 如果需要postscript输出,请通过
    pdf2ps

  • 我假设您正在寻找基于PS的解决方案。PS中没有允许您执行此操作的页面级运算符。您需要在每页的
    PageSetup
    部分中添加页脚类型的内容。任何脚本语言都应该能够帮助您。基于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}
    
    或者,对于双面页面(即页码始终在外部):

    更改页眉边距的简便方法:

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

    我曾经用latex在我的pdf中添加页码,就像在接受的答案中一样

    现在我找到了一个更简单的方法: 使用
    enscript
    创建页眉包含页码的空页面,然后使用
    pdftk
    multistamp
    选项将页眉放在文件中

    此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
    
    #/bin/bash
    输入=“$1”
    output=“${1%.pdf}-header.pdf”
    pagenum=$(pdftk“$input”转储|U数据| grep“NumberOfPages”|剪切-d:“-f2)
    enscript-L1--页眉='| |第$%页,共$='--输出-<您只需使用

    pspdtool

    这样:

    pspdftool 'number(x=-1pt,y=-1pt,start=1,size=10)' input.pdf output.pdf
    
    请参见以下两个示例(未编号和已编号的pdf和pspdtool)

    未编号的pdf

    编号的pdf

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

    number(start=1, size=40, x=297.5 pt, y=10 pt)
    

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

    需要enscript、pdftk 1.43或更高版本和pdfjam(用于pdfjoin实用程序)

    例如。。将其放置在/usr/local/bin/pagestamp.sh中并执行如下操作:

    pagestamp.sh doc.pdf 3
    
    这将从第3页的页码开始。。当您有封面、标题页和目录等时非常有用

    不幸的是,enscript的--footer选项已被破坏,因此无法使用此方法获取底部的页码。

    我尝试了pspdtool()

    我最终让它工作了,但一开始我犯了一个错误:

    pspdftool: xreftable read error
    
    源文件是使用pdfjam中的pdfjoin创建的,其中包含来自我的爱普生员工团队的一系列扫描以及生成的标记页。我无法找到修复外部参照表的方法,因此我使用pdf2ps转换为ps,使用pdf2ps转换回pdf。然后我可以用它在右下角得到漂亮的页码:

    pspdftool 'number(start=1, size=20, x=550 pt, y=10 pt)' input.pdf output.pdf
    
    不幸的是,这意味着任何文本搜索页面都不再可搜索,因为文本在ps转换中被光栅化。幸运的是,对我来说这并不重要


    有没有办法在不丢失可搜索页面的情况下修复或清空pdf文件的外部参照表?

    我采用了captaincomic的解决方案,增加了对包含空格的文件名的支持,并提供了一些有关进度的更多信息

    #!/bin/bash
    clear
    echo
    echo This skript adds pagenumbers to a given .pdf file.
    echo 
    echo This skript needs the packages pdftk and enscript
    echo if not installed the script will fail.
    echo use the command sudo apt-get install pdftk enscript
    echo to install.
    echo 
    input="$1"
    output="${1%.pdf}-header.pdf"
    echo input file is $input
    echo output file will be $output
    echo 
    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"
    echo done.
    
    #/bin/bash
    清楚的
    回声
    echo此脚本将页码添加到给定的.pdf文件中。
    回音
    echo此skcript需要包pdftk和enscript
    echo如果未安装,脚本将失败。
    echo使用命令sudo apt get install pdftk enscript
    echo安装。
    回音
    输入=“$1”
    output=“${1%.pdf}-header.pdf”
    echo输入文件是$input
    echo输出文件将为$output
    回音
    pagenum=$(pdftk“$input”转储|U数据| grep“NumberOfPages”|剪切-d:“-f2)
    
    enscript-L1——页眉=“| |第$%页,共$=”——输出-<我编写了以下
    shell
    脚本来解决
    LaTeX
    beamer
    风格的幻灯片,这些幻灯片是用
    inkscape
    制作的(我
    pdftk cat
    将幻灯片一起放入最终演示文稿
    PDF
    ,然后使用下面的脚本添加幻灯片编号):

    #/垃圾箱/垃圾箱
    #创建工作目录
    tmpdir=$(mktemp--目录)
    #从标准格式中读取未编号的beamer幻灯片PDF并创建临时副本
    cat>$tmpdir/input.pdf
    #获取总页数
    pagenum=$(pdftk$tmpdir/input.pdf dump|u data | awk'/NumberOfPages/{print$NF}')
    #使用所需数量的空幻灯片生成latex beamer文档
    打印文件“%s”
    \文档类{beamer}
    \使用导航符号模板{}
    \setbeamertemplate{footline}[帧编号]
    \usepackage{forloop}
    \开始{document}
    \新计数器{thepage}
    \forloop{thepage}{0}{\value{thepage}<'$pagenum'}{
    \开始{frame}
    \结束{帧}
    }
    \结束{document}
    '>$tmpdir/numbers.tex
    #将latex文件编译成PDF(总页数需要第二次运行)并将输出重定向到STDERR
    pdflatex-output directory=$tmpdir numbers.tex>&2&&pdflatex-output directory=$tmpdir numbers.tex>&2
    #将空编号的PDF幻灯片作为背景添加到(透明背景)输入幻灯片(逐页)
    #页面)将结果写入标准输出(&W)
    pdftk$tmpdir/input.pdf多背景$tmpdir/numbers.pdf输出-
    #删除包含所有中间文件的临时工作目录
    rm-r$tmpdir>&2
    
    脚本读取
    STDIN
    &写入
    STDOUT
    printin
    pspdftool 'number(start=1, size=20, x=550 pt, y=10 pt)' input.pdf output.pdf
    
    #!/bin/bash
    clear
    echo
    echo This skript adds pagenumbers to a given .pdf file.
    echo 
    echo This skript needs the packages pdftk and enscript
    echo if not installed the script will fail.
    echo use the command sudo apt-get install pdftk enscript
    echo to install.
    echo 
    input="$1"
    output="${1%.pdf}-header.pdf"
    echo input file is $input
    echo output file will be $output
    echo 
    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"
    echo done.
    
    $ strings input.pdf | grep Font
    
    $ pdfinfo input.pdf | grep "Pages:" | tr -s ' ' | cut -d" " -f2
    
    $ 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;
    
    %!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
    
    gs \
      -dBATCH -dNOPAUSE \
      -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress \
      -sOutputFile=/path/to/merged.pdf \
      -f pagecount.ps -f input1.pdf -f input2.pdf
    
    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