pdfbox 2.0:如何将大页面大小扩展到字母大小?

hof1towb  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(494)

我使用pdfbox 2.0.21(物理)打印pdf文件,可能有混合页面大小,包括非标准大小,如8.7“x11.3”,这将打印在合法大小或更大的表没有缩放。pdf由许多不同的员工以不同的方式创建(acrobat dc,从word保存,打印到pdf,由我们的文档系统生成…),并发送给我的团队打印和邮寄。该卷不允许我们单独检查它们,最终我们将应用程序指向一个文件夹,它将在那里打印所有内容。我更喜欢创建一个printerjob或pdfpageable,可以根据需要自动缩放所有页面,但无法使其工作。

public class PDPrn {
  public static void main(String[] args) {
    try (PDDocument pdf = PDDocument.load(new File("foo.pdf"))) {
      PrinterJob job = PrinterJob.getPrinterJob();
      Paper paper = new Paper();
      Paper.setSize(612, 792); // letter size
      paper.setImageableArea(36, 36, paper.getWidth() - 72, paper.getHeight() - 72); // 0.5in margins
      PageFormat format = new PageFormat();
      format.setPaper(paper);
      PDFPageable pageable = new PDFPageable(pdf);

      //doesn't scale down pages
      //printer will ask for legal and cutoff bottom when forced to use letter
      pageable.append(new PDFPrintable(pdf, Scaling.SCALE_TO_FIT), format);

      job.setPageable(pageable);
      try {
        job.print();
      } catch (PrinterException pe) {
      }
      pdf.close();
    } catch (IOException ioe) {
    }
  }
}

失败的是,我尝试遍历每一页,并直接缩小任何比字母更大的东西,但这移动的内容在页面上以令人费解的方式。

public class PDPrn {
  public static void main(String[] args) {
    try (PDDocument pdf = PDDocument.load(new File("foo.pdf"))) {
      PrinterJob job = PrinterJob.getPrinterJob();
      PDFPageable pageable = new PDFPageable(pdf);
      PDPageTree tree = pdf.getDocumentCatalog().getPages();
      Iterator<PDPage> iterator = tree.iterator();
      while (iterator.hasNext()) {
        PDPage page = iterator.next();
        if (page.getMediaBox().getWidth() !=612 || page.getMediaBox().getHeight() != 792) {
          PDPageContentStream contentStream = new PDPageContentStream(pdf, page,
            PDPageContentStream.AppendMode.PREPEND, false);

          //these are the wrong scaling factors but they do shrink oversized pages
          //however the shrunk content is moved far down the page and runs off the bottom
          //printer still asks for legal and cuts off bottom when forced to use letter
          //edit: these floats should probably be smaller
          //to account for margins, but the result is still
          //offset significantly below the upper left
          contentStream.transform(Matrix.getScaleInstance(612f / page.getMediaBox().getWidth(),
            792f / page.getMediaBox().getHeight()));

          //round figures, a large positive Y moves the content upward
          //but not cleanly to the upper left
          //whereas a large negative Y moves the content down and cuts off the bottom
          //but does print on letter without the printer complaining about size
          contentStream.transform(Matrix.getTransformInstance(0f, 250f);
          contentStream.close();
        }
      }
      job.setPageable(pageable);
      try {
        job.print();
      } catch (PrinterException pe) {
      }
      pdf.close();
    } catch (IOException ioe) {
    }
  }
}

将属性集与medianame、mediasizename和mediatray常量的各种组合一起使用也不会导致减少,打印机会停止并要求使用合法纸张,并在强制打印时切断底部。
如何使用pdfbox 2.0在打印时正确地将单个超大页面缩小到字母大小?
编辑:通过将页面的mediabox设置为pdrectangle.letter,我可以确保打印机不会要求合法纸张,并且左下角的内容流的来源是可见的。现在只需要使用正确的数学来得到缩放因子,也许影响边距,用imageablearea还是cropbox?

public class PDPrn {
  public static void main(String[] args) {
    try (PDDocument pdf = PDDocument.load(new File("foo.pdf"))) {
      PrinterJob job = PrinterJob.getPrinterJob();
      PDFPageable pageable = new PDFPageable(pdf);
      PDPageTree tree = pdf.getDocumentCatalog().getPages();
      Iterator<PDPage> iterator = tree.iterator();
      while (iterator.hasNext()) {
        PDPage page = iterator.next();
        if (page.getMediaBox().getWidth() !=612 || page.getMediaBox().getHeight() != 792) {
          PDPageContentStream contentStream = new PDPageContentStream(pdf, page,
            PDPageContentStream.AppendMode.PREPEND, false);

          //these are the wrong scaling factors but they do shrink oversized pages
          contentStream.transform(Matrix.getScaleInstance(0.5f,0.5f));
          page.setMediaBox(PDRectangle.LETTER); // make the page letter size with shrunk content origin in lower left
          contentStream.close();
        }
      }
      job.setPageable(pageable);
      try {
        job.print();
      } catch (PrinterException pe) {
      }
      pdf.close();
    } catch (IOException ioe) {
    }
  }
}
acruukt9

acruukt91#

设置新的mediabox大小确实足以将缩放后的内容流推送到更小的页面中。通过比较源宽度/高度和目标宽度/高度的红利,我可以找到一个适用于两侧的合适比例因子,保持纵横比。页面本身产生的边距对于我们的使用来说是可以接受的,所以我现在不担心精细控制,只需将纸张的边距设置为0就可以了。在pdfprintable构造器中收缩以适应似乎什么都没有做,我不确定它在什么情况下有效果。

public class PDPrn {
  public static void main(String[] args) {
    try (PDDocument pdf = PDDocument.load(new File("foo.pdf"))) {
      PrinterJob job = PrinterJob.getPrinterJob();
      PDPageTree tree = pdf.getDocumentCatalog().getPages();
      Iterator<PDPage> iterator = tree.iterator();
      while (iterator.hasNext()) {
        PDPage page = iterator.next();
        if (page.getMediaBox().getWidth() > 612 || page.getMediaBox().getHeight() > 792) {
          float fWidth = 612f / page.getMediaBox().getWidth();
          float fHeight = 792f / page.getMediaBox().getHeight();
          float factor = 0f;
          if (fWidth > fHeight) {
            factor = fHeight;
          } else {
            factor = fWidth;
          }
          PDPageContentStream contentStream = new PDPageContentStream(pdf, page,
            PDPageContentStream.AppendMode.PREPEND, false);
          contentStream.transform(Matrix.getScaleInstance(factor, factor));
          contentStream.close();
          page.setMediaBox(PDRectangle.LETTER);
        }
      }
      Paper paper = new Paper();
      paper.setSize(612, 792);
      paper.setImageableArea(0, 0, 612, 792);
      PageFormat pageFormat = new PageFormat();
      pageFormat.setPaper(paper);
      Book book = new Book();
      book.append(new PDFPrintable(pdf, Scaling.SHRINK_TO_FIT), pageFormat, pdf.getNumberOfPages());
      job.setPageable(book);
      try {
        job.print();
      } catch (PrinterException pe) {
      }
      pdf.close();
    } catch (IOException ioe) {
    }
  }
}

相关问题