一旦第1页已满,我试图将内容推送到第2页,但是目前我的代码仅写在第1页上,并且无法自动创建第二页,从而丢失了内容。
private static void insertStringEmail1(PDDocument doc, PDPage page, PDFont font,
float fontSize, float statVarX, float statVarY, String text) throws IOException {
PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, true, true);
PDPage page1 = new PDPage(PDPage.PAGE_SIZE_A4);
PDPageContentStream contentStream1 = new PDPageContentStream(doc, page);
PDFont pdfFont = PDType1Font.COURIER;
PDFont fontBold = PDType1Font.TIMES_BOLD;
float leading = 1.5f * fontSize;
PDRectangle mediabox = page.getMediaBox();
float margin = 40;
float width = mediabox.getWidth() - 2 * margin;
float height = mediabox.getHeight() - 2 * margin;
float startX = mediabox.getLowerLeftX() + margin - statVarX;
float startY = mediabox.getUpperRightY() - margin - statVarY;
float heightcounter = startY;
List<String> lines = new ArrayList<String>();
int lastSpace = -1;
String bold;
String[] arrOfStr;
text = text.replace("<BR><BR>", "<BR>");
System.out.println(text);
arrOfStr = text.split("<BR>");
for (String a1 : arrOfStr) {
String strRegEx = "<[^>]*>";
String a = a1.replaceAll(strRegEx, " ");
while (a.length() > 0) {
int spaceIndex = a.indexOf(' ', lastSpace + 1);
if (spaceIndex < 0)
spaceIndex = a.length();
String subString = a.substring(0, spaceIndex);
float size = fontSize * pdfFont.getStringWidth(subString) / 1000;
//System.out.printf("'%s' - %f of %f\n", subString, size, width);
if (size > width) {
if (lastSpace < 0)
lastSpace = spaceIndex;
subString = a.substring(0, lastSpace);
lines.add(subString);
a = a.substring(lastSpace).trim();
// System.out.printf("'%s' is line\n", subString);
lastSpace = -1;
} else if (spaceIndex == a.length()) {
lines.add(a);
//System.out.printf("'%s' is line\n", text);
a = "";
} else {
lastSpace = spaceIndex;
}
}
lines.add("");
}
contentStream.beginText();
contentStream.setFont(pdfFont, fontSize);
contentStream.moveTextPositionByAmount(startX, startY);
for (String line : lines) {
//String parsedline = line.replaceAll(strRegEx, " ");
line = line.trim();
float charSpacing = 0;
if (line.length() > 1) {
float size = fontSize * pdfFont.getStringWidth(line) / 1000;
float free = width - size;
if (free > 0) {
charSpacing = free / (line.length() - 1);
}
}
contentStream.drawString(line);
contentStream.moveTextPositionByAmount(0, -leading);
heightcounter++;
}//this part is added for page 2
if (heightcounter > height) {
contentStream.endText();
contentStream.close();
page = new PDPage();
doc.addPage(page);
contentStream = new PDPageContentStream(doc, page);
contentStream.beginText();
for (String line : lines) {
doc.addPage(page);
contentStream1.setFont(pdfFont, fontSize);
contentStream1.beginText();
contentStream1.moveTextPositionByAmount(startX, startY);
contentStream1.drawString(line);
contentStream1.moveTextPositionByAmount(0, -leading);
contentStream1.endText();
contentStream1.close();
}
}
}
此后,我使用outputByteArrayStream来存储内容并返回byteArray []。
这是我得到的错误
java.lang.NullPointerException
at org.apache.pdfbox.pdmodel.edit.PDPageContentStream.setFont(PDPageContentStream.java:321)
at ca.muhc.ri.account.pdf.PdfGenerator.insertStringEmail1(PdfGenerator.java:1224)
at ca.muhc.ri.account.pdf.PdfGenerator.generatePdfFromEmail(PdfGenerator.java:1083)
at ca.muhc.ri.account.manager.TaskTrackingManager.notifyByEmail(TaskTrackingManager.java:512)
at ca.muhc.ri.account.manager.TaskTrackingManager.onDialogSendNotif(TaskTrackingManager.java:328)
Truncated. see log file for complete stacktrace