In this paper in order to record their implementation, with reference to the specific implementation steps Bowen https://www.cnblogs.com/youzhibing/p/7692366.html to achieve, but on the basis of his pages and add headers and footers eyebrow pictures
WkhtmlToPdf originally decided to convert html page to pdf, html and save the style is also very good, but the last attempt, I discovered that he could not convert html page our framework, the framework of the html page address conversion will always be converted Home page picture, multiple queries to no avail, finally gave up and changed into itext tool.
As our demand requires headers and footers, and page header logo image is required, so the Internet also find a lot of blog, try a variety of online methods can not be used, I do not know my side of the question or bloggers who are not tested put the issue to the article, the last really can not find the information I began my own exploration, and ultimately through my own exploration to achieve the picture header added functionality, hereby record it.
Roughly implementation steps are based on the early side of the text of the article, just add a Build class inherits his PdfPageEventHelper, specific code as follows:
package com.tzCloud.core.utils; import com.itextpdf.text.Chunk; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.Image; import com.itextpdf.text.PageSize; import com.itextpdf.text.Phrase; import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.ColumnText; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfPageEventHelper; import com.itextpdf.text.pdf.PdfTemplate; import com.itextpdf.text.pdf.PdfWriter; import java.io.IOException; /** * PDF export headers, footers, watermarks and other processing class * Set additional attributes page * * @Author LiuYu * @date 2019/11/5 16:45 */ public class PDFBuilder extends PdfPageEventHelper { /** * Header * / Public String header = "test iText Header" ; /** * Document font size, footer and header text size consistent with the best */ public int presentFontSize = 12; /** * Document page size, best front passed, otherwise it defaults to A4 paper */ public Rectangle pageSize = PageSize.A4; /** * Templates */ public PdfTemplate total; /** * Base font object */ public BaseFont bf = null; /** * Use font generator font object basis, generally for generating Chinese characters */ public Font fontDetail = null; public void setHeader(String header) { this.header = header; } public void setPresentFontSize(int presentFontSize) { this.presentFontSize = presentFontSize; } /** * * Create a template when the document is opened TODO * * @see PdfPageEventHelper#onOpenDocument(PdfWriter, * Document) */ @Override public void OnOpenDocument (the PdfWriter Writer, the Document Document) { // rectangular co lwh page Total = writer.getDirectContent () createTemplate (50, 50. ); } /** * * TODO close the page when the header is written, is written 'on page a few common' words. * * @see PdfPageEventHelper#onEndPage(PdfWriter, * Document) */ @Override public void onEndPage(PdfWriter writer, Document document) { this.addPage(writer, document); //this.addWatermark(writer); } //加分页 public void addPage(PdfWriter writer, Document document){ // Home does not add headers and footers IF (writer.getPageNumber () == 1 ) { // add a cover page of background, here added two home background, a lower left corner from the top left corner from a specific location information can modify the following position the try { // PDF Home top background Image Image = Image.getInstance ( "HTTPS: // ******" ); // set the location of the picture is the picture the lower-left position on the left angle is 0,0 image.setAbsolutePosition (0, 441 ); // set the picture size image.scaleAbsolute (570, 400 ); // PDF Home bottom background Image instance = Image.getInstance ( "HTTPS: // *****" ); instance.setAbsolutePosition(0, 0); instance.scaleAbsolute(595, 260); document.add(image); document.add(instance); } catch (IOException | DocumentException e) { e.printStackTrace (); } return; } // set page headers and footers font Image Image = null ; the try { // header logo image examples of Image = Image.getInstance ( "HTTPS: // ******" ); IF (bf == null ) { bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false); } IF (fontDetail == null ) { // data style font fontDetail = new new the Font (BF, presentFontSize, Font.NORMAL); } } catch (DocumentException | IOException e) { e.printStackTrace (); } PdfContentByte directContent = writer.getDirectContent(); // 1, generating the right header // document.right (-20) Method spacing disposed about the header // document.top (+20) method sets the pitch of the upper and lower header ColumnText.showTextAligned (directContent, Element.ALIGN_RIGHT, new new Phrase ( "Tel: 400-000-0000" , fontDetail), document.right(), document.top(), 0); // ! ! ! ! The most important thing is, if you need to set the header picture, you need to add a Chunk object Phrase object, add image information in the Chunk object to Phrase p1 = new new Phrase ( "" , fontDetail); p1.add ( new new the Chunk (Image, 0, -30 )); // . 1, the left header write ColumnText.showTextAligned (directContent, Element.ALIGN_LEFT, p1, document.left(), document.top(-20), 0); // 2, generating the right footer ColumnText.showTextAligned (directContent, Element.ALIGN_RIGHT, new Phrase(String.valueOf(document.getPageNumber() - 1), fontDetail), document.right(), document.bottom(-20), 0); // 2, generating left footer ColumnText.showTextAligned (directContent, Element.ALIGN_LEFT, new new Phrase ( "****** Limited" , fontDetail), document.left(), document.bottom(-20), 0); } /** * Watermarked * / Public void addWatermark (PdfWriter Writer) { // Watermark Image Image Image; the try { image = Image.getInstance("./web/images/001.jpg"); PdfContentByte content = writer.getDirectContentUnder(); content.beginText (); // start writing watermark for ( int K = 0; K <. 5; K ++ ) { for ( int J = 0; J <. 4; J ++ ) { image.setAbsolutePosition(150*j,170*k); content.addImage(image); } } content.endText(); } catch (IOException | DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * * TODO when you close the document, replacing the template, complete the header and footer components * * @see PdfPageEventHelper#onCloseDocument(PdfWriter, * Document) */ @Override public void OnCloseDocument (PdfWriter Writer, the Document the Document) { // 7. The final step, that is, when the document is closed, will replace the template to the actual value of Y, so far, page x of y production is completed, perfectly compatible with a variety of document size. total.beginText (); // generated template font, color total.setFontAndSize (BF, presentFontSize); // footer splicing as 1 / Total 2 String foot2 = "" + (writer.getPageNumber () ) + "page" ; // content template displayed total.showText (foot2); total.endText(); total.closePath(); } }
A specific action on each page PDF can be implemented in this class, this page may be determined in the first few getPageNumber PDF by the method of the document, and then the specific operation can be performed, for example, the above code to be added at home bACKGROUND FIG other operations.
At this point you can add a picture realized in the PDF header