简体   繁体   English

JavaFx-如何整体打印SpreadsheetView

[英]JavaFx - How to print a SpreadsheetView as a whole

I would like to know how to print a SpreadsheetView (ControlFx API). 我想知道如何打印SpreadsheetView(ControlFx API)。 I tried to do this with the following code, but it only prints the visible part of the SpreadsheetView. 我尝试使用以下代码执行此操作,但它仅显示SpreadsheetView的可见部分。

PrinterJob printerJob = PrinterJob.createPrinterJob(); 
            if (printerJob != null && printerJob.showPageSetupDialog(null)) { 
                boolean success = printerJob.printPage(spreadsheetview);
                if (success) {
                    printerJob.endJob();
                }
            }

The result of printing 打印结果

Do you have a solution ? 你有解决方案吗 ?

Thank you 谢谢

Edit N°1 编辑编号1

I tried something with this code and that's work but not totally from This forum : 我用这段代码尝试了一些东西,这是可行的,但并非完全来自本论坛

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.

Following some adaptations of this code I obtained the following result : 经过对该代码的一些修改,我获得了以下结果:

This is the first page, the result is printed on 4 pages 这是第一页,结果打印在4页上

So this code allows to print the Spreadsheet only in all its height 因此,此代码仅允许在整个高度上打印电子表格

That would be difficult because the SpreadsheetView only renders the visible part of the Grid. 这将很困难,因为SpreadsheetView仅呈现网格的可见部分。 Therefore, printing the whole SpreadsheetView might be a bit difficult.. 因此,打印整个SpreadsheetView可能会有点困难。

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

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