BIRT生成的PDF文档中,如果要确保换行符功能正常,则应在生成文本的同时指定正确的字体和字号。以下是示例代码:
//初始化PDF文档
PDFDocument pdfDoc = new PDFDocument(pdOutputStream);
//设置字体和字号
PDFont font = PDType1Font.HELVETICA;
float fontSize = 12.0f;
//创建新的页面
PDPage page = new PDPage();
pdfDoc.addPage(page);
//创建新的内容流
PDPageContentStream contentStream = new PDPageContentStream(pdfDoc, page);
//指定字体和字号
contentStream.setFont(font, fontSize);
//写入文本,处理换行
String text = "This is a test string\nwith a line break.";
List lines = Arrays.asList(text.split("\n"));
for (String line : lines) {
contentStream.showText(line);
contentStream.newLine();
}
contentStream.close();
//保存文档
pdfDoc.save(outputStream);