2012-09-30 76 views
0

我想用兩個表打印jpanel。我面臨的兩個問題,第一個是打印圖像的準確性不是很好。第二我如何控制打印頁面上jpanel的大小?控制打印頁面的面板大小

這裏是使用XPS查看器打印頁面的圖像(精度不高) enter image description here

是有沒有去使打印的圖像具有這樣的精度enter image description here這樣

,這是代碼:

 PrinterJob printjob = PrinterJob.getPrinterJob(); 
    printjob.setJobName(" TESSCO CUSTOMER CARD "); 

    Printable printable = new Printable() { 

      public int print(Graphics pg, PageFormat pf, int pageNum) { 

        if (pageNum > 0) { 
          return Printable.NO_SUCH_PAGE; 
        } 

        Dimension size = jPanel1.getSize(); 
        BufferedImage bufferedImage = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB); 

        jPanel1.print(bufferedImage.getGraphics()); 

        Graphics2D g2 = (Graphics2D) pg; 
        g2.translate(pf.getImageableX(), pf.getImageableY()); 
        g2.drawImage(bufferedImage, 0, 0, (int) pf.getWidth(), (int) pf.getHeight(), null); 

        return Printable.PAGE_EXISTS; 
      } 
    }; 

    Paper paper = new Paper(); 
    paper.setImageableArea(0, 0,700,890); 
    paper.setSize(700,890); 

    PageFormat format = new PageFormat(); 
    format.setPaper(paper); 
    format.setOrientation(PageFormat.LANDSCAPE); 

    printjob.setPrintable(printable, format); 
    if (printjob.printDialog() == false) 
      return; 

    try { 
      printjob.print(); 
    } catch (PrinterException ex) { 
      System.out.println("NO PAGE FOUND." + ex); 

    } 
+0

@kleopatra你是什麼意思通過重新發明輪子? –

+0

oops ...沒有讀你的第一句話;-) – kleopatra

+0

我的潛意識自我可能被認爲是沿着http://stackoverflow.com/a/6343475/203657的行 - 這是實現委託打印,調整紙張設置(沒有嘗試,但預計會使它適用於兩個表格) – kleopatra

回答

1

首先,不要使用窗格大小,您需要充當佈局管理器並調整面板大小以適合頁面。

其次,不要使用緩衝圖像。這將不會與打印引擎過去給您的圖形上下文共享相同的屬性。此外,另一種印刷方法重入的,這意味着它可以被稱爲多次,爲每個頁面,創建一個緩衝的形象這樣是對資源的浪費

你可能想看看How to Print Tables

UPDATE

你可以做這樣的事情......

public int print(Graphics pg, PageFormat pf, int pageNum) { 
    if (page > 0) { 
     return NO_SUCH_PAGE; 
    } 

    Graphics2D g2d = (Graphics2D)pg; 

    double pageWidth = pf.getImageableWidth(); 
    double pageHeight = pf.getImageableHeight(); 

    double pageX = pf.getImageableX(); 
    double pageY = pf.getImageableY(); 

    g2d.translate(pageX, pageY); 

    double tableHeight = pageHeight/2d; 

    jPanel1.setBounds(0, 0, (int)Math.floor(pageWidth), (int)Math.floor(pageHeight)); 
    jPanel1.printAll(g2d); 

    return Printable.PAGE_EXISTS; 

} 

只是要小心,這可能會對您的截斷表的潛力。此外,您不應該使用屏幕上已有的Component。您應該創建一個新的「打印」組件。

更新與工作示例

好了,所以是合理的,只是需要一些調整,以得到它的工作理念;)

enter image description here

public class PrintTableTest { 

    public static void main(String[] args) { 

     final JTable table1 = new JTable(new AbstractTableModel() { 
      @Override 
      public int getRowCount() { 
       return 3; 
      } 

      @Override 
      public int getColumnCount() { 
       return 3; 
      } 

      @Override 
      public String getColumnName(int column) { 
       String name = null; 
       switch (column) { 
        case 0: 
         name = "Day"; 
         break; 
        case 1: 
         name = "FirstName"; 
         break; 
        case 2: 
         name = "LastName"; 
         break; 
       } 
       return name; 
      } 

      @Override 
      public Object getValueAt(int rowIndex, int columnIndex) { 
       Object value = null; 
       switch (columnIndex) { 
        case 0: 
         switch (rowIndex) { 
          case 0: 
           value = "First"; 
           break; 
          case 1: 
           value = "Second"; 
           break; 
          case 2: 
           value = "Final"; 
           break; 
         } 
         break; 
       } 
       return value; 
      } 
     }); 
     int rowHeight = (int) Math.floor(((700f/2f) - table1.getTableHeader().getPreferredSize().height)/3f); 
     table1.setRowHeight(rowHeight); 

     PrinterJob printjob = PrinterJob.getPrinterJob(); 
     printjob.setJobName(" TESSCO CUSTOMER CARD "); 

     Printable printable; 
     printable = new Printable() { 
      public int print(Graphics pg, PageFormat pf, int pageNum) { 

       if (pageNum > 0) { 
        return NO_SUCH_PAGE; 
       } 

       Graphics2D g2d = (Graphics2D) pg; 

       double pageWidth = pf.getImageableWidth(); 
       double pageHeight = pf.getImageableHeight(); 

       double pageX = pf.getImageableX(); 
       double pageY = pf.getImageableY(); 

       g2d.translate(pageX, pageY); 

       // Each table will take half the page... 
       double tableHeight = pageHeight/2d; 

       // We need to print the header as well... 
       JTableHeader header = table1.getTableHeader(); 
       int headerHeight = header.getPreferredSize().height; 

       int yOffset = 0; 

       for (int index = 0; index < 2; index++) { 
        // Set the bounds of the components 
        // The yOffset is actuall irrelevent to us, but for consitency sake 
        // we'll keep it. 
        header.setBounds(0, yOffset, (int) Math.floor(pageWidth), headerHeight); 
        table1.setBounds(0, yOffset + headerHeight, (int) Math.floor(pageWidth), (int) Math.floor(tableHeight)); 
        // Force the components to update there internal layouts to match 
        // the new size. We need to do this because, technically, we're not 
        // attached to any peer, nor do we want them to be taking into account 
        // the dimensions of any parent any way :P 
        table1.doLayout(); 
        header.doLayout(); 

        // Translate the graphics. Components asume a position of 0x0 when 
        // painting. This is a side effect of the AWT/Swing painting engine 
        // (for which we are greatful), but we need to simulate the change 
        g2d.translate(0, yOffset); 
        header.printAll(g2d); 
        // Translations are relative to the last translation... 
        g2d.translate(0, headerHeight); 
        table1.printAll(g2d); 
        // Reset the last translation 
        g2d.translate(0, -(headerHeight + yOffset)); 

        // Next table... 
        yOffset += table1.getHeight(); 
       } 
       return Printable.PAGE_EXISTS; 
      } 
     }; 

     Paper paper = new Paper(); 
     paper.setImageableArea(0, 0, 700, 890); 
     paper.setSize(700, 890); 

     PageFormat format = new PageFormat(); 
     format.setPaper(paper); 
     format.setOrientation(PageFormat.LANDSCAPE); 

//  printjob.setPrintable(printable, format); 
     BufferedImage img = new BufferedImage(890, 700, BufferedImage.TYPE_INT_RGB); 
     Graphics2D g2d = img.createGraphics(); 
     g2d.setColor(Color.WHITE); 
     g2d.fill(new Rectangle(0, 0, 890, 700)); 
     try { 
      printable.print(g2d, format, 0); 
     } catch (Exception exp) { 
      exp.printStackTrace(); 
     } 
     g2d.dispose(); 

     try { 
      ImageIO.write(img, "png", new File("Print.png")); 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } 
    } 
} 
+0

感謝您的回覆,我想在同一頁上打印兩張表,因此我無法使用打印表格方法。好的,我怎樣才能解決第一個問題編程? –

+0

_Athens_?像在希臘:-) – kleopatra

+0

@MadProgrammer謝謝任何方式,但沒有奏效。 –