Passing values from a class when generating a pdf in JavaFX

0

Good afternoon, I created a PDF.java, which has the function of generating a pdf, and in another file (FormularioHoteleriosController.java) by clicking on a particular button, I instantiate the PDF.java file in order to generate and open pdf , however, I need when I click the button that is in the FormulasHotelControler.java, retrieve values from EditText's located in it, and then print the results in the PDF. Thank you !!

FILE GENERATING PDF:

public class Pdf {
public static final String IMAGE = "imagens/boleto.png";
public static final String DEST = "results/images/boleto.pdf";

public static void main(String[] args) throws IOException, DocumentException {
    File file = new File(DEST);
    file.getParentFile().mkdirs();
    new Pdf().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
    Document document = new Document(PageSize.A4.rotate());
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
    document.open();

    PdfContentByte canvas = writer.getDirectContentUnder();
    Image image = Image.getInstance(IMAGE);
    image.scaleAbsolute(PageSize.A4.rotate());
    image.setAbsolutePosition(0, 0);
    canvas.addImage(image);
    document.close();

    Desktop.getDesktop().open(new File("results/images/boleto.pdf"));
}

}

FILE CALLING INSTANCIA PDF.AJVA BY CLICKING THE BUTTON:

@FXML public void gerarPdf(){

    try {
        Pdf.main(null);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


}
    
asked by anonymous 02.11.2017 / 13:47

1 answer

0

I suggest you create a modal window to view your PDF . First let's fix your PDF generation class by following good Java programming practices:

public class GeradorPdf {

    private String source;
    private String destination;

    // Construtor da Classe
    public GeradorPdf(String source, String destination) {
        this.source = source;
        this.destination = destination;
    }

    // Não mexi neste método mas você poderia retornar a Image gerada
    // para uma classe VisualizadorPdf
    public void createPdf() throws IOException, DocumentException {
        Document document = new Document(PageSize.A4.rotate());
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(destination));
        document.open();

        PdfContentByte canvas = writer.getDirectContentUnder();
        Image image = Image.getInstance(source);
        image.scaleAbsolute(PageSize.A4.rotate());
        image.setAbsolutePosition(0, 0);
        canvas.addImage(image);
        document.close();

        Desktop.getDesktop().open(new File("results/images/boleto.pdf"));
   }

   // Encapsulamento dos atributos
   public String getSource(){
       return this.source;
   }

   public String getDestination(){
       return this.destination;
   }

   public void setSource(String newSource){
       this.source = newSource;
   }

   public void setDestination(String newDestination){
       this.destination = newDestination;
   }

Now I will show how to create a modal window in JavaFX using FXML. First we'll have to create a global variable for your Stage in the start method of your main class:

public static Stage stage;

@Override
public void start(Stage primaryStage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

    Scene scene = new Scene(root);
    stage = primaryStage;
    stage.setScene(scene);
    stage.show();
}

Now the Pdf Viewer class (Modal Window):

public class VisualizadorPdf {

    private final Stage dialog;
    private final Image image;
    // O resto de seus atributos (botões, paineis, etc);

    public VisualizadorPdf(Stage owner, Image image){
        this.image = image;

        dialog = new Stage();
        dialog.initModality(Modality.WINDOW_MODAL);
        dialog.initOwner(owner);
        dialog.setScene(createScene());
        dialog.setTitle("Título da minha janela");
    }

    public Scene createScene() {
        // Crie sua cena aqui
    }

    // Método para exibir sua janela
    public void show() {
        dialog.show();
    }
}

If you're running out of ideas on how to create a scene you can look at this viewer of pdf . It would be interesting to pass the image from the Generator to the Viewer, or you can join the two in a class only, it's up to you.

Finally the use would look like this:

@FXML
private TextField meutextfield;

// ...

@FXML 
public void gerarPdf(){

    try {

        // Pegando valores dos TextField
        String valor = meutextfield.getText();

        GeradorPdf gerador = new GeradorPdf("imagens/boleto.png", "results/images/boleto.pdf");
        Image image = gerador.createPdf(); // Supondo que seguiu minha sugestão
        VisualizadorPdf visualizador = new VisualizadorPdf(SuaClasse.stage, image);
        visualizador.show();

    } catch (IOException | DocumentException e) {
        e.printStackTrace();
    } 
} 
    
02.11.2017 / 23:16