当前位置: 首页 > 知识库问答 >
问题:

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

郜玉石
2023-03-14

我正在使用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设置为PDRectange。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) {
    }
  }
}

共有1个答案

胡元明
2023-03-14

设置新的MediaBox大小确实足以将缩放的内容流推送到较小的页面内。比较源宽度/高度和目标宽度/高度的红利可以让我找到一个合适的缩放因子来应用于双方,保持长宽比。页面本身产生的边距看起来可以接受我们的使用,所以我现在不担心精细控制,只需将纸张设置为0边距并让它走。PDFPrintable构造函数中的SHRINK_TO_FIT似乎没有做任何事情,我不确定它在什么情况下会产生影响。

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) {
    }
  }
}
 类似资料:
  • 问题内容: 我读过很多文章说,在Java 8中可以将DH密钥的大小扩展到2048。他们说可以通过将system的值更改 为2048来实现。我试图弄清楚在哪里以及如何这样做并找不到。在哪里可以找到此变量或属性?哪个文件?路径?我正在使用Windows。 问题答案: 这是一个系统属性,因此您可以通过将其设置为JVM参数或在代码内进行设置。(我没有检查该属性,但是某些属性仅被使用它们的库读取一次,然后在

  • 我试图使帧动画保持在图像视图的中心,在下载图像之前,动画正在其中播放。基本上,这是一个加载微调器的动画。这是我在xml文件中的imageview。 这是我的框架xml文件 这是我用来启动动画的代码 问题是动画拉伸了Imageview的整个宽度和高度。Imageview设置为fitCenter。我如何避免这种拉伸?

  • 有人知道如何改变页面大小(从字母到A4)为(docx)吗? 我找不到任何像XSSFDocument(xlsx)中那样的打印设置。 谢谢你的帮助。

  • 我在创建.pdf文件时遇到问题,页面设置为信封,横向格式。 这是我的代码,用于将asp页面转换为Itext Sharp中的pdf。 我谷歌了一下,但找不到信封size.How我要动态设置页面大小为信封、风景。 提前致谢

  • 问题内容: 我正在使用Ubuntu 11.04。如何找出进程的最大调用堆栈大小以及堆栈中每个帧的大小? 问题答案: 您可以使用查询最大进程和堆栈大小。堆栈框架没有固定的尺寸。它取决于每个帧需要多少本地数据(即本地变量)。 要在命令行上执行此操作,可以使用ulimit。 如果要为正在运行的进程读取这些值,我不知道执行此操作的任何工具,但是查询/ proc文件系统很容易:

  • 扩展说明 对等网络节点组网器。 扩展接口 org.apache.dubbo.container.page.PageHandler 扩展配置 <dubbo:protocol page="xxx,yyy" /> <!-- 缺省值设置,当<dubbo:protocol>没有配置page属性时,使用此配置 --> <dubbo:provider page="xxx,yyy" /> 已知扩展 org.ap