Molto probabilmente sbagli qualcosa nella costruzione del documento quel codice che hai postato è formalmente corretto.
Di seguito ti posto la classe con cui ho eseguito il test
codice:
public class Test {
public PDDocument createDocument() throws Exception {
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
PDFont font = PDType1Font.HELVETICA_BOLD;
PDPageContentStream contentStream = new PDPageContentStream(document,
page);
contentStream.beginText();
contentStream.setFont(font, 12);
contentStream.moveTextPositionByAmount(100, 700);
contentStream.drawString("Hello World");
contentStream.endText();
contentStream.close();
return document;
}
public byte[] getBytes(PDDocument document) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
document.save(out);
byte[] bytes = out.toByteArray();
return bytes;
}
public static void main(String[] args) throws Exception {
Test test = new Test();
PDDocument document = test.createDocument();
byte[] bytes = test.getBytes(document);
document.close();
System.out.println("La lunghezza della array è "+bytes.length);
}
}