虽然我正在开始和关闭,但我得到的是异常。不确定是否因为异常块我没有关闭。
ERROR xyz.A - execute() error closing OutputStreamWriter. [source:]
java.io.IOException: com.lowagie.text.exceptions.IllegalPdfSyntaxException: Unbalanced begin/end text operators.
at PDFOutputWriter.close(PDFOutputWriter.java:91)
at xyz.A.execute_aroundBody0(A.java:319)
org.aspectj.runtime.reflect.JoinPointImpl.proceed(JoinPointImpl.java:149)
以下代码抛出此异常。
public void addPageNumber(PdfWriter writer, Document document) {
PdfContentByte cb = writer.getDirectContent();
cb.saveState();
String text = writer.getCurrentPageNumber() - pageOffset + " - " + timeStamp;
float textBase = document.bottom() - 20;
cb.beginText();
try {
cb.setFontAndSize( BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252,
false), 12);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
cb.setTextMatrix(document.left(), textBase);
cb.showText(text);
cb.endText();
cb.restoreState();
previousPage = writer.getCurrentPageNumber();
}
<groupId>com.itextpdf</groupId>
<artifactId>com.lowagie</artifactId>
<itext.version>2.1.7</itext.version>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.8</version>