Print contents of JavaFx TableView

前端 未结 4 1397
野性不改
野性不改 2020-12-09 06:55

I am looking for a way to print the contents of a JavaFX TableView. I understand that JavaFX doesn\'t have Printing capabillities just yet (what a disapointment). I have fou

相关标签:
4条回答
  • 2020-12-09 07:19

    Snip the area you want

    Rectangle rect = new Rectangle(0,0,dataDisplayAreaAnchorPane.getWidth(),dataDisplayAreaAnchorPane.getHeight());
                dataDisplayAreaAnchorPane.setClip(rect);
                WritableImage writableImage;
                writableImage = new WritableImage((int) dataDisplayAreaAnchorPane.getPrefWidth(),
                        (int) dataDisplayAreaAnchorPane.getPrefHeight());
                dataDisplayAreaAnchorPane.snapshot(null, writableImage);        
                eventDispatcher.printLandscape(writableImage);
    
    **------------------------------------**
    

    Resize according to A4 paper size and print

    public void print(WritableImage writableImage, Stage primaryStage) {
                ImageView imageView =new ImageView(writableImage);
                Printer printer = Printer.getDefaultPrinter();
                PageLayout pageLayout = printer.createPageLayout(Paper.A4, PageOrientation.LANDSCAPE, Printer.MarginType.DEFAULT);
                double scaleX = pageLayout.getPrintableWidth() / imageView.getBoundsInParent().getWidth();
                double scaleY = pageLayout.getPrintableHeight() / imageView.getBoundsInParent().getHeight();
                imageView.getTransforms().add(new Scale(scaleX, scaleY));
    
                PrinterJob job = PrinterJob.createPrinterJob();
                if (job != null) {
                    boolean successPrintDialog = job.showPrintDialog(primaryStage.getOwner());
                    if(successPrintDialog){
                        boolean success = job.printPage(pageLayout,imageView);
                        if (success) {
                            job.endJob();
                        }
                    }
                }
            }
    
    0 讨论(0)
  • 2020-12-09 07:20

    I understand that JavaFX doesn't have Printing capabillities just yet (what a disapointment).

    Java 8 includes printing capabilities for JavaFX. https://jdk8.java.net/download.html

    The Java 8 printing features were implemented under RT-17383 Printing and function as described in @Asimaruk's answer.

    I have found some information about taking a screenshot of a WebView for example and print it as an image. Is it possible to do something like that with a Table view.

    You can do this if you can't use Java 8.

    Use the JavaFX snapshot api, use SwingFXUtils to convert the JavaFX image to a BufferedImage, use Swing print methods to print the BufferedImage.

    How to go about to handle multiple pages on tables with many data.

    If you can't use Java 8, use the above snapshot method for each page, scrolling the table data between each snapshot.


    Also see RT-17665 Make some UI controls support printing their content which is a request for a direct print api added to some controls (e.g. TableView), so you could right tableView.print() to print only the tableview and not an entire scene. This convenience API for printing has not currently been implemented but you can vote or comment on the related issue to indicate your interest in the feature.

    0 讨论(0)
  • 2020-12-09 07:23

    I have tried this . It first shows a page dialog window, then scales, translates and prints.

    cmItem2.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e){
                Printer printer = Printer.getDefaultPrinter();
                Stage dialogStage = new Stage(StageStyle.DECORATED);            
                PrinterJob job = PrinterJob.createPrinterJob(printer);
                    if (job != null) {                    
                        boolean showDialog = job.showPageSetupDialog(dialogStage);
                        if (showDialog) {                        
                            table.setScaleX(0.60);
                            table.setScaleY(0.60);
                            table.setTranslateX(-220);
                            table.setTranslateY(-70);
                        boolean success = job.printPage(table);
                            if (success) {
                                 job.endJob(); 
                            } 
                            table.setTranslateX(0);
                            table.setTranslateY(0);               
                            table.setScaleX(1.0);
                            table.setScaleY(1.0);                                              
                                        }    
                                    }
            }});
        ContextMenu menu = new ContextMenu();
        menu.getItems().addAll(cmItem1, cmItem2);
        table.setContextMenu(menu);
    
    0 讨论(0)
  • 2020-12-09 07:25

    Printing API appeared in fx8.0. And it can print nodes. You can create printer job with javafx.print.PrinterJob class. But it prints only region that fits to a printed page, and not the one that you on a screen. So you need to make your node fit page(scale, translate, etc) by hands. Here is simple printing example:

       PrinterJob printerJob = PrinterJob.createPrinterJob();
       if(printerJob.showPrintDialog(primaryStage.getOwner()) && printerJob.printPage(yourNode))
           printerJob.endJob();
    
    0 讨论(0)
提交回复
热议问题