Philip Vaughn
Philip Vaughn

Reputation: 616

JavaFX & iText7

I need to be able to add nodes to a PDF document. Currently I am converting all nodes to an FX writable image and then loading that image into iText7 to create a PDF file.

For example: say I have a JavaFX BarChart and I need to add that BarChart to a PDF file. Currently I'm converting the chart (through snapshot) to a JavaFX WritableImage saving that image to my computer as a PNG file then loading that file via

com.itextpdf.layout.element.Image image = new com.itextpdf.layout.element.Image(ImageDataFactory.create(myLocation));

This to me seems quite a bit excessive. If any of the iText tech people see this I'd really appreciate a response.

In short (and in summary) I would like a way to add an FX node directly to a PDF file. If there's a better solution than iText for doing this I'm all ears.

Thanks

Upvotes: 2

Views: 2611

Answers (1)

James_D
James_D

Reputation: 209339

You can do

javafx.scene.image.Image fxImage = chart.snapshot(null, null);
java.awt.Image awtImage = SwingFXUtils.fromFXImage(fxImage);
com.itextpdf.layout.element.Image pdfImage = 
    new com.itextpdf.layout.element.Image(ImageDataFactory.create(awtImage, null));

SSCCE:

import java.io.File;
import java.io.FileOutputStream;
import java.util.Random;

import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;

import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart.Data;
import javafx.scene.chart.XYChart.Series;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.layout.BorderPane;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Stage;

public class ITextTest extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        BarChart<String, Number> chart = new BarChart<>(new CategoryAxis(), new NumberAxis());
        Random rng = new Random();
        Series<String, Number> series = new Series<>();
        series.setName("Data");
        for (int i = 1 ; i<=10; i++) {
            series.getData().add(new Data<>("Group "+i, rng.nextDouble()));
        }
        chart.getData().add(series);

        Button save = new Button("Save to pdf");
        FileChooser chooser = new FileChooser();
        chooser.getExtensionFilters().add(new ExtensionFilter("PDF files", "*.pdf"));
        save.setOnAction(e -> {
            File file = chooser.showSaveDialog(primaryStage);
            if (file != null) {
                try {
                    Image img = chart.snapshot(null, null);
                    ImageData imgData = ImageDataFactory.create(SwingFXUtils.fromFXImage(img, null), null);
                    com.itextpdf.layout.element.Image pdfImg = new com.itextpdf.layout.element.Image(imgData);

                    PdfWriter writer = new PdfWriter(new FileOutputStream(file));
                    PdfDocument pdfDoc = new PdfDocument(writer);
                    Document doc = new Document(pdfDoc);
                    doc.add(pdfImg);
                    doc.close();
                } catch (Exception exc) {
                    exc.printStackTrace();
                }               
            }
        });

        BorderPane.setAlignment(save, Pos.CENTER);
        BorderPane.setMargin(save, new Insets(10));
        BorderPane root = new BorderPane(chart, null, null, save, null);

        Scene scene = new Scene(root, 600, 600);
        primaryStage.setScene(scene);
        primaryStage.show();


    }

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

Upvotes: 2

Related Questions