I am trying to save a TextArea as PDF using PDFBox with Java 8. The file gets saved fine and open fine as well. But the file stores the TextArea as one line. I tried to split the TextArea and loop through it with drawString on each split but it's still not working.
Code:
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();
}
}