Popular Posts
android.intent.action.SCREEN_ON & android.intent.action.SCREEN_OFF First, I've tried create a receiver to receive screen on/off and register receiver on AndroidManifest.xml like below, but unfortunately ... Multiple line of text limit With Sharepoint Designer, edit the page of list view. Add xsl template as below to override original template. Source template could be foun... Date format memo Available Picture Formatting Codes Specifier Description Examples ...
Blog Archive
Stats
Print multiple image
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

public class PringMultipleImage implements Printable {

    @Override
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
        // 要列印的圖片
        BufferedImage[] images = this.getPrintImages();
        if (images.length == 0)
            return Printable.NO_SUCH_PAGE;

        // 固定寬度(以符合掃描器大小)
        double stickerWidth = 220;
        double maxStickerHeight = 0; // 最大高度
        for (int i = 0; i < images.length; i++) {
            maxStickerHeight = Math.max(maxStickerHeight, stickerWidth / images[i].getWidth() * images[i].getHeight());
        }
        // 計算頁面可列印張數, 每頁張數 : c*r
        int c = (int) (pageFormat.getImageableWidth() / stickerWidth);
        int r = (int) (pageFormat.getImageableHeight() / maxStickerHeight);
        int p = (int) Math.ceil(1.0 * images.length / c / r); // 頁數

        if (pageIndex < p) {
            // 計算cell大小
            double cellWidth = pageFormat.getImageableWidth() / c;
            double cellHeight = pageFormat.getImageableHeight() / r;
            // 計算位置
            double translateX = (cellWidth - stickerWidth) / 2;

            // 
            int cellx = 0;
            int celly = 0;

            for (int i = 0 + pageIndex * c * r; i < images.length; i++) {
                double stickerHeight = stickerWidth / images[i].getWidth() * images[i].getHeight();
                double translateY = (cellHeight - stickerHeight) / 2;

                graphics.drawImage(images[i], //
                        (int) (translateX + cellx * cellWidth), //
                        (int) (translateY + celly * cellHeight), //
                        (int) stickerWidth, //
                        (int) stickerHeight, //
                        null);

                // 下一個cell
                if (cellx + 1 == c) {
                    cellx = 0;
                    celly++;
                } else {
                    cellx++;
                }
            }
            return Printable.PAGE_EXISTS;
        } else {
            return Printable.NO_SUCH_PAGE;
        }
    }

    private BufferedImage[] getPrintImages() {
        // Generate image here
        return null;
    }

    public static void main(String[] args) {
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable(new PringMultipleImage());
        if (job.printDialog()) {
            try {
                job.print();
            } catch (PrinterException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
reference:
Lesson: Printing (The Java™ Tutorials > 2D Graphics)
Java Print Service API User Guide