简体   繁体   English

Java:打印数据表

[英]Java: Print table of data

I want to be able to print a list of data using PrinterJob , but it seems PrinterJob prints only Node s, so when I use a TableView and print it, first it does not print all elements, second I do not like the design.我希望能够使用PrinterJob打印数据PrinterJob ,但似乎PrinterJob只打印Node s,所以当我使用TableView并打印它时,首先它不打印所有元素,其次我不喜欢设计。 My application already has a functionality of creating pdf tables (a sample is show at the end of this question) and my question is how can I print this pdf, and this proccess of printing is it the same for a POS printer as for a normal printer?我的应用程序已经具有创建 pdf 表格的功能(在这个问题的末尾显示了一个示例),我的问题是如何打印这个 pdf,这个打印过程对于 POS 打印机和普通打印机是一样的打印机?

在此处输入图片说明

EDIT编辑

Based on the answer of James_D 2, I used this code to print:根据 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();
}

as for the createLogDocument it's as follows:至于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;

} }

But when I exectue this code I get a java.io.IOException saying that No file in print request .但是当我执行这段代码时,我得到一个java.io.IOExceptionNo file in print request What mistake am I making?我犯了什么错误?

The JavaFX print API will not be able to print your TableView will all the data in it. JavaFX 打印 API 将无法打印TableView所有数据。 Recall that table views do not create cells for all of the items in the table: cells are only created for visible items (those not scrolled out of the view) and are reused as needed (eg when the user scrolls).回想一下,表格视图不会为表格中的所有项目创建单元格:单元格仅为可见项目(未滚动出视图的项目)创建,并在需要时重用(例如,当用户滚动时)。 Since the JavaFX print API prints nodes, and no nodes exist representing all of the data, then this simply won't work.由于 JavaFX 打印 API 打印节点,并且不存在代表所有数据的节点,因此这根本行不通。

Since you already have a PDF representation of the data in your table, you can use the standard javax.print API to print the PDF.由于您已经拥有表中数据的 PDF 表示,因此您可以使用标准javax.print API 来打印 PDF。 The package documentation has details on how to do this, but in brief you would do: 包文档详细说明了如何执行此操作,但简而言之,您将执行以下操作:

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

If your PDF is in memory, rather than in a file, and you can get the data in the form of a byte[] , you can do如果您的 PDF 在内存中,而不是在文件中,并且您可以以byte[]的形式获取数据,则可以这样做

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

instead of working with the file as above.而不是像上面那样处理文件。

Well I changed my createLog method to return a byte array, and it seams to work fine.好吧,我改变了我的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();
    }

As for the createLogDocument method:至于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