Export Stage into PDF

坚强是说给别人听的谎言 提交于 2019-12-03 21:40:00

you can send a node to a printer and save it as pdf with a virtual printer

public class PrinterNodetExample extends Application {


@Override
public void start(Stage primaryStage) {
    Button btn = new Button();
    StackPane root = new StackPane();
    root.getChildren().add(btn);

    Scene scene = new Scene(root, 300, 250);
    btn.setText("Say 'Hello World'");
    btn.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            System.out.println("To Printer!");
            PrinterJob job = PrinterJob.createPrinterJob();
            if(job != null){
            job.showPrintDialog(primaryStage); 
            job.printPage(root);
            job.endJob();
    }
        }
    });



    primaryStage.setTitle("Printer");
    primaryStage.setScene(scene);
    primaryStage.show();



}


public static void main(String[] args) {
    launch(args);
}}

You can use PDFBox libreries in your javafx project . this code take a snapshot from a node , make an image file , then create a pdf file and insert the image in the pdf file . you need this .jar http://www-us.apache.org/dist/pdfbox/2.0.8/pdfbox-app-2.0.8.jar . with pdfbox you can pass strings of javafx to text in pdf , you can do many things ( change font , size .... )

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.control.Button;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
 import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;


 public class NodeToPdf extends Application {

@Override
public void start(Stage primaryStage) {

    CategoryAxis xAxis = new CategoryAxis();
    xAxis.setLabel("x");
     NumberAxis yAxis = new NumberAxis();
    yAxis.setLabel("y");
    BarChart bar = new BarChart(xAxis, yAxis);
    bar.setMaxSize(300, 300);
    bar.setTitle("Bar node" );
    bar.setTranslateY(-100);

    Button btn = new Button();
    btn.setTranslateY(100);
    btn.setText("To Pdf'");



    btn.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            WritableImage nodeshot = bar.snapshot(new SnapshotParameters(), null);
            File file = new File("chart.png");

try {
    ImageIO.write(SwingFXUtils.fromFXImage(nodeshot, null), "png", file);
} catch (IOException e) {

}

            PDDocument doc    = new PDDocument();
            PDPage page = new PDPage();
            PDImageXObject pdimage;
            PDPageContentStream content;
            try {
                pdimage = PDImageXObject.createFromFile("chart.png",doc);
                content = new PDPageContentStream(doc, page);
                content.drawImage(pdimage, 100, 100);
                content.close();
                doc.addPage(page);
                doc.save("pdf_file.pdf");
                doc.close();
                file.delete();
            } catch (IOException ex) {
                Logger.getLogger(NodeToPdf.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    });


    StackPane root = new StackPane();

    root.getChildren().add(btn);
    root.getChildren().add(bar);




    Scene scene = new Scene(root, 600, 600);


    primaryStage.setScene(scene);
    primaryStage.show();
}


public static void main(String[] args) {
    launch(args);
}

 }

First guess is that you could simply save the Scene root to the image and then include it to the PDF. See Node#snapshot method for details.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!