I am try to print text from text Area using JavaFX Printer(Like in notepad printing feature). I want to print that text without textArea background styles. So I try it with another text area. I wrap its text for avoid x bar overflow. But, I can't find a way to avoid y bar overflow. also I can't hide the background. can some one help or give a subjections for this or any other way. This is my current code. Also I try to hide scroll bars using css. but it did't support. I want to print only the text.
TextArea txtArea = new TextArea("This is some long text..");
public void printText() {
PrinterJob job = PrinterJob.createPrinterJob();
if(job == null) {
System.out.println("Error");
return;
}
boolean proseed = job.showPrintDialog(root.getScene().getWindow());
JobSettings ss1 = job.getJobSettings();
PageLayout pageLayout1 = ss1.getPageLayout();
double pgW1 = pageLayout1.getPrintableWidth();
double pgH1 = pageLayout1.getPrintableHeight();
TextArea tempTxtArea = new TextArea(txtArea.getText());
tempTxtArea.setPrefSize(pgW1, pgH1);
tempTxtArea.setWrapText(true);
tempTxtArea.setId("tempScroolBar");
if(proseed) {
boolean printed = job.printPage(tempTxtArea);
if (printed) {
job.endJob();
} else {
System.out.println("Fail");
}
}
}
this is my css file
#tempScroolBar > .scroll-pane {
-fx-vbar-policy: never;
-fx-hbar-policy: never;
}
Update
I updated my code. But now I have another problem. I changed TextArea to Label and to print all the content(avoid y bar overflow) I want to get number of the pages. But it doesn't return height(always return 0). Then I refer https://stackoverflow.com/a/21075734/13862869 and I set It to the Scene. Without add the label to the new Scene it doesn't give the width. But when I set the label to the Scene only three '.' are print in my document. Can some one help me to solve this...
public void printText() {
PrinterJob job = PrinterJob.createPrinterJob();
if(job == null) {
System.out.println("Error");
return;
}
boolean proseed = job.showPrintDialog(root.getScene().getWindow());
JobSettings ss1 = job.getJobSettings();
PageLayout pageLayout1 = ss1.getPageLayout();
double pgW1 = pageLayout1.getPrintableWidth();
double pgH1 = pageLayout1.getPrintableHeight();
HBox h = new HBox();
Label tempText = new Label();
tempText.setPrefWidth(pgW1);
tempText.setWrapText(true);
tempText.setText(txtArea.getText());
h.getChildren().add(tempText);
Scene s = new Scene(h); // when i remove this line again text can print
tempText.applyCss();
double fullLabelHeight = tempText.prefHeight(-1);
int numberOfPages = (int) Math.ceil(fullLabelHeight/ pgH1);
if(proseed) {
job.printPage(tempText);
job.endJob();
}
}