简体   繁体   English

为什么使用相同的页码多次调用java Printable的print方法?

[英]Why does the java Printable's print method get called multiple times with the same page number?

From sun's documentation 来自sun的文档

"The printing system might request that a page be rendered multiple times before moving to the next page." “在移动到下一页之前,打印系统可能会要求多次渲染页面。”

The examples always show something like this: 示例总是显示如下:

Printable print(Graphics g, PageFormat pageFormat, int page) {
    if (page == 0)
      do...
    else if(page == blah...)
}

If you follow this pattern your code typically works fine because it is explicit based on the page number. 如果您遵循此模式,您的代码通常可以正常工作,因为它是基于页码显式的。 Not following this pattern caused me great pain until I realized it was getting called multiple times with the same page number and started to cache the pages. 不遵循这种模式给我带来了巨大的痛苦,直到我意识到它被多次调用相同的页码并开始缓存页面。

Why does the java Printable's print method get called multiple times with the same page number? 为什么使用相同的页码多次调用java Printable的print方法?

The Java printing system is at the mercy of the underlying OS printing system, and that system may request a single page be rendered multiple times. Java打印系统受底层OS打印系统的支配,并且该系统可以请求多次呈现单个页面。

One reason is banded printing -- if the printer doesn't have enough memory to render the entire page at once -- in that case, the OS will ask Java for the page again so it can print the page in strips ("bands"). 一个原因是带状打印 - 如果打印机没有足够的内存来一次呈现整个页面 - 在这种情况下,操作系统将再次向Java请求页面,以便它可以打印带状页面(“带”) )。 This is the specific case mentioned in the Java 2D Programmer's Guide, in the section " Printing Concepts ". 这是Java 2D程序员指南中“ 打印概念 ”一节中提到的特定情况。

There may be other reasons; 可能还有其他原因; it's really up to the OS's printing system. 它真的取决于操作系统的打印系统。

There are a number of reasons it may be doing this. 它可能有很多原因。

Depending on the underlying print system it may want to compute certain properties "up front" (eg: page extents, ink usage, etc.) without having to buffer the entire document. 根据底层打印系统,它可能希望“预先”计算某些属性(例如:页面范围,墨水使用等),而不必缓冲整个文档。

Also, certain printing systems are "band based", rather than page-based. 而且,某些打印系统是“基于带”的,而不是基于页面的。 Inkjets, for example, will print out one a horizontal band of raster data at a time. 例如,喷墨打印机将一次打印出一个水平条带的栅格数据。 Rather than buffering a page's worth of raster data (about 100MB for a 600dpi US letter page) the Java printing system may buffer only a few bands (or possibly even just one band) at a time. Java打印系统可以一次仅缓冲几个频段(或者甚至可能只缓冲一个频段),而不是缓冲页面的光栅数据(600dpi美国字母页面大约100MB)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM