0

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();
           }
    }
Moe
  • 1,427
  • 4
  • 34
  • 54
  • 2
    use itext or pdfbox .you can't open because it's not a real pdf file. – Madhawa Priyashantha Aug 22 '15 at 09:22
  • Thanks for the heads up, I updated my question with some progress. Take a look and let me know your thoughts. Thanks again!!! – Moe Aug 22 '15 at 14:33
  • 1
    read this question .it may help you http://stackoverflow.com/questions/7598099/how-to-insert-a-linefeed-with-pdfbox-drawstring – Madhawa Priyashantha Aug 22 '15 at 14:42
  • 2
    possible duplicate of [How to generate multiple lines in PDF using Apache pdfbox](http://stackoverflow.com/questions/19635275/how-to-generate-multiple-lines-in-pdf-using-apache-pdfbox) – mkl Aug 22 '15 at 16:54
  • This solution talks about breaking the text to fit in one page. What I am looking is to write what I have in the TextArea in the same matter in the PDF file. So I am looking for a way to copy/convert my string to pdf. – Moe Aug 23 '15 at 07:14
  • One possibility is to just change the media box so that you just see the area you're interested: https://stackoverflow.com/questions/30573931/pddocument-cant-add-list-of-pdpage-with-addpage – Tilman Hausherr Aug 23 '15 at 12:17

0 Answers0