将A4页面分割成A7部分

问题描述 投票:0回答:2

我收到一份 A4 页的文档,每页有 8 个 A7 部分。 我需要从每个页面的每个 A7 区域中提取数据,因为它们是相关的。

是否可以将8个A7中的每一个A4拆开,并遍历数据?

这是我正在处理的 PDF 文件:https://s3.us-east-2.amazonaws.com/s3.barcodegen-website.io/programada+pdf+teste.pdf

(有关 A4/A7 纸张尺寸,请参阅维基百科上的ISO 216。)

java pdf pdfbox
2个回答
1
投票

因为我不知道“从每个页面的每个 A7 区域中提取数据,因为它们是相关的”的真正含义是什么,所以我首先提出一个解决方案,(至少)在视觉上将单个 A$ pdf 页面拆分为八个 A7,遵循以下配置:

+----+----+
| 4  | 8  |
+----+----+
| 3  | 7  |
+----+----+
| 2  | 6  |
+----+----+
| 1  | 5  |
+----+----+

基本上,它使用移动窗口裁剪当前页面并将其导入到新文档的页面中,然后旋转每个新页面。

public void splitPdf(String pdfFileName) throws IOException {
  File pdfFile = new File(pdfFileName);
  File pdfTargetFile = new File(pdfFileName + ".a7.pdf");

  try (PDDocument pdfDocument = Loader.loadPDF(new RandomAccessReadBufferedFile(pdfFile));
      PDDocument pdfTargetDocument = new PDDocument(); ) {

    for (PDPage pdfPage : pdfDocument.getPages()) {
      PDRectangle cropBox = pdfPage.getCropBox();
      float upperRightX = cropBox.getUpperRightX();
      float upperRightY = cropBox.getUpperRightY();
      for (int j = 0; j < 2; ++j) {
        for (int i = 0; i < 4; ++i) {
          float cropLowerLeftX = upperRightX / 2 * j;
          float cropUpperRightX = upperRightX / 2 * (j + 1);
          float cropLowerLeftY = upperRightY / 4 * i;
          float cropUpperRightY = upperRightY / 4 * (i + 1);
          cropBox.setLowerLeftX(cropLowerLeftX);
          cropBox.setUpperRightX(cropUpperRightX);
          cropBox.setLowerLeftY(cropLowerLeftY);
          cropBox.setUpperRightY(cropUpperRightY);
          pdfPage.setCropBox(cropBox);
          pdfTargetDocument.importPage(pdfPage);
        }
      }
    }

    for (PDPage pdfTargetPage : pdfTargetDocument.getPages()) {
      pdfTargetPage.setRotation(90);
    }
    pdfTargetDocument.save(pdfTargetFile);
  }
}

无论如何,这并不是真正的“拆分”,因为实际上新文档的每一页都包含了原始文档的所有数据,但它只是“超出页面边界”,正因为如此,我将其称为“视觉” “分裂。


0
投票

拆分 PDF 页面会引发许多次要问题,例如您将如何处理半个“字形”(或半个超链接),因此内部超链接通常会被丢弃,但外部超链接可能需要保留。

我们需要测试资源的重复情况,因此 526 KB(539,607 字节)的源 A4 实际上可能会与 537 KB(550,093 字节)略有不同,后者有时会小得多,但这里只是稍微大一点!

enter image description here

使用图像方法是不可接受的,因为在这种规模下条形码很可能会被破坏。

左图(注意填充不良),右图扫描准确。

enter image description here

裁剪重复并不总是一个好的解决方案,因为每页可能有重叠的内容。然而,在这种情况下,可以通过抽取将其分解为 4 x 2 页面,见此处成对的页面。我们还可能在那个阶段看到偏移量有所不同,并且对于这种分割来说并不完美。因此,源位置要么需要改变,要么页面边界向不同方向滑动。

enter image description here

在 Acrobat Reader 等中看到的更正结果

mutool poster -x 4 -y 2 -r programada.pdf output.pdf

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.