如何将 PDF 文件中的页面图像向左或向右移动?

发布于 2024-09-13 20:11:54 字数 218 浏览 1 评论 0原文

我们有一堆扫描页面(大约 600 个),每个 PDF 查看器都显示右边缘零边距的图像,但左边缘大约 2 英寸边距。 (大概是在扫描时使用了错误的设置......)

我们想要打印这些页面,最好是作为小册子。有没有办法永久地将所有页面图像移向中心,并使 PDF 以更令人满意的方式显示这些页面? Ghostscript 能做到吗?可以用其他方法来做到这一点,例如在某些 PDF 处理库的帮助下以编程方式实现吗?

We have a bunch of scanned pages (about 600) for which every PDF viewer displays the image with zero margin on the right edge, but about 2 inch margin on the left. (Presumably while scanning, there was a wrong setting used...)

We want to print these pages, preferably as a booklet. Is there a way to permanently shift all page images towards the center and have the PDF display these pages also in a more pleasing way? Can Ghostscript do that? Can one do this with some other method, such as programatically with the help of some PDF processing library?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱冒险 2024-09-20 20:11:54

如果您不想编写自己的程序代码(如 Nikolaus 建议的那样),而是使用 Ghostscript 命令行,则需要了解 3 件事:

  1. PostScript 有一个 setpagedevice 运算符,它接受 PageOffset 参数;
  2. 如果您在命令行上使用 -c ... 传递 PostScript 代码片段,Ghostscript 将处理它们;
  3. Ghostscript 可以评估和应用(某些)PostScript 代码,甚至可以直接进行 PDF=>PDF 转换。

现在尝试此命令行将所有页面图像向左移动 1 英寸(==72pt):

gswin32c.exe ^
  -sDEVICE=pdfwrite ^
  -o c:/path/to/output/pdf-shifted-by-1-inch-to-left.pdf ^
  -dPDFSETTINGS=/prepress ^
  -c "<</PageOffset [-72 0]>> setpagedevice" ^
  -f c:/path/to/input/pdf-original.pdf

(我放入的 -dPDFSETTINGS=/prepress 是为了不丢失任何图片质量扫描...)

If you don't want to write your own program code (as Nikolaus suggested), but use a Ghostscript commandline instead, you need to know 3 things:

  1. PostScript has a setpagedevice operator that takes a PageOffset parameter;
  2. Ghostscript will process snippets of PostScript code if you pass them with -c ... on the commandline;
  3. Ghostscript can evaluate and apply (some) PostScript code even for direct PDF=>PDF conversions.

Now try this commandline to shift all page images by 1 inch (==72pt) to the left:

gswin32c.exe ^
  -sDEVICE=pdfwrite ^
  -o c:/path/to/output/pdf-shifted-by-1-inch-to-left.pdf ^
  -dPDFSETTINGS=/prepress ^
  -c "<</PageOffset [-72 0]>> setpagedevice" ^
  -f c:/path/to/input/pdf-original.pdf

(The -dPDFSETTINGS=/prepress I put in in order to not loose any picture quality of the scans...)

酒几许 2024-09-20 20:11:54

您可以使用 iText 移动、缩放或裁剪 pdf 页面,

您需要为源文件定义 PdfReader,为目标文件定义文档
然后,如果读者在文档中创建一个新页面,则迭代页面
并将 sourcePage 作为模板添加到新页面(随心所欲地移动、缩放等)

    PdfReader reader = new PdfReader( input );
    int n = reader.getNumberOfPages();

    Rectangle psize = reader.getPageSize(1);
    float width = psize.getHeight();
    float height = psize.getWidth();

    Document document = new Document(new Rectangle(height, width));
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream( output ));

    document.open();

    PdfContentByte cb = writer.getDirectContent();

    int i = 0;
    while (i < n) {
        i++;
        document.newPage();
        PdfImportedPage page = writer.getImportedPage(reader, i);
        cb.addTemplate(page, factor, 0, 0, factor, left, down);
    }

    document.close();

you can use iText to move, scale or crop pdf-pages

you need to define a PdfReader for your source file, and a Document for your Target file
then you iterate over the pages if the Reader, create a new page in the Document
and add the sourcePage as a Template to the new page (shifting, scaling etc wherever you want)

    PdfReader reader = new PdfReader( input );
    int n = reader.getNumberOfPages();

    Rectangle psize = reader.getPageSize(1);
    float width = psize.getHeight();
    float height = psize.getWidth();

    Document document = new Document(new Rectangle(height, width));
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream( output ));

    document.open();

    PdfContentByte cb = writer.getDirectContent();

    int i = 0;
    while (i < n) {
        i++;
        document.newPage();
        PdfImportedPage page = writer.getImportedPage(reader, i);
        cb.addTemplate(page, factor, 0, 0, factor, left, down);
    }

    document.close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文