2017-01-18 8 views
0

PrinterJobを使用してデータのリストを印刷することができますが、はNodeしか印刷されないため、TableViewを使用して印刷すると、私はデザインが好きではありません。私のアプリケーションはすでにpdfテーブルを作成する機能を持っています(サンプルはこの質問の終わりに表示されます)。私の質問はこのpdfをどのように印刷することができるかです。この印刷の処理はPOSプリンタと同じですプリンタ? James_D 2の回答に基づいてJava:データの表を印刷する

enter image description here

EDIT

は、私は印刷にこのコードを使用:

次のようにそれはだ createLogDocument用として

Document document = createLogDocument(items); 
if (document != null) { 
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
    PdfWriter.getInstance(document, byteArrayOutputStream); 

    byte[] pdfData = byteArrayOutputStream.toByteArray(); 

    DocFlavor pdfInFormat = DocFlavor.BYTE_ARRAY.PDF; 

    Doc myDoc = new SimpleDoc(pdfData, pdfInFormat, null); 

    PrintRequestAttributeSet set = new HashPrintRequestAttributeSet(); 

    PrintService[] services = PrintServiceLookup.lookupPrintServices(pdfInFormat, set); 
    if (services.length > 0) { 
     PrintService service = ServiceUI.printDialog(null, 50, 50, services, services[0], null, set); 


     DocPrintJob job = service.createPrintJob(); 
     job.print(myDoc, set); 

    } 
} 
} catch (DocumentException e) { 
    e.printStackTrace(); 
} catch (PrintException e) { 
    e.printStackTrace(); 
} 

private static Document createLogDocument(List<Log> logs) throws DocumentException { 
Document document = new Document(); 
document.open(); 

PdfPTable table = new PdfPTable((new float[]{10, 20, 50, 10, 10})); 
table.setWidthPercentage(100); 
table.setSpacingAfter(20); 
table.setSpacingAfter(20); 

PdfPTable header = new PdfPTable((new float[]{10, 20, 50, 10, 10})); 
header.setWidthPercentage(100); 
header.setSpacingAfter(20); 
header.setSpacingBefore(20); 

PdfPCell cell = null; 

setHeaderCell(cell, "Date", header); 
setHeaderCell(cell, "Type", header); 
setHeaderCell(cell, "Details", header); 
setHeaderCell(cell, "Client", header); 
setHeaderCell(cell, "Employee", header); 

for (Log log : logs) { 

    setNormalCell(cell, log.getDate().toString(), table); 

    setNormalCell(cell, log.getDescription().split(":")[0], table); 

    setNormalCell(cell, log.getDescription().split(":")[1], table); 

    setNormalCell(cell, log.getClient().getName(), table); 

    setNormalCell(cell, log.getEmployee().getName(), table); 
} 

boolean b = true; 
for (PdfPRow r : table.getRows()) { 
    for (PdfPCell c : r.getCells()) { 
     c.setBackgroundColor(b ? BaseColor.LIGHT_GRAY : BaseColor.WHITE); 
    } 
    b = !b; 
} 

document.add(header); 
document.add(table); 
document.close(); 

return document; 

}

しかし、私はこのコードをexectueとき、私はそのNo file in print requestを言っjava.io.IOExceptionを取得します。私は何の間違いをしていますか?

答えて

1

JavaFX印刷APIは、TableViewのすべてのデータを印刷することができません。テーブルビューはテーブル内のすべてのアイテムのセルを作成しません。セルはの表示アイテム(ビューからスクロールしないもの)に対してのみ作成され、必要に応じて(ユーザーがスクロールするなど)再利用されます。 JavaFX印刷APIはノードを印刷し、すべてのデータを表すノードは存在しないため、これはうまく動作しません。

既にテーブルにデータのPDF表現があるので、標準javax.print APIを使用してPDFを印刷できます。 package documentationこれを行う方法の詳細を持っていますが、簡単にあなたがするだろう:

FileInputStream pdfStream; 
try { 
    pdfStream = new FileInputStream("file.pdf"); 
} catch (FileNotFoundException ffne) { 
    ffne.printStackTrace(); 
} 
if (pdfStream == null) { 
    return; 
} 

DocFlavor pdfInFormat = DocFlavor.INPUT_STREAM.PDF; 
Doc myDoc = new SimpleDoc(pdfStream, pdfInFormat, null); 
PrintRequestAttributeSet aset = 
     new HashPrintRequestAttributeSet(); 
PrintService service = 
    PrintServiceLookup.lookupDefaultPrintService(psInFormat, aset); 
if (service != null) { 
    DocPrintJob job = service.createPrintJob(); 
    try { 
     job.print(myDoc, aset); 
    } catch (PrintException pe) { 
     pe.printStackTrace(); 
    } 
} 

をあなたのPDFではなく、ファイルに比べて、メモリ内にある場合、あなたはbyte[]の形式でデータを取得することができ、上記のようにファイルを処理する代わりに、

byte[] pdfData = ... ; 
DocFlavor pdfInFormat = DocFlavor.BYTE_ARRAY.PDF; 
Doc myDoc = new SimpleDoc(pdfData, pdfInFormat, null); 

の代わりに実行することができます。

+0

両方のプリンタのためのこの作品は、ウィル(私はPOSと通常の意味)私はそれをしようとするPOSプリンタを所有していないので、? –

+1

@ Ayoub.A私はどちらかを所有していません...それはすべきです。私は実際にここに書類にあなたを参照することができます。 'PrintServiceLookup.lookupPrintServices(...)'を使って文書を印刷することができるすべてのプリンタ( "印刷サービス")を取得し、アクセス権があれば 'PrintService.getAttributes() 'を使ってプリンタのアスペクトを問い合わせまたは変更することができます適切なプリンタ。しかし、私はどちらの方法も試していません... –

+0

印刷ダイアログを表示する方法を教えてください。私はそれを行う方法を見つけることができません。ありがとう –

0

私はcreateLogメソッドをバイト配列を返すように変更しました。うまく動作するように継ぎ目があります。 createLogDocument方法として

try { 
     byte[] pdfData = createLogDocument(items); 

     if (pdfData.length != 0) { 

      DocFlavor pdfInFormat = DocFlavor.BYTE_ARRAY.PDF; 

      Doc myDoc = new SimpleDoc(pdfData, pdfInFormat, null); 

      PrintRequestAttributeSet set = new HashPrintRequestAttributeSet(); 

      PrintService[] services = PrintServiceLookup.lookupPrintServices(pdfInFormat, set); 

      if (services.length > 0) { 
       PrintService service = ServiceUI.printDialog(null, 50, 50, services, services[0], null, set); 

       if (service != null) { 
        DocPrintJob job = service.createPrintJob(); 
        job.print(myDoc, set); 
       } 

      } 
     } 

    } catch (DocumentException e) { 
     e.printStackTrace(); 
    } catch (PrintException e) { 
     e.printStackTrace(); 
    } 

:?

private static byte[] createLogDocument(List<Log> logs) throws DocumentException { 
    Document document = new Document(); 

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
    PdfWriter.getInstance(document, byteArrayOutputStream); 

    document.open(); 

    //Fill the document with info 

    document.close(); 
    byte[] pdfData = byteArrayOutputStream.toByteArray(); 

    return pdfData; 
}