我正在尝试使用带有Java 8的PDFBox将TextArea另存为PDF。该文件也可以保存并且打开正常。但该文件将TextArea存储为一行。我尝试拆分TextArea并在每次拆分时使用drawString循环遍历它,但它仍然无法正常工作。
代码:
public void saveOutput(MouseEvent e) throws IOException, COSVisitorException {
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("PDF file(*.pdf)", " *.pdf");
fileChooser.getExtensionFilters().add(extFilter);
File savedFile = fileChooser.showSaveDialog(Controller.stage);
if (savedFile != null) {
PDDocument doc = null;
PDPage page = null;
doc = new PDDocument();
page = new PDPage();
doc.addPage(page);
PDFont font = PDType1Font.HELVETICA_BOLD;
PDPageContentStream content = new PDPageContentStream(doc, page);
content.beginText();
content.setFont(font, 8);
content.moveTextPositionByAmount(100, 700);
for (String line : d1CheckedOut.getText().split("\\R+")) {
System.out.println(line+"new line");
content.drawString(line+"\n");
}
content.endText();
content.close();
doc.save(savedFile);
doc.close();
}
}