2017-10-23 2 views
-1

사용자가 필드 (이름, 이름, 번호, 날짜 1, 날짜 2)를 채우고 3 개의 명제 중 하나 인 man, woman, child 중에서 하나를 선택하는 양식을 만들었습니다. 나는 사용자 응답을 저장하여 (데이터베이스에 저장) pdf 파일에 통합 할 수 있었으면 좋겠다.javafx를 사용하여 텍스트 필드에서 pdf를 저장하고 생성하는 방법

그러나 필자는 내 PDF 기능을 수행 할 수 없다. 어떻게해야합니까? 조언이나 예가 있습니까? 사용자 입력란에 응답하기 전에 pdf 템플릿을 생성해야합니까? 나는 'itextpdf'로 무언가를하려했지만 성공하지 못했다. 다른 전략?

도와주세요!

데이터 파일

public class Data { 

    private String name; 
    private String surname; 
    private Integer numb; 
    private String date1; 
    private String date2; 

    public Patient(String name, String surname, Integer numb, String date1, String date2) { 
     super(); 
     this.name = name; 
     this.surname = surname; 
     this.numb = numb; 
     this.date1 = date1; 
     this.date2 = date2; 
    } 

    public String getName() { 
     return name; 
    } 

    public String getSurname() { 
     return surname; 
    } 

    public Integer getNumb() { 
     return numb; 
    } 

    public String getDate1() { 
     return date1; 
    } 

    public String getDate2() { 
     return date2; 
    } 

} 

DataManager에 파일

import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.util.ArrayList; 

import main; 
import models.data; 

public class DataManager { 


    private Main main; 

    public DataManager(Main main) { 
     super(); 
     this.main = main; 
    } 

    public void addData(data p) { 
     String query = "INSERT INTO Data(name, surname, numb, date1, date2) VALUES (" 
       + "\"" + p.getName() + "\", " 
       + "\"" + p.getSurname() + "\", " 
       + "\"" + p.getNumb() + "\", " 
       + "\"" + p.getDate1() + "\", " 
       + "\"" + p.getDate2() + "\");"; 
     try { 
      this.main.getDatabase().connect(); 
      this.main.getDatabase().updateValue(query); 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 
     finally { 
      this.main.getDatabase().disconnect(); 
     } 
    } 

    public ArrayList<Data> getData() { 
     ArrayList<Data> data = new ArrayList<Data>(); 

     String query = "SELECT name, surname, numb, Date1, Date2 FROM Data ORDER BY name"; 
     try { 
      this.main.getDatabase().connect(); 
      ResultSet rslt = this.main.getDatabase().getResultOf(query.toString()); 
      while (rslt.next()) { 
       data.add(new Data(rslt.getString("name"), rslt.getString("surname"), rslt.getInt("numb"),rslt.getString("Date1"),rslt.getString("Date2"))); 
      } 
      rslt.close(); 
     } catch (SQLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     finally { 
      this.main.getDatabase().disconnect(); 
     } 
     return data; 
    } 

fichier 옵션

public class Options { 


    public enum Ref { 
     man("man"), 
     woman("woman"), 
     child("child"); 

     private final String refName; 

     Ref(String goalName){ 
      this.refName = goalName; 
     } 

     @Override 
     public String toString() { 
      return refName; 
     } 
    } 
    private Ref ref; 

    public Ref getRef() { 
     return ref; 
    } 


} 

FormController에 파일

012 : 여기

내 코드입니다 3,516,
import Main; 
import models.options; 



public class FormController{ 

    Map<String,Ref> refsMap = new HashMap<String,Ref>(); 

    private Main main; 

    private JFrame frame; 


    TextArea ta = new TextArea(); 

    public void setMain(Main main) { 
     this.main = main; 
    } 

    @FXML 
    private TextField tfCharName; 

    @FXML 
    private TextField tfCharSurname; 

    @FXML 
    private TextField tfNum; 

    @FXML 
    private TextField tfDate1; 

    @FXML 
    private TextField tfDate2; 

    @FXML 
    private ChoiceBox<String> cbGoal; 

    @FXML 
    private Button bChoose; 

    @FXML 
    private Button bFinish; 

    @FXML 
    private String eFinish; 



    @FXML public void initialize(){ 

    Ref[] refs = Ref.values(); 
    for (Ref ref : refs) { 
     refsMap.put(ref.toString(), ref); 
    } 

    cbGoal.setItems(FXCollections.observableArrayList(refsMap.keySet())); 

    } 

    @FXML 
    public void handleButtonAction(ActionEvent event) throws IOException{ 
     Button pressedButton = (Button) event.getSource(); 

     if(pressedButton==bChoose){ 
      FileChooser fileChooser = new FileChooser(); 
      File selectedFile = fileChooser.showOpenDialog(null); 
      if (selectedFile != null) { 



        try { 
         ta.setText(new String(Files.readAllBytes(selectedFile.toPath()))); 
        } catch (IOException ex) {} 
      } 

     } 


     else if(pressedButton==bFinish){ 

      //fields 
      String name = tfCharName.getText().trim(); 
      String surname = tfCharSurname.getText().trim(); 
      String numb = tfNum.toString().trim(); 
      String date1 = tfDate1.getText().trim(); 
      String date2 = tfDate2.getText().trim(); 

//     AddToReport field = new AddToReport (name+" "+surname+" "+number+" "+date1+" "+date2); 
//     AddToReport field = new AddToReport(); 
//     this.dataManager.updateData(new Data(this.name, surname, number, date1, date2)); 
//     Button pressedButton = (Button) event.getSource(); 
        Stage mainStage = (Stage) pressedButton.getScene().getWindow(); 
        FXMLLoader loader = new FXMLLoader(); 
        loader.setLocation(MainApp.class.getResource("../view/following.fxml")); 

        AnchorPane page = (AnchorPane) loader.load(); 
        Stage dialogStage = new Stage(); 
        dialogStage.setTitle("suite"); 
        dialogStage.initModality(Modality.WINDOW_MODAL); 
        Window primaryStage = null; 
        dialogStage.initOwner(primaryStage); 
        Scene scene = new Scene(page); 
        dialogStage.setScene(scene); 

        dialogStage.show(); 

    } 
    } 
} 

form.fxml

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.chart.*?> 
<?import java.lang.*?> 
<?import javafx.scene.layout.*?> 
<?import javafx.scene.layout.AnchorPane?> 
<?import javafx.geometry.Insets?> 
<?import javafx.scene.control.Button?> 
<?import javafx.scene.control.ChoiceBox?> 
<?import javafx.scene.control.Label?> 
<?import javafx.scene.control.RadioButton?> 
<?import javafx.scene.control.ScrollPane?> 
<?import javafx.scene.control.Slider?> 
<?import javafx.scene.control.TextField?> 
<?import javafx.scene.control.ToggleGroup?> 
<?import javafx.scene.image.ImageView?> 
<?import javafx.scene.layout.HBox?> 
<?import javafx.scene.layout.VBox?> 
<?import javafx.scene.text.Font?> 


<VBox alignment="TOP_CENTER" prefHeight="550.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="view.FormController"> 

    <children> 
     <Label alignment="CENTER" contentDisplay="CENTER" text="New analysis" textAlignment="CENTER" underline="true"> 
      <font> 
       <Font size="29.0" /> 
      </font> 
      <VBox.margin> 
       <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /> 
      </VBox.margin> 
     </Label> 
     <ScrollPane> 
     <content> 
      <VBox alignment="BOTTOM_CENTER"> 
       <children> 
        <HBox fillHeight="false" VBox.vgrow="ALWAYS"> 
         <VBox.margin> 
          <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /> 
         </VBox.margin> 
         <children> 
          <Label text="Enter patient name and surname" /> 
          <TextField fx:id="tfCharSurname" promptText="Name"> 
           <HBox.margin> 
            <Insets left="5.0" right="5.0" /> 
           </HBox.margin> 
          </TextField> 
          <TextField fx:id="tfCharSurname" promptText="Surname" /> 
         </children> 
         <padding> 
          <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /> 
         </padding> 
        </HBox> 

        <HBox fillHeight="false" VBox.vgrow="ALWAYS"> 
         <VBox.margin> 
          <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /> 
         </VBox.margin> 
          <Label text="number" /> 
          <TextField fx:id="tfNum" promptText="number"> 
           <HBox.margin> 
            <Insets left="5.0" right="5.0" /> 
           </HBox.margin> 
          </TextField> 
         <padding> 
          <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /> 
         </padding> 
        </HBox> 
        <HBox fillHeight="false" VBox.vgrow="ALWAYS"> 
         <VBox.margin> 
          <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /> 
         </VBox.margin> 
          <Label text="date1" /> 
          <TextField fx:id="tfDate1" promptText="date1"> 
           <HBox.margin> 
            <Insets left="5.0" right="5.0" /> 
           </HBox.margin> 
          </TextField> 
         <padding> 
          <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /> 
         </padding> 
        </HBox> 
        <HBox fillHeight="false" VBox.vgrow="ALWAYS"> 
         <VBox.margin> 
          <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /> 
         </VBox.margin> 
          <Label text="date2" /> 
          <TextField fx:id="tfDate2" promptText="date2"> 
           <HBox.margin> 
            <Insets left="5.0" right="5.0" /> 
           </HBox.margin> 
          </TextField> 
         <padding> 
          <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /> 
         </padding> 
        </HBox> 
        <HBox fillHeight="false" VBox.vgrow="ALWAYS"> 
         <children> 
          <Label text="Choisir"> 
           <HBox.margin> 
            <Insets right="10.0" /> 
           </HBox.margin> 
          </Label> 
          <ChoiceBox fx:id="cbGoal" prefWidth="150.0" /> 
         </children> 
         <VBox.margin> 
          <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /> 
         </VBox.margin> 
         <padding> 
          <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /> 
         </padding> 
        </HBox> 
        <HBox fillHeight="false" VBox.vgrow="ALWAYS"> 
           <children> 
          <Label text="Choose input file"> 
           <HBox.margin> 
            <Insets right="10.0" /> 
           </HBox.margin> 
          </Label> 
        <Button fx:id="bChoose" alignment="CENTER" mnemonicParsing="false" onAction="#handleButtonAction" text="choose"> 
        </Button> 
         </children> 
         <VBox.margin> 
          <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /> 
         </VBox.margin> 
         <padding> 
          <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /> 
         </padding> 
        </HBox> 

       </children> 
       <padding> 
        <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /> 
       </padding> 
      </VBox> 
     </content> 
     </ScrollPane> 

        <Button fx:id="bFinish" alignment="CENTER" mnemonicParsing="false" onAction="#handleButtonAction" text="suite"> 
     <VBox.margin> 
      <Insets top="10.0" /> 
     </VBox.margin> 
     </Button> 
     <Label fx:id="eFinish" alignment="CENTER" textFill="#ee0404"> 
     <VBox.margin> 
      <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /> 
     </VBox.margin> 
     </Label> 
    </children> 
</VBox> 
+0

안녕하세요. https://stackoverflow.com/help/how-to-ask를 읽어보십시오. 지금까지 뭐 해봤 어? –

답변

0

당신은 PDF 파일의 생성을위한 iText를 도구를 사용할 수 있습니다. 당신은 그것에 대해 확신을 가져야합니다. 자바 프로그래밍에서 itext pdf 도구를 사용하는 것에 대한 많은 예제가 있습니다.

이미 데이터를 PDF로 표시하려면 GeneratePdf라는 클래스를 만들어 사용하십시오. 필요한 단락, 표, 셀에 데이터를 제공하면됩니다.

예 : here.

+0

iText는 무료가 아닙니다. – JKostikiadis

+1

그 문장은 거짓입니다. iText 웹 사이트 (https://itextpdf.com/agpl)에서 직접 읽을 수 있듯이 iText는 무료 소프트웨어 사용권 계약에 명시된 조건을 따르는 한 무료로 사용할 수 있습니다.이 특별한 경우 Affero 일반 공중 사용권 계약 (AGPL). AGPL 약관에 구속되기를 원치 않으시면 iText 비용 만 지불하면됩니다. –

+0

사실 당신 말이 맞습니다. 저는 '자유'라는 말을 잘못 사용합니다. 많은 사람들은 iText가 라이센스에 대한 어떠한 제한도없이 무료로 완성되어 상업적 용도로 사용하는 경향이 있다고 생각한다. 그래서 나는 그것이 '자유'가 아니라고 말했다. 어쨌든 그것을 지적 해 주셔서 감사합니다. – JKostikiadis

관련 문제