Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaFX打印多页表格视图_Java_Printing_Javafx_Tableview - Fatal编程技术网

JavaFX打印多页表格视图

JavaFX打印多页表格视图,java,printing,javafx,tableview,Java,Printing,Javafx,Tableview,所以,我的问题是我需要打印我的tableview的内容,但是我有太多的项目,所以它只打印前23个项目。 我已经在这里找到了一些解决方案,不幸的是它们没有多大帮助 这是我的打印方法: @FXML private void printIt() { Printer printer = Printer.getDefaultPrinter(); PageLayout pageLayout = printer.createPageLayout(Paper.A4, PageOrientatio

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

这是我的打印方法:

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

我自己找到了解决办法

1) 创建for循环

2) 创建新表并插入未打印的项目


3) 打印

在我找到答案之前,我浏览了很多帖子。关键是扩展tableview的高度以显示,而无需在屏幕右侧滚动。(有一些方法可以在不扭曲程序布局的情况下完成此操作。我在本问题中没有提到这一部分。关于如何完成此任务,有非常好的答案。)一旦您将tableview的高度扩展到显示所有行,而无需滚动条。然后一次只打印一页,从零高度到一页的负高度(在这种情况下:向下接近11英寸。边框在其中起作用。)下一页应该在最后一页结束的地方打印,并再向下打印到几乎11英寸。(从大约-11英寸到大约-22英寸)此模式将持续,直到遍历了tableview的整个高度

    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.
Printer-Printer=Printer.getDefaultPrinter()//获取默认打印机
javafx.print.PageLayout PageLayout=printer.createPageLayout(Paper.NA_-LETTER,PageOrientation.肖像,printer.MarginType.DEFAULT)//创建页面布局。我用Paper.NA_字母表示标准的8.5x11页。
PrinterJob作业=PrinterJob.createPrinterJob()//创建打印机作业
if(job.showPrintDialog(taMain.getScene().getWindow())//这非常有用,它允许您将文件保存为pdf格式,而不是使用打印机的所有纸张。弹出一个对话框,允许您将“名称”选项从默认打印机更改为Adobe pdf。
{
double pagePrintableWidth=pageLayout.getPrintableWidth();//此页面布局应为8.5英寸。
double pagePrintableHeight=pageLayout.getPrintableHeight();//此页面布局应为11英寸。
tblvMain.prefHeightProperty().bind(Bindings.size(tblvMain.getItems()).multiply(35));//如果单元格的行大小可变,则添加.multiply并使用输入值,直到输出接近所需值。如果单元格的行高度相同,我认为可以使用.multiply(1)。这将更改tableView的高度以显示表中的所有行。
tblvMain.minHeightProperty().bind(tblvMain.prefHeightProperty());//您可能可以使用它来查看是否确实需要它。请对其进行注释以了解情况。
tblvMain.maxHeightProperty().bind(tblvMain.prefHeightProperty());//您可能可以使用它来查看是否确实需要它。请对其进行注释以了解情况。
double scaleX=pagePrintableWidth/tblvMain.getBoundsInParent().getWidth();//缩小比例,使打印宽度适合纸张的宽度范围。
double scaleY=scaleX;//使用与宽度相同的比例缩放高度。这允许书写和图像保持其比例,或者看起来不歪斜。
double localScale=scaleX;//实际上不需要,因为所有内容都是按相同的比例缩小的。scaleX在考虑程序的情况下用于缩放打印输出。
double numberOfPages=Math.ceil((tblvMain.getPrefHeight()*localScale)/pagePrintableHeight);//用于计算将要打印的页数。
//System.out.println(“pref Height:+tblvMain.getPrefHeight());
//System.out.println(“页数:“+numberOfPages”);
tblvMain.getTransforms().add(新比例(scaleX,(scaleY));//缩放打印。允许宽度在纸张宽度内,并缩放高度以消除倾斜的字母和图像。
tblvMain.getTransforms().add(new Translate(0,0));//在需要打印的图像的左上角开始第一次打印
//由于需要打印的部分的高度比纸张的高度长,因此我们使用gridTransfrom仅为给定页面选择要打印的部分。
Translate gridTransform=新的Translate();
tblvMain.getTransforms().add(gridTransform);
//现在我们循环遍历需要打印的图像,只打印完整图像的子图像。
//例如:在第一个循环中,我们只从上到下将可打印图像缩小到标准纸张的高度。然后我们从最后一页开始打印,一直到下一页的高度。直到所有页面都打印出来为止。
//第一页打印高度从0到-11英寸,第二页打印高度从-11英寸到-22英寸,以此类推。
对于(int i=0;i
打印机打印机=打印机。getDefaultPrinter()

Printer Printer=Printer.getDefaultPrinter();PrinterJob PrinterJob=PrinterJob.createPrinterJob();
PageLayout PageLayout=printer.createPageLayout(纸张.A4,页面方向.横向,打印机.MarginType.HARDWARE_最小值);
printerJob.getJobSettings().setPageLayout(pageLayout);
Stage Stage=(Stage)anchorPane.getScene().getWindow();
布尔openPrintDialog=printerJob.showPrintDialog(阶段);
如果(openPrintDialog){
tableView.setScaleX(0.8);
tableView.setScaleY(0.8);
tableView.setTranslateX(-70);
tableView.setTranslateY(-50);
ObservableList allPrintItems=tableView.getItems();
ObservableList pageList=FXCollections.Observable
            Printer printer = Printer.getDefaultPrinter();PrinterJob  printerJob = PrinterJob.createPrinterJob();
            PageLayout pageLayout = printer.createPageLayout(Paper.A4, PageOrientation.LANDSCAPE, Printer.MarginType.HARDWARE_MINIMUM);
            printerJob.getJobSettings().setPageLayout(pageLayout);
            Stage stage = (Stage) anchorPane.getScene().getWindow();
            boolean openPrintDialog = printerJob.showPrintDialog(stage);
            if(openPrintDialog){
                tableView.setScaleX(0.8);
                tableView.setScaleY(0.8);
                tableView.setTranslateX(-70);
                tableView.setTranslateY(-50);
                ObservableList<List<SimpleStringProperty>> allPrintItems = tableView.getItems();
                ObservableList <List<SimpleStringProperty>> pageList = FXCollections.observableArrayList();
                boolean printing = false;
                for(int i=0; i<allPrintItems.size(); i++) {
                    List<SimpleStringProperty> oneRow = allPrintItems.get(i);
                    pageList.add(oneRow);
                    if(i!=0 && (i%24==0 || i == (allPrintItems.size()-1))){
                        tableView.setItems(pageList);
                        printing = printerJob.printPage(tableView);
                        pageList.clear();
                    }
                }
                tableView.setItems(allPrintItems);
                if(printing)printerJob.endJob();
                tableView.setScaleX(1.0);
                tableView.setScaleY(1.0);
                tableView.setTranslateX(0);
                tableView.setTranslateY(0);