简体   繁体   中英

Printing a continuous BufferedImage

I am trying to print a continuous buffered image onto seperate pages. The problem I have is that the best I can do is print the last page. The count on the variable inty seems to be ignored until it reaches the end. Here is my code

public class PrintDocs implements Printable {
    BufferedImage incomingImage;
    int inty;

    public PrintDocs(BufferedImage incomingImage) {
        this.incomingImage = incomingImage;
        inty = 0;

    }

    public void PrintImage() {
        HashPrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();
        attr.add(new MediaPrintableArea(0f, 0f, incomingImage.getWidth() / 72f, incomingImage.getHeight() / 72f,
                MediaPrintableArea.INCH));

        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable(this);
        PageFormat pf = job.pageDialog(job.defaultPage());
        boolean ok = job.printDialog();

        if (ok) {

            try {
                job.print(attr);
                inty = inty + 842;

            } catch (PrinterException ex) {
                /* The job did not successfully complete */
            }

        }

    }

    public int print(Graphics g, PageFormat pf, int page) throws PrinterException {

        // if (page > 0){ //Use this for the moment may get rid of it
        // return NO_SUCH_PAGE;
        // }

        /*
         * User (0,0 is typacally outside the imageable area, so we must
         * translate by the X and Y values in the PageFormatz to avoid clipping
         */

        if (inty < incomingImage.getHeight()) {
            // if (inty < incomingImage.getHeight()){
            Graphics2D g2d = (Graphics2D) g;

            g2d.translate(pf.getImageableX() - 1.0, pf.getImageableY() - 1.0);

            /* Now we perform out rendering */

            g.drawImage(incomingImage, 0, 0, 595, 842, 0, inty, 595, 842 + inty, null);

            // g2d.drawImage(incomingImage, 0, 0, 595, 842, 0, inty, 595,842 +
            // inty, null);
            System.out.println("inty = " + inty);
            inty = inty + 842;
            return PAGE_EXISTS;
        }
        return NO_SUCH_PAGE;
    }
}

Invoking printImage():

private void printActionPerformed(java.awt.event.ActionEvent evt){ 
    // TODO add your handling code here:
    //BufferedImage printCopy = tooOutput.displayProcessing.getCreatedImage();
    BufferedImage printCopy = tooOutput.getPrintImage();
    PrintDocs sendToPrinter = new PrintDocs(printCopy);
    sendToPrinter.PrintImage();
}

Your variable inty seems to reset to because you always create a new instance of PrintDocs everytime your actionPerformed method is invoked.

PrintDocs sendToPrinter = new PrintDocs(printCopy); //This line causing the reset of inty
sendToPrinter.PrintImage();

To prevent that, you can keep a reference of the same PrintDocs instance:

private void printActionPerformed(java.awt.event.ActionEvent evt){ 
    BufferedImage printCopy = tooOutput.getPrintImage();
    sendToPrinter.PrintImage();  //Keep a reference of sendToPrinter in your class
}

An alternative solution will be making intY a staic variable and printImage() a static method. When you need to print , just invoke via the class name:

PrintDos.printImage();

And you class will look like:

public class PrintDocs implements Printable {
    static int inty = 0;    //<= become static 

    //other variables, methods and constructor not shown

    public static void PrintImage() {    //<= become static
        //implementation for printImage
    }
}

The problem is that inty is incremented each time the print method is called internally. That works iff the print method is called exactly once for each page.

From the JavaDoc of the java.awt.print.Printable ( https://docs.oracle.com/javase/7/docs/api/java/awt/print/Printable.html ):

The printing system may request a page index more than once. On each occasion equal PageFormat parameters will be supplied.

Instead of counting the current page number itself, your print method should use a page parameter to determine which part of the image to print.

public class PrintDocs implements Printable {
    private static final int pageHeight = 842;
    private BufferedImage incomingImage;
    private int pagePrintCount = 0;

    public PrintDocs(BufferedImage incomingImage) {
        this.incomingImage = incomingImage;
    }

    public void PrintImage() {
        HashPrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();
        attr.add(new MediaPrintableArea(0f, 0f, incomingImage.getWidth() / 72f, incomingImage.getHeight() / 72f,
                MediaPrintableArea.INCH));

        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable(this);
        PageFormat pf = job.pageDialog(job.defaultPage());
        boolean ok = job.printDialog();

        if (ok) {

            try {
                job.print(attr);
            } catch (PrinterException ex) {
                System.out.println("The job did not successfully complete");
                ex.printStackTrace();
            }

        }

    }

    public int print(Graphics g, PageFormat pf, int page) throws PrinterException {

        System.out.println("print called for page="+page+", call count="+pagePrintCount++);

        /*
         * User (0,0 is typacally outside the imageable area, so we must
         * translate by the X and Y values in the PageFormatz to avoid clipping
         */

        if (page*pageHeight < incomingImage.getHeight()) {
            Graphics2D g2d = (Graphics2D) g;

            g2d.translate(pf.getImageableX() - 1.0, pf.getImageableY() - 1.0);

            /* Now we perform out rendering */

            g.drawImage(incomingImage, 0, 0, 595, pageHeight, 0, page*pageHeight, 595, pageHeight*(page+1), null);

            return PAGE_EXISTS;
        }
        return NO_SUCH_PAGE;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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