简体   繁体   English

如何在java中打印缓冲图像列表

[英]how to print list of buffered image in java

Is there any way to print list of(multiple) buffered image in a single printjob ? 有什么方法可以在单个printjob中打印(多个)缓冲图像列表吗? Thanks in advance. 提前致谢。

In case of printing several graphics images, one per page, use the page index to iterate through these pages and print one on each page. 如果要打印几张图形图像(每页一张),请使用页面索引来遍历这些页面并在每页上打印一张。 For example, if several images are represented in the following array: 例如,如果在以下数组中表示多个图像:

BufferedImage[] images = new BufferedImage[10];

then use the print() method as shown in the following code fragment: 然后使用print()方法,如以下代码片段所示:

public int print(Graphics graphics,
           PageFormat pageFormat, int pageIndex)
           throws PrinterException {

    if (pageIndex < images.length) {
        graphics.drawImage(images[pageIndex], 100, 100, null);
        return PAGE_EXISTS;
    } else {
        return NO_SUCH_PAGE:
    }
}

If the document is continuous, the application must calculate how much content can fit on each page, and break the page at that point. 如果文档是连续的,则应用程序必须计算每个页面上可以容纳多少内容,然后在该位置分页。 If text document consists of many lines, then an application must calculate how many of these lines can fit entirely on a page. 如果文本文档由多行组成,则应用程序必须计算出其中的几行可以完全适合页面。 The Point class creates a point representing a location in (x,y) Point类创建一个表示(x,y)中位置的点

To calculate the height of a single line of text, use the FontMetrics class. 要计算单行文本的高度,请使用FontMetrics类。

Font font = new Font("Serif", Font.PLAIN, 10);
FontMetrics metrics = graphics.getFontMetrics(font);
int lineHeight = metrics.getHeight();

The PageFormat parameter describes the printable area of the page. PageFormat参数描述页面的可打印区域。 In particular, to find the vertical span of the page use the following code fragment: 特别是,要查找页面的垂直跨度,请使用以下代码片段:

double pageHeight = pageFormat.getImageableHeight();

Use the following code fragment to calculate the number of lines that fit on a page and the number of page breaks: 使用以下代码片段计算适合页面的行数和分页符数:

int linesPerPage = ((int)pageHeight)/lineHeight);
int numBreaks = (textLines.length-1)/linesPerPage;
int[] pageBreaks = new int[numBreaks];
for (int b=0; b < numBreaks; b++) {
    pageBreaks[b] = (b+1)*linesPerPage; 
}

Use the print() method to calculate the printable area for the following reasons: 出于以下原因,使用print()方法计算可打印区域:

Text measurement depends on the FontRenderContext and this is implicit in the FontMetrics object returned by the printer graphics which is not available except inside the print() method. 文本度量取决于FontRenderContext,并且隐含在打印机图形返回的FontMetrics对象中,该对象除了print()方法内部不可用。 The page format may not be disclosured until printing occurs. 在进行打印之前,可能不会公开页面格式。 Since if the user selected a landscape mode in the print dialog, then this setting needs to be accounted for. 由于如果用户在打印对话框中选择了横向模式,则需要考虑此设置。 The PageFormat object passed into the print() method provides this information. 传递给print()方法的PageFormat对象提供了此信息。 The page break positions are used as represented in the following code fragment: 分页符的位置如以下代码片段所示:

/* Draw each line that is on this page.
 * Increment 'y' position by lineHeight
 * for each line.
 */
int y = 0; 
int start = (pageIndex == 0) ? 0 : pageBreaks[pageIndex-1];
int end   = (pageIndex == pageBreaks.length)
                 ? textLines.length : pageBreaks[pageIndex];
for (int line=start; line<end; line++) {
    y += lineHeight;
    g.drawString(textLines[line], 0, y);
}

If a document contains 100 lines and only 48 lines fit on a page, then an application prints 3 pages with page breaks after 48 and 96 lines of text. 如果文档包含100行,而一页上只能容纳48行,则应用程序将打印3页,并在48行和96行文本之后显示分页符。 The remaining 4 lines are printed on the last page. 剩余的4行打印在最后一页上。 The complete code for this example is in PaginationExample.java. 此示例的完整代码在PaginationExample.java中。

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

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