iText + Freemarker achieve export pdf to support Chinese, css and images, headers and footers, headers, add pictures iText + Freemarker achieve export pdf to support Chinese, css and images, headers and footers, headers add pictures

Excerpt: https://www.cnblogs.com/kaifaxiaoliu/p/IText.html

iText + Freemarker achieve export pdf to support Chinese, css and images, headers and footers, headers add pictures

 

 

  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:

 

Copy the code
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 properties page 
 * 
 * @author LiuYu 
 * @date 2019/11/5 16:45 
 * / 
public class PDFBuilder the extends PdfPageEventHelper { 

    / ** 
     * page eyebrow 
     * / 
    public String header = "header iText test"; 

    / ** 
     * documentation font size, text size and the best header footer consistent 
     * / 
    public int presentFontSize = 12; 

    / ** 
     * documentation page size, the best front passed, otherwise it defaults to A4 paper 
     * / 
    public pageSize of the Rectangle = PageSize.A4; 

    / ** 
     * template 
      * / 
    public PdfTemplate Total; 

    / ** 
     * base font object 
     * / 
    public BaseFont bf = null;

    / ** 
     * based fonts using the font object generated, typically for generating Chinese characters 
     * / 
    public fontDetail the Font = null; 


    public void the setHeader (String header) { 
        this.header = header; 
    } 

    public void setPresentFontSize (int presentFontSize) { 
        the this = presentFontSize .presentFontSize; 
    } 

    / ** 
     * 
     * create a template when the document is opened TODO 
     * 
     * @see PdfPageEventHelper # OnOpenDocument (PdfWriter, 
     * the document) 
     * / 
    @Override 
    public void OnOpenDocument (PdfWriter Writer, the document the document) { 
        // total page rectangular LWH 
        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 (the PdfWriter, 
     * the Document) 
     * / 
    @Override 
    public void OnEndPage (the PdfWriter Writer, the Document Document) { 
        this.addPage (Writer, Document); 
        //this.addWatermark(writer); 
    } 

    // add paging 
    public void addPage (PdfWriter Writer, the Document the Document) { 

        // Home does not add headers and footers 
        IF (writer.getPageNumber () == 1) { 
            // add a cover page of background, add here Home two background image, one by one from the lower left corner, the specific location may be modified from the upper left position information following 
            the try { 
                 // set the picture picture position is the lower left corner to the left position 0,0
                // the top of the Home PDF background
                Image.getInstance Image = Image ( "HTTPS: // ******"); 
        // set page header and footer Font
                image.setAbsolutePosition (0, 441); 
                 // set the image 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); 
            } the catch (IOException | DocumentException E) { 
                e.printStackTrace (); 
            } 

            return ; 
        } 

        image image = null; 
        the try { 
            // header logo image examples
            Image.getInstance = Image ( "HTTPS: // ******"); 
            IF (BF == null) { 
                BF = BaseFont.createFont ( "STSong-Light", "the UCS2-UniGB-H", to false) ; 
            } 
            IF (fontDetail == null) { 
                // style font data 
                fontDetail the Font new new = (BF, presentFontSize, Font.NORMAL); 
            } 
        } the catch (DocumentException | IOException E) { 
            e.printStackTrace (); 
        } 

        PdfContentByte directContent = Writer .getDirectContent (); 


        //. 1, the right side of the header generating 
        // document.right (-20) method disposed about the header spacing  
        // document.top (+20) method sets the upper and lower header spacing
        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 new Phrase (String.valueOf (document.getPageNumber () -. 1), fontDetail), 
                document.right (), document.bottom (-20), 0); 

        / / 2, generating a left footer
        ColumnText.showTextAligned (directContent, 
                Element.ALIGN_LEFT, new new Phrase ( "****** Limited", fontDetail), 
                document.left (), document.bottom (-20), 0); 


    } 

    / ** 
     * add watermark 
     * / 
    public void addWatermark (PdfWriter Writer) { 
        // watermark Image 
        Image Image; 
        the try { 
            Image = Image.getInstance ( "./ Web / ImagesRF Royalty Free / 001.jpg"); 
            PdfContentByte Content = writer.getDirectContentUnder (); 
            content.beginText();
            // watermark start writing 
            for (int = 0 K; K <. 5; K ++) { 
                for (int J = 0; J <. 4; J ++) { 
                    image.setAbsolutePosition (J * 150, 170. * K); 
                    content.addImage (Image); 
                } 
            } 
            content.endText (); 
        } the catch (IOException | DocumentException E) { 
            // the TODO Auto-Generated Block the catch 
            e.printStackTrace (); 
        } 
    } 

    / ** 
     * 
     * the TODO when closing the document, replacing the template, to complete the assembly of header and footer 
     * 
     * @see PdfPageEventHelper # OnCloseDocument (the PdfWriter, 
     * the document) 
     * / 
    @Override 
    public void OnCloseDocument (PdfWriter writer, Document 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 (); 
        // template generated font, color
        total.setFontAndSize (BF, presentFontSize); 
        // footer content splicing as described on page 1/2 Total 
        String foot2 = "" + (writer.getPageNumber ()) + " p"; 
        // template displayed content 
        total.showText (foot2); 
        total.endText (); 
        total.closePath (); 
    } 
}
Copy the code

 

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

 

 

 
Tags:  IText

 

  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:

 

Copy the code
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 properties page 
 * 
 * @author LiuYu 
 * @date 2019/11/5 16:45 
 * / 
public class PDFBuilder the extends PdfPageEventHelper { 

    / ** 
     * page eyebrow 
     * / 
    public String header = "header iText test"; 

    / ** 
     * documentation font size, text size and the best header footer consistent 
     * / 
    public int presentFontSize = 12; 

    / ** 
     * documentation page size, the best front passed, otherwise it defaults to A4 paper 
     * / 
    public pageSize of the Rectangle = PageSize.A4; 

    / ** 
     * template 
      * / 
    public PdfTemplate Total; 

    / ** 
     * base font object 
     * / 
    public BaseFont bf = null;

    / ** 
     * based fonts using the font object generated, typically for generating Chinese characters 
     * / 
    public fontDetail the Font = null; 


    public void the setHeader (String header) { 
        this.header = header; 
    } 

    public void setPresentFontSize (int presentFontSize) { 
        the this = presentFontSize .presentFontSize; 
    } 

    / ** 
     * 
     * create a template when the document is opened TODO 
     * 
     * @see PdfPageEventHelper # OnOpenDocument (PdfWriter, 
     * the document) 
     * / 
    @Override 
    public void OnOpenDocument (PdfWriter Writer, the document the document) { 
        // total page rectangular LWH 
        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 (the PdfWriter, 
     * the Document) 
     * / 
    @Override 
    public void OnEndPage (the PdfWriter Writer, the Document Document) { 
        this.addPage (Writer, Document); 
        //this.addWatermark(writer); 
    } 

    // add paging 
    public void addPage (PdfWriter Writer, the Document the Document) { 

        // Home does not add headers and footers 
        IF (writer.getPageNumber () == 1) { 
            // add a cover page of background, add here Home two background image, one by one from the lower left corner, the specific location information can be modified from the upper left position below 
            the try { 
                // the top of the Home PDF background
                Image.getInstance Image = Image ( "HTTPS: // ******");  
                 // set the picture picture position is the lower left corner to the left position 0,0
                image.setAbsolutePosition (0, 441); 
                 // set the image size 
                image.scaleAbsolute (570, 400); 

                // bottom PDF Home FIG bACKGROUND 
                Image instance Image.getInstance = ( "HTTPS: // *****"); 
                instance.setAbsolutePosition (0, 0); 
                instance.scaleAbsolute (595, 260.); 

                document.add (Image); 
                document.add (instance); 
            } the catch (IOException | DocumentException E) { 
                e.printStackTrace (); 
            } 

            return; 
        } 

        // set page header and footer Font
        = Null Image Image; 
        // document.top (+20) method sets the upper and lower header spacing
        the try { 
            // header logo image examples
            Image.getInstance = Image ( "HTTPS: // ******"); 
            IF (BF == null) { 
                BF = BaseFont.createFont ( "STSong-Light", "the UCS2-UniGB-H", to false) ; 
            } 
            IF (fontDetail == null) { 
                // style font data 
                fontDetail the Font new new = (BF, presentFontSize, Font.NORMAL); 
            } 
        } the catch (DocumentException | IOException E) { 
            e.printStackTrace (); 
        } 

        PdfContentByte directContent = Writer .getDirectContent (); 


        //. 1, the right side of the header generating 
        // document.right (-20) method disposed about the header spacing 
        ColumnText.showTextAligned (directContent,
                Element.ALIGN_RIGHT, 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 new Phrase (String.valueOf (document.getPageNumber () -. 1), fontDetail), 
                document.right (), document.bottom (-20), 0); 

        / / 2, generating a left footer
        ColumnText.showTextAligned (directContent, 
                Element.ALIGN_LEFT, new new Phrase ( "****** Limited", fontDetail), 
                document.left (), document.bottom (-20), 0); 


    } 

    / ** 
     * add watermark 
     * / 
    public void addWatermark (PdfWriter Writer) { 
        // watermark Image 
        Image Image; 
        the try { 
            Image = Image.getInstance ( "./ Web / ImagesRF Royalty Free / 001.jpg"); 
            PdfContentByte Content = writer.getDirectContentUnder (); 
            content.beginText();
            // watermark start writing 
            for (int = 0 K; K <. 5; K ++) { 
                for (int J = 0; J <. 4; J ++) { 
                    image.setAbsolutePosition (J * 150, 170. * K); 
                    content.addImage (Image); 
                } 
            } 
            content.endText (); 
        } the catch (IOException | DocumentException E) { 
            // the TODO Auto-Generated Block the catch 
            e.printStackTrace (); 
        } 
    } 

    / ** 
     * 
     * the TODO when closing the document, replacing the template, to complete the assembly of header and footer 
     * 
     * @see PdfPageEventHelper # OnCloseDocument (the PdfWriter, 
     * the document) 
     * / 
    @Override 
    public void OnCloseDocument (PdfWriter writer, Document 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 (); 
        // template generated font, color
        total.setFontAndSize (BF, presentFontSize); 
        // footer content splicing as described on page 1/2 Total 
        String foot2 = "" + (writer.getPageNumber ()) + " p"; 
        // template displayed content 
        total.showText (foot2); 
        total.endText (); 
        total.closePath (); 
    } 
}
Copy the code

 

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

 

 

Guess you like

Origin www.cnblogs.com/xichji/p/12009246.html