Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javafx 将阶段导出为PDF_Javafx_Javafx 2_Javafx 8 - Fatal编程技术网

Javafx 将阶段导出为PDF

Javafx 将阶段导出为PDF,javafx,javafx-2,javafx-8,Javafx,Javafx 2,Javafx 8,有没有办法将Stage或其他组件(如BorderPane)和可视组件导出到PDF文件中?当我点击一个按钮将组件导出到PDF文件时,我想要什么?有什么例子吗?第一个猜测是,您可以简单地将场景根保存到图像中,然后将其包含到PDF中。有关详细信息,请参阅方法。您可以将节点发送到打印机,并使用虚拟打印机将其另存为pdf public class PrinterNodetExample extends Application { @Override public void start(Stage pr

有没有办法将Stage或其他组件(如BorderPane)和可视组件导出到PDF文件中?当我点击一个按钮将组件导出到PDF文件时,我想要什么?有什么例子吗?

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

您可以将节点发送到打印机,并使用虚拟打印机将其另存为pdf

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);
}}
公共类printerNodeSample扩展应用程序{
@凌驾
公共无效开始(阶段primaryStage){
按钮btn=新按钮();
StackPane root=新的StackPane();
root.getChildren().add(btn);
场景=新场景(根,300,250);
btn.setText(“说‘你好,世界’”);
btn.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
System.out.println(“到打印机!”);
PrinterJob作业=PrinterJob.createPrinterJob();
如果(作业!=null){
job.showPrintDialog(primaryStage);
作业打印页(根);
job.endJob();
}
}
});
primaryStage.setTitle(“打印机”);
初级阶段。场景(场景);
primaryStage.show();
}
公共静态void main(字符串[]args){
发射(args);
}}

您可以在javafx项目中使用PDFBox库。这段代码从一个节点获取快照,生成一个图像文件,然后创建一个pdf文件并将图像插入pdf文件中。你需要这个,罐子。使用pdfbox,您可以将javafx字符串传递到pdf中的文本,您可以做很多事情(更改字体、大小……)

导入java.io.File;
导入java.io.IOException;
导入java.util.logging.Level;
导入java.util.logging.Logger;
导入javafx.application.application;
导入javafx.embed.swing.SwingFXUtils;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.scene.scene;
导入javafx.scene.Snapshot参数;
导入javafx.scene.chart.BarChart;
导入javafx.scene.chart.CategoryAxis;
导入javafx.scene.chart.NumberAxis;
导入javafx.scene.control.Button;
导入javafx.scene.image.WritableImage;
导入javafx.scene.layout.StackPane;
导入javafx.stage.stage;
导入javax.imageio.imageio;
导入org.apache.pdfbox.pdmodel.PDDocument;
导入org.apache.pdfbox.pdmodel.PDPage;
导入org.apache.pdfbox.pdmodel.PDPageContentStream;
导入org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
公共类NodeToPdf扩展应用程序{
@凌驾
公共无效开始(阶段primaryStage){
CategoryAxis xAxis=新CategoryAxis();
xAxis.setLabel(“x”);
NumberAxis yAxis=新的NumberAxis();
yAxis.setLabel(“y”);
条形图条形图=新条形图(X轴、Y轴);
bar.setMaxSize(300300);
bar.setTitle(“bar节点”);
bar.setTranslateY(-100);
按钮btn=新按钮();
btn.setTranslateY(100);
btn.setText(“至Pdf”);
btn.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
WritableImage nodeshot=bar.snapshot(新的SnapshotParameters(),null);
File File=新文件(“chart.png”);
试一试{
write(SwingFXUtils.fromFXImage(nodeshot,null),“png”,文件);
}捕获(IOE异常){
}
PDDocument doc=新的PDDocument();
PDPage page=新PDPage();
PDImageXObject pdimage;
PDPageContentStream内容;
试一试{
pdimage=PDImageXObject.createFromFile(“chart.png”,doc);
内容=新PDPageContentStream(文档,页面);
内容.drawImage(pdimage,100100);
content.close();
文件添加页(第页);
doc.save(“pdf_file.pdf”);
doc.close();
delete();
}捕获(IOEX异常){
Logger.getLogger(NodeToPdf.class.getName()).log(Level.SEVERE,null,ex);
}
}
});
StackPane root=新的StackPane();
root.getChildren().add(btn);
root.getChildren().add(bar);
场景=新场景(root,600600);
初级阶段。场景(场景);
primaryStage.show();
}
公共静态void main(字符串[]args){
发射(args);
}
}

Hi,你的方法很好用,但是我的png对于pdf文件来说太大了,我应该如何解决这个问题?Hi。在“content.drawImage(pdimage,100100);”行中,“您可以像这样添加高度和宽度参数”content.drawImage(pdimage,100100,70,70);”
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);
}

 }