I am trying to write content into a PDF file. I have written the code
public ByteArrayOutputStream createPDF(String text) throws IOException, COSVisitor
You made two mistakes:
You have closed the contentStream
after saving the document instead of before.
You haven't set a font.
Code that works for me (exception handling removed):
PDDocument document;
PDPage page;
PDPageContentStream contentStream;
document = new PDDocument();
page = new PDPage();
document.addPage(page);
contentStream = new PDPageContentStream(document, page);
contentStream.setFont(PDType1Font.COURIER, 10);
contentStream.beginText();
contentStream.moveTextPositionByAmount(100, 700);
contentStream.drawString("Hello World Hello World Hello World Hello World Hello World");
contentStream.endText();
contentStream.close();
document.save(....);
document.close();