页眉与页脚
iText5中并没有之前版本HeaderFooter对象设置页眉和页脚,可以利用PdfPageEventHelper来完成页眉页脚的设置工作。PdfPageEventHelper中包含以下事件处理器。
1 | onOpenDocument() — 当打开一个文档时触发,可以用于初始化文档的全局变量。 |
要想出发事件需要在程序中添加事件
如下
1 | PdfReportM1HeaderFooter footer=new PdfReportM1HeaderFooter(); |
该类PdfReportM1HeaderFooter继承自PdfPageEventHelper所以可以直接添加、
1 |
|
页边距
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 | Document document = new Document(pdfDocument); |
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:
1 | public class CustomTagWorkerFactory extends DefaultTagWorkerFactory { |
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.
1 | page-break-inside: avoid; |
PdfPage转换成图片
生成image对象
https://stackoverflow.com/questions/37809019/itext7-pdf-to-image
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:1
2PdfDocument origPdf = new PdfDocument(new PdfReader(src));
PdfPage origPage = origPdf.getPage(1);
This is how we use that page in a new document:1
2PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
PdfFormXObject pageCopy = origPage.copyAsFormXObject(pdf);
If you want to use that pageCopy
as an Image
, just create it like this:1
Image image = new Image(pageCopy);
存成图片(验证了这种方法不可行)
https://stackoverflow.com/questions/24940813/saving-com-itextpdf-text-image-as-a-image-file
Just convert the Barcode39 itext image into an AWT image using createAwtImage):1
java.awt.Image awtImage = code39.createAwtImage(Color.BLACK, Color.WHITE);
Then convert it to a BufferedImage
and store it:1
2
3
4
5
6BufferedImage bImage= new BufferedImage(awtImage.getWidth(), awtImage.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = bImage.createGraphics();
g.drawImage(awtImage, 0, 0, null);
g.dispose();
File outputfile = new File("saved.png");
ImageIO.write(bImage, "jpg", new File("code39.jpg"));