2

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;
}

This is the current look of my output result

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();
    }
}
  • 2
    See if the answer [here](https://stackoverflow.com/questions/31918959/javafx-print-tableview-on-multiple-pages/37118619#37118619) helps. – SedJ601 Feb 03 '22 at 18:36
  • 1
    Instead of trying to remove the GUI decorations through styling. create a new scene. Get the text from the TextArea and put it in a new Text or Label node in the new scene, then print the new scene. You will also need to deal with pagination and scaling, which Sedrick’s comment may help with. – jewelsea Feb 03 '22 at 22:27
  • Thank you for both of you. I think, I will be able to do this with those ideas and answers. – Kavishka Madhushan Feb 04 '22 at 12:57

0 Answers0