Pass class values when generating a pdf in Javafx

Asked

Viewed 337 times

1

Good afternoon, I created a PDF.java, which has the function of generating a pdf, and in another file (Formulariohotelerioscontroller.java) when clicking on a certain button, I urge the PDF.java file to be able to generate and open the pdf, however, I need that when I click on the button that finds itif in Formulariohotelerioscontroler.java, redeem Edittext’s values located in it, and then print the results in the PDF. Thank you!!

FILE THAT GENERATES THE 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 INSTANCE PDF.AJVA WHEN CLICKING 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();
    }


}
  • Your question is confused, one more thing confuses me even more: does your code have two main classes? Why Javafx Creates a Main Class and a Controller.

  • hello Gustavo, I did not understand your remark, two classes with main?

  • I am trying to rescue filled values in an Edittext, located in the file that instantiates the pdf to be generated. And then present them in the generated pdf.

  • Its PDF class has a public Static void main(String[] args) and the javafx main class has another public Static void main(String[] args) { Launch(args); }

  • I understood, but it prevented me from accomplishing the enlightened?

  • From the point of view of programming is incorrect.

Show 1 more comment

1 answer

1

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

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’ll show you how to create a modal window in Javafx using FXML. First we will have to create a global variable for your Stage in the method start of its 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 Visualized 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 have no idea how to create an Act you can look at this pdf viewer. It would be interesting to pass the Generator image to the Viewer, or you can join the two in one class, it depends on you.

Finally the use would be this way:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.