Append text for printing before and after a JTable

后端 未结 1 1821
旧时难觅i
旧时难觅i 2020-11-29 12:36

I am trying to print a JTable and the print() method works great till I come to this scenario. Lets say I want to print before, in the first page o

相关标签:
1条回答
  • 2020-11-29 13:01

    One way to do this is to append() a series of suitable Printable instances to a java.awt.print.Book, as shown here.

    Addendum: JTable has a getPrintable() method that should simplify things; here's an outline and simple title Printable:

    PrinterJob pj = PrinterJob.getPrinterJob();
    Book book = new Book();
    book.append(new Title(), pj.defaultPage());
    book.append(table.getPrintable(...), pj.defaultPage());
    book.append(new EndPage(), pj.defaultPage());
    pj.setPageable(book);
    pj.print();
    ...
    private static class Title implements Printable {
    
        Font font = new Font("SansSerif", Font.PLAIN, 48);
    
        @Override
        public int print(Graphics g, PageFormat pf, int pageIndex)
            throws PrinterException {
            Graphics2D g2d = (Graphics2D) g;
            g2d.translate(pf.getImageableX(), pf.getImageableY());
            g2d.setFont(font);
            g2d.setColor(Color.black);
            g2d.drawString("Report", 50, 200);
            return Printable.PAGE_EXISTS;
        }
    }
    
    0 讨论(0)
提交回复
热议问题