2014-04-01 97 views
1

有沒有任何方法將Stage或其他組件如BorderPane與可視化組件導出爲PDF文件?當我點擊一個按鈕將組件導出爲PDF文件時,我想要什麼?有沒有例子?導出階段爲PDF

回答

1

首先猜測,您可以簡單地將Scene根存儲到圖像,然後將其包含到PDF中。詳情請參閱Node#snapshot方法。

1

您可以發送一個節點到打印機,並將其與虛擬打印機

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

您可以在JavaFX項目使用PDFBox的libreries保存爲PDF格式。此代碼從節點獲取快照,製作圖像文件,然後創建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); 
} 

} 
+0

嗨,你的方法效果很好,但我的PNG太大的PDF文件,我應該如何解決這個問題? –

+1

嗨。在內容「content.drawImage(pdimage,100,100)」;「你可以添加高度的寬度參數,如」content.drawImage(pdimage,100,100,70,70);「 –