2013-05-07 46 views
0

我的應用程序中有一個非常簡單的打印功能,可以打印Jtextpane的內容。我想將默認頁面大小設置爲A4,但是在搜索後我發現很多涉及書籍和文檔formater等的方法,我希望儘可能簡單。打印JTextPane時的簡單頁面大小設置?

我的代碼目前是:

public void printy(){ 
    JTextPane jtp = new JTextPane(); 
    jtp.setBackground(Color.white); 
    try { 
      // open the file we have just decrypted 

       File myFile = new File(deletefile + "mx.txt"); 
       FileInputStream fIn = new FileInputStream(myFile); 
       BufferedReader myReader = new BufferedReader(
         new InputStreamReader(fIn)); 
       String aDataRow = ""; 
       String aBuffer = ""; 
       while ((aDataRow = myReader.readLine()) != null) { 
        aBuffer += aDataRow + "\n"; 
       } 

       String[] splitdata = aBuffer.split("`"); //recover the file and split it based on ` 
      String lines = ""; 
      for(String line : splitdata){ 
      lines = lines + line + System.getProperty("line.separator") + System.getProperty("line.separator"); 
      } 

       myReader.close(); 

       System.out.println(Arrays.toString(splitdata)); 
       System.out.println(lines); 

       jtp.setText(lines); 
       boolean show = true; 
       try { 
        //set the header and footer data here 
        MessageFormat headerFormat = new MessageFormat("HEADER HERE"); 
        MessageFormat footerFormat = new MessageFormat("FOOTER HERE"); 
        Paper A4 = new Paper(); 
        A4.setSize(595, 842); 
        A4.setImageableArea(43, 43, 509, 756); 


        jtp.print(headerFormat, footerFormat, show, null, null, show); 


       } catch (java.awt.print.PrinterException ex) { 
        ex.printStackTrace(); 
       } 
      } catch (Exception ez) { 
       System.out.println("error in array building"); 
      } 
} 
} 

我已經設置了A4紙大小,但不知道如何在對的JTextPane的屬性.PRINT設置。

感謝您的幫助;

安迪

+0

Can [this](http://stackoverflow.com/questions/13558152/how-can-i-print-a-custom-paper-size-cheques-8-x-4)有幫助嗎? – Mateusz 2013-05-07 21:00:59

+0

排序,但我很努力地看到如何在我的代碼中使用示例。我希望我可以保持大致相同的代碼(如其簡單),但用某種紙張大小替換空參數之一? – andy 2013-05-07 21:32:43

回答

1

實際上,在嘗試StanislavL提供的鏈接後,我在Oracle指南中發現了我認爲是解決問題的更好方法,我使用的代碼是;

public void printy(){ 
    JTextPane jtp = new JTextPane(); 
    jtp.setBackground(Color.white); 
    try { 
      // open the file we have just decrypted 

       File myFile = new File(deletefile + "mx.txt"); 
       FileInputStream fIn = new FileInputStream(myFile); 
       BufferedReader myReader = new BufferedReader(
         new InputStreamReader(fIn)); 
       String aDataRow = ""; 
       String aBuffer = ""; 
       while ((aDataRow = myReader.readLine()) != null) { 
        aBuffer += aDataRow + "\n"; 
       } 

       String[] splitdata = aBuffer.split("`"); //recover the file and split it based on ` 
      String lines = ""; 
      for(String line : splitdata){ 
      lines = lines + line + System.getProperty("line.separator") + System.getProperty("line.separator"); 
      } 

       myReader.close(); 

       System.out.println(Arrays.toString(splitdata)); 
       System.out.println(lines); 

       jtp.setText(lines); 
       boolean show = true; 
       try { 
        //set the header and footer data here 
        MessageFormat headerFormat = new MessageFormat("Your header here - {0}"); //sets the page number 
        MessageFormat footerFormat = new MessageFormat("Your footer here"); 

        PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet(); 
        attr_set.add(MediaSizeName.ISO_A4); 
        attr_set.add(Sides.DUPLEX); 

        jtp.print(headerFormat, footerFormat, show, null, attr_set, show); 


       } catch (java.awt.print.PrinterException ex) { 
        ex.printStackTrace(); 
       } 

      } catch (Exception ez) { 
       System.out.println("error in array building"); 
      } 

} 
} 

希望這可以幫助其他人,不是說其完美的,但它確實工作得很好,默認情況下增加了複式。

2

您可以使用的方法http://java-sl.com/JEditorPanePrinter.html

在那裏,你可以傳遞的PageFormat你需要,你可以指定所需的紙張尺寸/類型。

+0

謝謝,這看起來像我以後的簡單解決方案。一旦我嘗試一下,我會報告回來。 – andy 2013-05-08 19:03:51