我正在编写一个生成PDF文件的后端;有用。我们正在使用iText 2.1.7;很长一段时间它一直在我的店里,所以它就在这里。
我试图生成" Y"在没有阅读整个文档的页脚中获取页数。它几乎可以工作。
我在实现PdfPageEventHelper的类中有以下两种方法:
/**
* at the end of each page, write out our footer, including the
* 'totalPages' template.
*/
public void onEndPage(PdfWriter writer, Document document) {
PdfContentByte cb = writer.getDirectContent();
cb.saveState();
// cb.beginMarkedContentSequence(new PdfName("Artifact"));
cb.setColorFill(MEDIUM_GRAY);
String idString = String.format("ID : %s", proposalId);
String pageString = String.format("Page %s of ", writer.getPageNumber());
// dateString already formatted
float textBase = document.bottom() - 20;
float pageTextSize = baseFont.getWidthPoint(pageString, footerTextSize);
cb.beginText();
cb.setFontAndSize(baseFont, 8);
cb.setTextMatrix((document.right() / 8), textBase);
cb.showText(idString);
cb.setTextMatrix((document.right() / 2), textBase);
cb.showText(pageString);
cb.setTextMatrix(((document.right() / 8) * 7), textBase);
cb.showText(dateString);
cb.addTemplate(totalPages, (document.right() / 2) + pageTextSize, textBase);
cb.endText();
// cb.endMarkedContentSequence();
cb.restoreState();
}
@Override
public void onCloseDocument(PdfWriter writer, Document document) {
totalPages.beginText();
totalPages.setFontAndSize(baseFont, footerTextSize);
totalPages.setTextMatrix(0, 0);
totalPages.showText(String.valueOf(writer.getPageNumber() - 1));
totalPages.endText();
}
这确实通过文件提出了" Page 1 of 9"等。但是,间歇性地,Acrobat Reader会告诉用户该文档格式不正确。每次打开这样的文档时,我们都有一台机器和用户可以使这个错误始终如一。如果我只删除这两种方法,那就不会发生。
我在Bruno Lowagie的其他地方看到了一篇文章,指出由于这篇文章不是主文档的一部分,因此需要标记为#34;并举例说明调用方法' beginMarkedContentSequence()'和' endMarkedContentSequence()',如上所示,虽然已注释掉。我看到的例子在第一次调用中使用了常量PdfName.ARTIFACT,但该常量仅在更高版本的iText中可用。我查看了它的源代码,发现该常量只是通过实例化一个PdfName来创建的,所以我想我会尝试一下。但是,当我这样做时,iText会抛出异常:
ERROR:[10:28:41] ProposalManagementController.saveProposal.PUBLISH: Unknown Error trying to publish Proposal.
com.lowagie.text.exceptions.IllegalPdfSyntaxException: Unbalanced begin/end marked content operators.
at com.lowagie.text.pdf.PdfContentByte.endMarkedContentSequence(Unknown Source)
at com.accesspointinc.proposals.ProposalPDFGen$FooterWriter.onEndPage(ProposalPDFGen.java:1784)
at com.lowagie.text.pdf.PdfDocument.newPage(Unknown Source)
at com.lowagie.text.Document.newPage(Unknown Source)
at com.accesspointinc.proposals.ProposalPDFGen.insertLocationSummaryTable(ProposalPDFGen.java:282)
at com.accesspointinc.proposals.ProposalPDFGen.addPricing(ProposalPDFGen.java:219)
at com.accesspointinc.proposals.ProposalPDFGen.generatePDF(ProposalPDFGen.java:104)
at com.accesspointinc.proposals.ProposalManagementController.saveProposal(ProposalManagementController.java:410)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
那么,有没有办法从iText 2.1.7做到这一点?