/** * * TODO 关闭文档时,替换模板,完成整个页眉页脚组件 * * @see com.itextpdf.text.pdf.PdfPageEventHelper#onCloseDocument(com.itextpdf.text.pdf.PdfWriter, * com.itextpdf.text.Document) */ publicvoidonCloseDocument(PdfWriter writer, Document document) { // 7.最后一步了,就是关闭文档的时候,将模板替换成实际的 Y 值,至此,page x of y 制作完毕,完美兼容各种文档size。 total.beginText(); total.setFontAndSize(bf, presentFontSize);// 生成的模版的字体、颜色 Stringfoot2=" " + (writer.getPageNumber() - 1) + " 页"; total.showText(foot2);// 模版显示的内容 total.endText(); total.closePath(); } }
页边距
Isn’t it possible for you to use HtmlConverter#convertToElements method. It returns List as a result and then you can add its elements to a document with set margins:
1 2 3 4 5 6 7
Documentdocument=newDocument(pdfDocument); Listlist= HtmlConverter.convertToElements(newFileInputStream(htmlSource)); for (IElement element : list) { if (element instanceof IBlockElement) { document.add((IBlockElement) element); } }
Another approach: in your html just introduce the @page rule which sets the margins you need, for example:
1
@page {margin: 0;}
Yet another solution: implement your own custom tag worker for tag and set margins on its level. For example, to set zero margins one could create tag the next worker:
and pass it as a ConverterProperties parameter to Htmlconverter:
converterProperties.setTagWorkerFactory(new CustomTagWorkerFactory()); HtmlConverter.convertToPdf(new File(htmlPath), new File(pdfPath), converterProperties);
表格跨页问题
代码的处理方式
pdfHtml的一种处理方式
I had a similar issue of trying to keep together content within a div. I applied the following css property and this kept everything together. This worked with itext7 pdfhtml.
Please read the official documentation for iText 7, more specifically Chapter 6: Reusing existing PDF documents In PDF, there’s the concept of _Form XObject_s. A Form XObject is a piece of PDF content that is stored outside the content stream of a page, hence XObject which stands for eXternal Object. The use of the word Form in Form XObject could be confusing, because people might be thinking of a form as in a fillable form with fields. To avoid that confusing, we introduced the term PdfTemplate in iText 5. The class PdfImportedPage you refer to was a subclass of PdfTemplate: it was a piece of PDF syntax that could be reused in another page. Over the years, we noticed that people also got confused by the word PdfTemplate. In iText 7, we returned to the basics. When talking about a Form XObject, we use the class PdfFormXObject. When talking about a page in a PDF file, we use the class PdfPage. This is how we get a PdfPage from an existing document: