PrinterJob
を使用してデータのリストを印刷することができますが、はNode
しか印刷されないため、TableView
を使用して印刷すると、私はデザインが好きではありません。私のアプリケーションはすでにpdfテーブルを作成する機能を持っています(サンプルはこの質問の終わりに表示されます)。私の質問はこのpdfをどのように印刷することができるかです。この印刷の処理はPOSプリンタと同じですプリンタ? James_D 2の回答に基づいてJava:データの表を印刷する
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
を取得します。私は何の間違いをしていますか?
両方のプリンタのためのこの作品は、ウィル(私はPOSと通常の意味)私はそれをしようとするPOSプリンタを所有していないので、? –
@ Ayoub.A私はどちらかを所有していません...それはすべきです。私は実際にここに書類にあなたを参照することができます。 'PrintServiceLookup.lookupPrintServices(...)'を使って文書を印刷することができるすべてのプリンタ( "印刷サービス")を取得し、アクセス権があれば 'PrintService.getAttributes() 'を使ってプリンタのアスペクトを問い合わせまたは変更することができます適切なプリンタ。しかし、私はどちらの方法も試していません... –
印刷ダイアログを表示する方法を教えてください。私はそれを行う方法を見つけることができません。ありがとう –