Print contents of JavaFx TableView

前端 未结 4 1396
野性不改
野性不改 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();
                        }
                    }
                }
            }
    

提交回复
热议问题