JavaFX在多个页面上打印tableview [英] JavaFX print tableview on multiple pages

查看:196
本文介绍了JavaFX在多个页面上打印tableview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我的问题是我需要打印我的tableview的内容,但我有很多项目,它只打印其中的前23个。
我已经在这里找到了一些解决方案,不幸的是它们没有多大帮助。

So, my problem is that I need to print the content of my tableview, but I have so many items in it, that it only prints the first 23 of them. I found a few solutions here already, unfortunately they didn't help much.

这是我的打印方式:

@FXML
private void printIt() {
    Printer printer = Printer.getDefaultPrinter();
    PageLayout pageLayout = printer.createPageLayout(Paper.A4, PageOrientation.LANDSCAPE, Printer.MarginType.DEFAULT);
    double scaleX = pageLayout.getPrintableWidth() / logBookTable.getBoundsInParent().getWidth();
    double scaleY = pageLayout.getPrintableHeight() / logBookTable.getBoundsInParent().getHeight();
    logBookTable.getTransforms().add(new Scale(scaleX, scaleY));

    PrinterJob job = PrinterJob.createPrinterJob();
    if (job != null) {
        boolean successPrintDialog = job.showPrintDialog(dialogStage);
        if(successPrintDialog){
            boolean success = job.printPage(pageLayout,logBookTable);
            if (success) {
                job.endJob();
            }
        }
    }
}


推荐答案

在我能够得出答案之前,我浏览了很多帖子。关键是扩展tableview的高度以显示,而无需在屏幕右侧滚动。 (有一些方法可以做到这一点而不会扭曲你的程序布局。我没有在这个问题中解决这个问题。如何完成这个任务有很好的答案。)一旦你将tableview的高度扩展到show all all它的行不需要滚动条。然后只需要从零高度开始一次打印一页到一页的负高度。(在这种情况下:接近11英寸向下。边框在这里起作用。)下一页应该在最后一页找到页面结束并打印到近11英寸。 (从大约-11英寸到大约-22英寸。)这种模式一直持续到遍历了tableview的整个高度。

I looked through a lot of posts before I was able to come up with an answer. The key is to extend the height of the tableview to show without the need to have a scroll on the right of the screen. (There are ways of doing this without skewing your programs layout. I did not address that part in this questions. There are very good answer on how to accomplish this task.) Once you have extend the height of the tableview to the show all of it's rows without the need for a scroll bar. Then just print one page at a time starting from height of zero going to a negative height of one page.(In this case: close to 11 inches down. Borders play a role in this.) The next page should pick up where the last page ended and print down to almost 11 more inches. (from about -11 inches to about -22 inches.) This pattern continues until the whole height of the tableview has been traversed.

    Printer printer = Printer.getDefaultPrinter(); //get the default printer
    javafx.print.PageLayout pageLayout = printer.createPageLayout(Paper.NA_LETTER, PageOrientation.PORTRAIT, Printer.MarginType.DEFAULT); //create a pagelayout.  I used Paper.NA_LETTER for a standard 8.5 x 11 in page.
    PrinterJob job = PrinterJob.createPrinterJob();//create a printer job

    if(job.showPrintDialog(taMain.getScene().getWindow()))// this is very useful it allows you to save the file as a pdf instead using all of your printer's paper. A dialog box pops up, allowing you to change the "name" option from your default printer to Adobe pdf. 
    {
        double pagePrintableWidth = pageLayout.getPrintableWidth(); //this should be 8.5 inches for this page layout.
        double pagePrintableHeight = pageLayout.getPrintableHeight();// this should be 11 inches for this page layout.


        tblvMain.prefHeightProperty().bind(Bindings.size(tblvMain.getItems()).multiply(35));// If your cells' rows are variable size you add the .multiply and play with the input value until your output is close to what you want. If your cells' rows are the same height, I think you can use .multiply(1). This changes the height of your tableView to show all rows in the table.
        tblvMain.minHeightProperty().bind(tblvMain.prefHeightProperty());//You can probably play with this to see if it's really needed.  Comment it out to find out.
        tblvMain.maxHeightProperty().bind(tblvMain.prefHeightProperty());//You can probably play with this to see if it' really needed.  Comment it out to find out.

        double scaleX = pagePrintableWidth / tblvMain.getBoundsInParent().getWidth();//scaling down so that the printing width fits within the paper's width bound.
        double scaleY = scaleX; //scaling the height using the same scale as the width. This allows the writing and the images to maintain their scale, or not look skewed.
        double localScale = scaleX; //not really needed since everything is scaled down at the same ratio. scaleX is used thoughout the program to scale the print out.

        double numberOfPages = Math.ceil((tblvMain.getPrefHeight() * localScale) / pagePrintableHeight);//used to figure out the number of pages that will be printed.



        //System.out.println("pref Height: " + tblvMain.getPrefHeight());
        //System.out.println("number of pages: " + numberOfPages);


        tblvMain.getTransforms().add(new Scale(scaleX, (scaleY)));//scales the printing. Allowing the width to say within the papers width, and scales the height to do away with skewed letters and images.
        tblvMain.getTransforms().add(new Translate(0, 0));// starts the first print at the top left corner of the image that needs to be printed

        //Since the height of what needs to be printed is longer than the paper's heights we use gridTransfrom to only select the part to be printed for a given page.
        Translate gridTransform = new Translate();
        tblvMain.getTransforms().add(gridTransform);

        //now we loop though the image that needs to be printed and we only print a subimage of the full image.
        //for example: In the first loop we only pint the printable image from the top down to the height of a standard piece of paper. Then we print starting from were the last printed page ended down to the height of the next page. This happens until all of the pages are printed. 
        // first page prints from 0 height to -11 inches height, Second page prints from -11 inches height to -22 inches height, etc. 
        for(int i = 0; i < numberOfPages; i++)
        {
            gridTransform.setY(-i * (pagePrintableHeight / localScale));
            job.printPage(pageLayout, tblvMain);
        }

        job.endJob();//finally end the printing job.

这篇关于JavaFX在多个页面上打印tableview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆