繁体   English   中英

Java:打印数据表

[英]Java: Print table of data

我希望能够使用PrinterJob打印数据PrinterJob ,但似乎PrinterJob只打印Node s,所以当我使用TableView并打印它时,首先它不打印所有元素,其次我不喜欢设计。 我的应用程序已经具有创建 pdf 表格的功能(在这个问题的末尾显示了一个示例),我的问题是如何打印这个 pdf,这个打印过程对于 POS 打印机和普通打印机是一样的打印机?

在此处输入图片说明

编辑

根据 James_D 2 的回答,我用这段代码打印:

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();
}

至于createLogDocument如下:

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;

}

但是当我执行这段代码时,我得到一个java.io.IOExceptionNo file in print request 我犯了什么错误?

JavaFX 打印 API 将无法打印TableView所有数据。 回想一下,表格视图不会为表格中的所有项目创建单元格:单元格仅为可见项目(未滚动出视图的项目)创建,并在需要时重用(例如,当用户滚动时)。 由于 JavaFX 打印 API 打印节点,并且不存在代表所有数据的节点,因此这根本行不通。

由于您已经拥有表中数据的 PDF 表示,因此您可以使用标准javax.print API 来打印 PDF。 包文档详细说明了如何执行此操作,但简而言之,您将执行以下操作:

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);  

而不是像上面那样处理文件。

好吧,我改变了我的createLog方法来返回一个字节数组,它看起来工作正常。

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();
    }

至于createLogDocument方法:

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM