如何在多个页面中打印大型JPanel

时间:2014-09-18 17:40:49

标签: java printing jpanel

我想打印一个非常大的面板,这个面板包含一些组件,如jtable,jlabel和其他jpanel。现在我想在不同的页面中打印它。但我不知道该怎么做。我在面板类中实现了Printable。但是,如果我打印它,我只得到一页。

2 个答案:

答案 0 :(得分:1)

试试这个?

package com.mymoney.util;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Pageable;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

import javax.swing.RepaintManager;

public class PrintUtil implements Printable, Pageable {
   private Component componentToBePrinted;
   private PageFormat format;
   private int numPages;

   public PrintUtil(Component componentToBePrinted) {
      this.componentToBePrinted = componentToBePrinted;
      Dimension page = this.componentToBePrinted.getPreferredSize();
      numPages = (int) Math.ceil(page.height/format.getImageableY());
   }

   public void print() {
      PrinterJob printJob = PrinterJob.getPrinterJob();
      printJob.setPrintable(this);
      printJob.setPageable(this);
      format = printJob.defaultPage();

   if (printJob.printDialog())
      try {
        printJob.print();
      } catch(PrinterException pe) {
        System.out.println("Error printing: " + pe);
      }
   }

 public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
   if ((pageIndex < 0) | (pageIndex >= numPages)) {
      return(NO_SUCH_PAGE);
   } else {
     Graphics2D g2d = (Graphics2D)g;
     g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY()- pageIndex*componentToBePrinted.getPreferredSize().height);
     disableDoubleBuffering(componentToBePrinted);
     componentToBePrinted.paint(g2d);
     enableDoubleBuffering(componentToBePrinted);
     return(PAGE_EXISTS);
  }
}

public static void disableDoubleBuffering(Component c) {
   RepaintManager currentManager = RepaintManager.currentManager(c);
   currentManager.setDoubleBufferingEnabled(false);
}

   public static void enableDoubleBuffering(Component c) {
       RepaintManager currentManager = RepaintManager.currentManager(c);
       currentManager.setDoubleBufferingEnabled(true);
   }

   @Override
   public int getNumberOfPages() {
      // TODO Auto-generated method stub
      return numPages;
   }

 @Override
 public PageFormat getPageFormat(int arg0) throws IndexOutOfBoundsException {
      return format;
 }

 @Override
    public Printable getPrintable(int arg0) throws IndexOutOfBoundsException {
      // TODO Auto-generated method stub
      return this;
    }
}

参考 - &gt; https://community.oracle.com/thread/1263759?start=0&tstart=0

答案 1 :(得分:1)

我对Harry中的问题的修改未被接受,因此我将我的修改发布为新答案。

以下代码适合我(我测试过):

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.*;

import javax.swing.RepaintManager;

public class PrintMultiPageUtil implements Printable, Pageable {
    private Component componentToBePrinted;
    private PageFormat format;
    private int numPages;

    public PrintMultiPageUtil(Component componentToBePrinted) {
        this.componentToBePrinted = componentToBePrinted;

        // get total space from component  
        Dimension totalSpace = this.componentToBePrinted.getPreferredSize();

        // calculate for DIN A4
        format = PrinterJob.getPrinterJob().defaultPage();
        numPages = (int) Math.ceil(totalSpace .height/format.getImageableHeight());
    }

    public void print() {
        PrinterJob printJob = PrinterJob.getPrinterJob();

        // show page-dialog with default DIN A4
        format = printJob.pageDialog(printJob.defaultPage());

        printJob.setPrintable(this);
        printJob.setPageable(this);

        if (printJob.printDialog())
            try {
                printJob.print();
            } catch(PrinterException pe) {
                System.out.println("Error printing: " + pe);
            }
    }

    public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
        if ((pageIndex < 0) | (pageIndex >= numPages)) {
            return(NO_SUCH_PAGE);
        } else {
            Graphics2D g2d = (Graphics2D)g;
            g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY() - pageIndex * pageFormat.getImageableHeight());
            disableDoubleBuffering(componentToBePrinted);
            componentToBePrinted.paint(g2d);
            enableDoubleBuffering(componentToBePrinted);
            return(PAGE_EXISTS);
        }
    }

    public static void disableDoubleBuffering(Component c) {
        RepaintManager currentManager = RepaintManager.currentManager(c);
        currentManager.setDoubleBufferingEnabled(false);
    }

    public static void enableDoubleBuffering(Component c) {
        RepaintManager currentManager = RepaintManager.currentManager(c);
        currentManager.setDoubleBufferingEnabled(true);
    }

    @Override
    public int getNumberOfPages() {
        // TODO Auto-generated method stub
        return numPages;
    }

    @Override
    public PageFormat getPageFormat(int arg0) throws IndexOutOfBoundsException {
        return format;
    }

    @Override
    public Printable getPrintable(int arg0) throws IndexOutOfBoundsException {
        // TODO Auto-generated method stub
        return this;
    }
}

<强> NUMPAGES

我将numPages的表达式更改为:

(int) Math.ceil(page.height/format.getImageableHeight())

这将总高度(jpanel的高度)除以一页的高度,从而计算所有页面的数量。

<强> g2d.translate

我做了以下更改:在这一行:

g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY() - pageIndex * pageFormat.getImageableHeight());

componentToBePrinted.getPreferredSize().height更改为pageFormat.getImageableHeight()g2d.translate的第一个或第二个参数的正值分别将图形向右或向下移动。

.getImageableX().getImageableY()有助于定位图形,使其与填充不重叠。

对于大于pageIndex的{​​{1}},0会将图片- pageIndex * pageFormat.getImageableHeight() - 将页面高度移动到顶部。因此打印pageIndex所指的区域。

原始破解来源:

https://community.oracle.com