将舞台导出为 PDF

Pet*_*zov 6 javafx javafx-2 javafx-8

有没有办法将 Stage 或其他一些组件(例如带有可视组件的 BorderPane)导出到 PDF 文件中?我想当我单击按钮时将组件导出为 PDF 文件?有例子吗?

Gio*_*ras 8

您可以在 javafx 项目中使用 PDFBox 库。此代码从节点拍摄快照,创建图像文件,然后创建 pdf 文件并将图像插入到 pdf 文件中。你需要这个.jar http://www-us.apache.org/dist/pdfbox/2.0.8/pdfbox-app-2.0.8.jar。使用pdfbox,您可以将javafx字符串传递给pdf中的文本,您可以做很多事情(更改字体,大小......)

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

 }
Run Code Online (Sandbox Code Playgroud)


xue*_*eng 1

第一个猜测是,您可以简单地将场景根保存到图像中,然后将其包含到 PDF 中。有关详细信息,请参阅Node#snapshot方法。