0

我想使用网格窗格打印多个页面。数据来自 CSV 文件有人可以提出解决方案吗?这是我用来打印它的代码,它有一个网格窗格,它包含在一个窗格中,编写过程显示要打印的信息,然后通过单击 PRINT 按钮将信息发送到打印机最终 Printer selectedPrinter =打印机.getDefaultPrinter(); 诠释 R = 0; @FXML private void onBuildRpt(ActionEvent e) 抛出 IOException {

pane.setVisible(false);
paneRpt.setVisible(true);
gpRpt.setVisible(true);
gpRpt.getChildren().clear();
//gpRpt.gridLinesVisibleProperty().set(true);
gpRpt.setLayoutX(5);
gpRpt.setLayoutY(10);

R = 2;

Text txtH1 = new Text("Hospital Number");
GridPane.setHalignment(txtH1, HPos.CENTER);
txtH1.setFont(Font.font("Times New Roman", FontWeight.BLACK, FontPosture.REGULAR, 14));
gpRpt.add(txtH1, 0, 0);

Text txtH2 = new Text("First Name");
GridPane.setHalignment(txtH2, HPos.LEFT);
txtH2.setFont(Font.font("Times New Roman", FontWeight.BLACK,
FontPosture.REGULAR, 14));    
gpRpt.add(txtH2, 1, 0);

Text txtH3 = new Text("Last Name");
GridPane.setHalignment(txtH3, HPos.LEFT);
txtH3.setFont(Font.font("Times New Roman", FontWeight.BLACK,
FontPosture.REGULAR, 14));     
gpRpt.add(txtH3, 2, 0);

Text txtH4 = new Text("Room");
GridPane.setHalignment(txtH4, HPos.LEFT);
txtH4.setFont(Font.font("Times New Roman", FontWeight.BLACK, FontPosture.REGULAR, 14));
gpRpt.add(txtH4, 3, 0);

File file = new File("C:/A_Hospital/Patients.csv");
Path dirP = Paths.get(String.valueOf(file));
InputStream in = Files.newInputStream(dirP);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
//int H =list.size();
//System.out.println(H);
scan = new Scanner(reader);
scan.useDelimiter("\\s*,\\s*");

while (scan.hasNext()){
String hospnum = scan.next();
String fname = scan.next();
String lname = scan.next();
String roomnum = scan.next();

Text txt1 = new Text(hospnum);
GridPane.setHalignment(txt1, HPos.CENTER);
txt1.setFont(Font.font("Times New Roman", FontWeight.BLACK,
FontPosture.REGULAR, 14)); 
gpRpt.add(txt1, 0, R);

Text txt2 = new Text(fname + " ");
GridPane.setHalignment(txt2, HPos.LEFT);
txt2.setFont(Font.font("Times New Roman", FontWeight.BLACK, 
FontPosture.REGULAR, 14));
gpRpt.add(txt2 , 1, R);

Text txt3 = new Text(lname);
GridPane.setHalignment(txt3, HPos.LEFT);
txt3.setFont(Font.font("Times New Roman", FontWeight.BLACK, FontPosture.REGULAR, 14));
gpRpt.add(txt3, 2, R);

Text txt4 = new Text(roomnum);
GridPane.setHalignment(txt4, HPos.LEFT);
txt4.setFont(Font.font("Times New Roman", FontWeight.BLACK,
FontPosture.REGULAR, 14));
gpRpt.add(txt4, 3, R);

Text txtS0 = new Text(" ");
txtS0.prefHeight(5);
Text txtS1 = new Text(" ");
txtS1.prefHeight(5);
Text txtS2 = new Text(" ");
txtS2.prefHeight(5);
Text txtS3 = new Text(" ");
txtS3.prefHeight(5);
GridPane.setFillHeight(txtS0, true);
gpRpt.add(txtS0, 0, R+1);
GridPane.setFillHeight(txtS1, true);
gpRpt.add(txtS1, 1, R+1);
GridPane.setFillHeight(txtS2, true);
gpRpt.add(txtS2, 2, R+1);
GridPane.setFillHeight(txtS3, true);
gpRpt.add(txtS3, 3, R+1);

R = R + 2;
//System.out.println(R);//36\f
}

scan.close();
}

@FXML
public void onPrint(ActionEvent e){
//gpRpt.gridLinesVisibleProperty().set(true);
gpRpt.setStyle("-fx-background-color:transparent");
print(gpRpt, selectedPrinter);  
}

public void print(final Node node, Printer printer) { 
PrinterJob job = PrinterJob.createPrinterJob();
double lMargin = 0.25;
double rMargin = 0.25;
double tMargin = 0.25;
double bMargin = 0.25;
PageLayout pageLayout =
printer.createPageLayout(Paper.NA_LETTER,PageOrientation.PORTRAIT

,lMargin,rMargin,tMargin,bMargin);

JobSettings jobSettings = job.getJobSettings();
jobSettings.setPageLayout(pageLayout);

//System.out.println(jobSettings);
//boolean proceed = job.showPageSetupDialog(null);
boolean printed = job.printPage(node);
if (printed) {
// End the printer job
job.endJob();
}
pane.setVisible(true);
paneRpt.setVisible(false);

}
4

2 回答 2

1

你可以尝试这样的事情:

// Get the print page size:
final double  prnW = pageLayout.getPrintableWidth();
final double  prnH = pageLayout.getPrintableHeight();

// Work out how many pages across and down are needed (This code may not work?):
final int  pagesAcross = (int) Math.ceil( gridPane.getWidth() / prnW );
final int  pagesDown = (int) Math.ceil( gridPane.getHeight() / prnH );

/* Print pages down and then across like so:
   1    3
   2    4
   Swop the for loops around if you want to print pages across first and then down.
*/
for ( int pgCol = 0; pgCol < pagesAcross; pgCol++ )
{
    for ( int pgRow = 0; pgRow < pagesDown; pgRow++ )
    {
        gridPane.setTranslateX( -(prnW * pgCol) );
        gridPane.setTranslateY( -(prnH * pgRow) );
        job.printPage( pageLayout, gridPane );
    }
}

job.endJob();
于 2015-07-17T08:58:57.503 回答
0

@James_Duh 将两个文本字段添加到您的第二个窗格(paneRpt)。然后在 Scene Builder 中将 GridPane (gpRpt) 包装在 ScrollPane 中,并向 GridPane 列零添加一个新列,该列只需要 PerfWidth 25。我已经重写了您的 onBuildRpt 方法,因此 GridPane 反映了 CSV 文件中的所有数据。GridPane 现在是可滚动的,并且每条记录都有一个行号。这是有趣的开始,您现在可以选择开始行号和结束行号进行打印。我无法发出换页,因此打印仍然是一个多步骤过程。实际的打印设计由打印按钮调用的新方法 printRpt 管理。我在下面发布了相关代码。

@FXML
private void onBuildRpt(ActionEvent e) throws IOException {

if(list.size()==0) {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Information");
alert.setHeaderText("");
alert.setContentText("No Data for Report\n"+
"Select File then Alt + A to Add New Data");
alert.showAndWait();
return;
}

int row = 0;
togp = new ArrayList<>();
pane.setVisible(false);
paneRpt.setVisible(true);
gpRpt.setVisible(true);
gpRpt.getChildren().clear();
//gpRpt.gridLinesVisibleProperty().set(true);
//gpRpt.setLayoutX(20);
//gpRpt.setLayoutY(10);

int R = 2;

File file = new File("C:/A_Hospital/Patients.csv");
Path dirP = Paths.get(String.valueOf(file));
InputStream in = Files.newInputStream(dirP);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
scan = new Scanner(reader);
scan.useDelimiter("\\s*,\\s*");


while (scan.hasNext()){
String hospnum = scan.next();
String fname = scan.next();
String lname = scan.next();
String roomnum = scan.next();

togp.add(hospnum);
togp.add(fname);
togp.add(lname);
togp.add(roomnum);
}

scan.close();

for(int p = 0; p < togp.size(); p++){

row = row + 1;

Text txt0 = new Text(""+row);
//row number
GridPane.setHalignment(txt0, HPos.CENTER);
txt0.setFont(Font.font("Times New Roman", FontWeight.BLACK,
FontPosture.REGULAR, 14));`
gpRpt.add(txt0, 0, R);

Text txt1 = new Text(togp.get(p));
//hospnum
GridPane.setHalignment(txt1, HPos.CENTER);
txt1.setFont(Font.font("Times New Roman", FontWeight.BLACK,
FontPosture.REGULAR, 14));`
gpRpt.add(txt1, 1, R);

Text txt2 = new Text(togp.get(p=p+1));
//fname
GridPane.setHalignment(txt2, HPos.LEFT);
txt2.setFont(Font.font("Times New Roman", FontWeight.BLACK,
FontPosture.REGULAR, 14));`
gpRpt.add(txt2 , 2, R);

Text txt3 = new Text(togp.get(p=p+1));
//lname
GridPane.setHalignment(txt3, HPos.LEFT);
txt3.setFont(Font.font("Times New Roman", FontWeight.BLACK,
FontPosture.REGULAR, 14));`
gpRpt.add(txt3, 3, R);

Text txt4 = new Text(togp.get(p=p+1));
//roomnum
GridPane.setHalignment(txt4, HPos.LEFT);
txt4.setFont(Font.font("Times New Roman", FontWeight.BLACK,
`FontPosture.REGULAR, 14));`
gpRpt.add(txt4, 4, R);

Text txtS0 = new Text(" ");
txtS0.prefHeight(5);
Text txtS1 = new Text(" ");
txtS1.prefHeight(5);
Text txtS2 = new Text(" ");
txtS2.prefHeight(5);
Text txtS3 = new Text(" ");
txtS3.prefHeight(5);
GridPane.setFillHeight(txtS0, true);
gpRpt.add(txtS0, 0, R+1);
GridPane.setFillHeight(txtS1, true);
gpRpt.add(txtS1, 1, R+1);
GridPane.setFillHeight(txtS2, true);
gpRpt.add(txtS2, 2, R+1);
GridPane.setFillHeight(txtS3, true);
gpRpt.add(txtS3, 3, R+1);

R = R + 2;
}
txfStartRow.requestFocus();
}
@FXML
private void printRpt() {
//This method just clears the gpRpt and
//reloads it with selected row number range 
if(txfStartRow.getText().isEmpty() || txfEndRow.getText().isEmpty()) {  


Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Information");
alert.setHeaderText("");
alert.setContentText("No Data Entered\n"+
"Enter Start Row Number and End Row Number");
alert.showAndWait();

if(txfStartRow.getText().isEmpty()) {
    txfStartRow.requestFocus();
return;
}
if(txfEndRow.getText().isEmpty()) {
    txfEndRow.requestFocus();
return;
}
return;
}

String regX1 = ("[0-9\\^W]*");
String W1 = String.valueOf(txfStartRow.getText().trim());
if(!W1.matches(regX1) || Integer.valueOf(W1) > togp.size()/4) {

Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Information");
alert.setHeaderText("");
alert.setContentText("Numeric Values Only\n"+
"\nAcceptable Rows 1 to "+ togp.size()/4 +" Can Be Entered");
alert.showAndWait();

txfStartRow.requestFocus();     
return;
}

String regX2 = ("[0-9\\^W]*");
String W2 = String.valueOf(txfEndRow.getText().trim());
if(!W2.matches(regX2) || Integer.valueOf(W2) > togp.size()/4) {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Information");
alert.setHeaderText("");
alert.setContentText("Numeric Values Only\n"+
"\nAcceptable Rows 1 to "+ togp.size()/4 +" Can Be Entered");
alert.showAndWait();
txfEndRow.requestFocus();


return;
}

if(Integer.valueOf(txfStartRow.getText()) <= 0 || 
Integer.valueOf(txfEndRow.getText()) <=0) {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Information");
alert.setHeaderText("");
alert.setContentText("Data Entry Error\n"+
"Start & End Rows Can NOT be zero");
alert.showAndWait();
txfStartRow.setText("");
txfEndRow.setText("");
txfStartRow.requestFocus();
return;
}

if(Integer.valueOf(txfEndRow.getText()) - Integer.valueOf
(txfStartRow.getText()) > 19) {

Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Information");
alert.setHeaderText("");
alert.setContentText("Printing Issue\n"+
"Max Report Size 20 Rows");
alert.showAndWait();
//txfStartRow.setText("");
//txfEndRow.setText("");
txfStartRow.requestFocus();
return;
}

if(Integer.valueOf(txfStartRow.getText()) > 
Integer.valueOf(txfEndRow.getText())) {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Information");
alert.setHeaderText("");
alert.setContentText("Data Entry Error\n"+
"Start Row Greater than End Row");
alert.showAndWait();
//txfStartRow.setText("");
//txfEndRow.setText("");
txfStartRow.requestFocus();
return;
}

//Fancy Math for Row Calculation to find togp index
//This should work no matter the size of the togp ArrayList
int SR = Integer.valueOf(txfStartRow.getText());
SR = SR * 3 + SR - 4;
int ER = Integer.valueOf(txfEndRow.getText());
ER = ER * 3 + ER - 3;

gpRpt.getChildren().clear();
int row = 0;
int R = 2;

Text txtH0 = new Text("Row");
GridPane.setHalignment(txtH0, HPos.CENTER);
txtH0.setFont(Font.font("Times New Roman", FontWeight.BLACK,
FontPosture.REGULAR, 14));
gpRpt.add(txtH0, 0, 0);

Text txtH1 = new Text("Hospital ID");
GridPane.setHalignment(txtH1, HPos.CENTER);
txtH1.setFont(Font.font("Times New Roman", FontWeight.BLACK,
FontPosture.REGULAR, 14));
gpRpt.add(txtH1, 1, 0);

Text txtH2 = new Text("First Name");
GridPane.setHalignment(txtH2, HPos.LEFT);
txtH2.setFont(Font.font("Times New Roman", FontWeight.BLACK,
FontPosture.REGULAR, 14));
gpRpt.add(txtH2, 2, 0);

Text txtH3 = new Text("Last Name");
GridPane.setHalignment(txtH3, HPos.LEFT);
txtH3.setFont(Font.font("Times New Roman", FontWeight.BLACK,
FontPosture.REGULAR, 14));
gpRpt.add(txtH3, 3, 0);

Text txtH4 = new Text("Room");
GridPane.setHalignment(txtH4, HPos.LEFT);
txtH4.setFont(Font.font("Times New Roman", FontWeight.BLACK,
FontPosture.REGULAR, 14));`
gpRpt.add(txtH4, 4, 0);

for(int p = SR; p < ER; p++){

row = row + 1;

Text txt0 = new Text(""+row);
//row #
GridPane.setHalignment(txt0, HPos.CENTER);
txt0.setFont(Font.font("Times New Roman", FontWeight.BLACK,
FontPosture.REGULAR, 14));
gpRpt.add(txt0, 0, R);

Text txt1 = new Text(togp.get(p));
//hospnum
GridPane.setHalignment(txt1, HPos.CENTER);
txt1.setFont(Font.font("Times New Roman", FontWeight.BLACK,
FontPosture.REGULAR, 14));
gpRpt.add(txt1, 1, R);

Text txt2 = new Text(togp.get(p=p+1));
//fname
GridPane.setHalignment(txt2, HPos.LEFT);
txt2.setFont(Font.font("Times New Roman", FontWeight.BLACK,
FontPosture.REGULAR, 14));
gpRpt.add(txt2 , 2, R);

Text txt3 = new Text(togp.get(p=p+1));
//lname
GridPane.setHalignment(txt3, HPos.LEFT);
txt3.setFont(Font.font("Times New Roman", FontWeight.BLACK, FontPosture.REGULAR, 14));
gpRpt.add(txt3, 3, R);

Text txt4 = new Text(togp.get(p=p+1));
//roomnum
GridPane.setHalignment(txt4, HPos.LEFT);
txt4.setFont(Font.font("Times New Roman", FontWeight.BLACK,
FontPosture.REGULAR, 14));
gpRpt.add(txt4, 4, R);

Text txtS0 = new Text(" ");
txtS0.prefHeight(5);
Text txtS1 = new Text(" ");
txtS1.prefHeight(5);
Text txtS2 = new Text(" ");
txtS2.prefHeight(5);
Text txtS3 = new Text(" ");
txtS3.prefHeight(5);
GridPane.setFillHeight(txtS0, true);
gpRpt.add(txtS0, 0, R+1);
GridPane.setFillHeight(txtS1, true);
gpRpt.add(txtS1, 1, R+1);
GridPane.setFillHeight(txtS2, true);
gpRpt.add(txtS2, 2, R+1);
GridPane.setFillHeight(txtS3, true);
gpRpt.add(txtS3, 3, R+1);

R = R + 2;
}
onPrint();
}

public void onPrint(){  
//gpRpt.gridLinesVisibleProperty().set(true);
gpRpt.setStyle("-fx-background-color:transparent");
print(gpRpt, selectedPrinter);
}
于 2015-07-03T18:49:14.840 回答