javafx - JavaFX 打印机页面(布局,节点)不打印 TextFlow
问题描述
我正在尝试打印格式化的文本。这是我的代码:
public class ProvaTextFlow {
public static void main(String[] args)
{
ProvaTextFlow p = new ProvaTextFlow() ;
Node tf = p.creaTesto();
p.stampa(tf);
}
public Node creaTesto( )
{
String s1 = "Dear Mom," + "\n"
+ " I am going out dancing tonight with :" + "\n\n\n" ;
String f1 = " (girl) friend 1" + "\n" +
" friend 2" + "\n" +
" (girl) friend 3" + "\n\n\n\n" ;
String closing = "don't wait for me" + "\n\n\n\n"
+ "bye" + "\n\n" ;
Text t1 = new Text (s1);
Text t2 = new Text (f1);
Text t3 = new Text (closing) ;
t1.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
t2.setFont(Font.font("Tahoma", FontWeight.BOLD, FontPosture.ITALIC, 20));
t3.setFont(Font.font("Tahoma", FontPosture.ITALIC, 20));
TextFlow textFlow = new TextFlow() ; ;
try {
//Setting the width
textFlow.setPrefSize(2480, 3508); // 2480 X 3508 = A4(300)
//Setting the line spacing
textFlow.setLineSpacing(5.0);
//Retrieving the observable list of the TextFlow Pane
textFlow.getChildren().addAll(t1, t2, t3);
// method to extract out of a TextFlow the String text.
//
//////////////////////
// StringBuilder sb = new StringBuilder();
// for (Node node : textFlow.getChildren())
// {
// if (node instanceof Text)
// {
// sb.append(((Text) node).getText());
// }
// }
// String fullText = sb.toString();
/////////////////////////
// System.out.println("fulltext => " + fullText);
// this (when uncommented) correctly prints the unformatted text } catch (Exception e) {
System.out.println(e);
}
return textFlow;
}
public void stampa (Node n2p)
{
Printer printer = Printer.getDefaultPrinter();
javafx.print.PageLayout pageLayout = printer.createPageLayout
(
Paper.A4,
PageOrientation.PORTRAIT,
Printer.MarginType.DEFAULT
);
PrinterJob job = PrinterJob.createPrinterJob();
boolean result = job.printPage(pageLayout, n2p);
System.out.println(" print result => " + result);// this gives true
job.endJob();
}
}
此代码编译运行正确,job.printPage()
返回true
,打印机开始打印,但 A4 纸完全空白。基本上,我构建了一个TextFlow
,其中三个字符串以不同的方式格式化,然后我将这个 ( TextFlow
)传递Node
给该Stampa
方法。如果我打印 a TableView
,则完全没有问题。我错过了什么吗?谢谢
解决方案
推荐阅读
- python - python WooCommerce REST API 如何使用变体 id 检索产品的父 id?
- javascript - 在浏览器中格式化数学表达式
- c++ - 我可以加载 header(.h) 文件以在运行时获取结构吗?(C++)
- java - 获取服务器端请求伪造 (SSRF) (CWE ID 918) restTemplate.getForEntity
- google-apps-script - Google Sheet Script 上的 Keycloak 身份验证
- python - 使用嵌套 for 循环从另一个文件中的一个文件中搜索特定值
- function - 如何通过值而不是通过引用传递变量作为飞镖函数的参数?
- azure - 是否可以禁用 kudu scm
- python - 如果 xldate < 0.00:TypeError:使用 xldate 读取时,“str”和“float”的实例之间不支持“<”
- python - 在 TableColumn 内包装文本(Python/Bokeh:HTMLTemplateFormatter)