1. 程式人生 > >iText7通過模板生成PDF

iText7通過模板生成PDF

最後給附上我的整個程式碼和結果
package com.bupt.common.itext;
import com.bupt.common.entity.MaintenanceMaterial;
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfReader; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.kernel.pdf.canvas.PdfCanvas; import com.itextpdf.layout.Canvas; import com.itextpdf.layout.Document; import com.itextpdf.layout.element.Image; import com.itextpdf.layout.element.Paragraph;
import javax.servlet.http.HttpServletRequest; import java.util.List; import java.util.Map; import java.util.Random; public class PdfUtils{ public static String manipulatePdf(HttpServletRequest request, Map<String,Object> map) throws Exception { //String pdfDir = "E:///IdeaProjects/newWindPower/web/";
String pdfDir = request.getSession().getServletContext().getRealPath("/");
      final String SRC = pdfDir+"pdfMold/維修報告模板.pdf"
        // id + 時間戳 + 隨機數 + 原有的字尾名  保證不會被重複
        String reportPdfUrl = "PdfTemp/" + System.currentTimeMillis() + "-" +
                new Random().nextInt(100000) +".pdf";
        String DEST = pdfDir+reportPdfUrl;
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));

        PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
        form.setGenerateAppearance(true);

        PdfFont f = PdfFontFactory.createFont("STSongStd-Light","UniGB-UCS2-H",false);

        Document document = new Document(pdfDoc);
        //第1頁資料
        Rectangle[] areas1 = new Rectangle[] {
                new Rectangle(218, 586, 324, 31), new Rectangle(218, 553, 324, 31),
                new Rectangle(218, 460, 324, 90), new Rectangle(125, 270, 200, 190),
                new Rectangle(325, 270, 220, 190), new Rectangle(125, 80, 200, 190),
                new Rectangle(325, 80, 220, 190)
        };

       

        Paragraph p0 = new Paragraph(map.get("custCompanyName").toString()).setFont(f);
        new Canvas(new PdfCanvas(pdfDoc.getFirstPage()).stroke(), pdfDoc, areas1[0])
                .add(p0.setFixedPosition(218, 593, 324));


        Paragraph p1= new Paragraph(map.get("custPhoneNo").toString()).setFont(f);
        new Canvas(new PdfCanvas(pdfDoc.getFirstPage()).stroke(), pdfDoc, areas1[1])
                .add(p1.setFixedPosition(218, 562, 324));


        Paragraph p2= new Paragraph(map.get("demandContent").toString()).setFont(f);
        new Canvas(new PdfCanvas(pdfDoc.getFirstPage()).stroke(), pdfDoc, areas1[2])
                .add(p2.setFixedPosition(218, 472, 324));


        //第2頁資料
        Rectangle[] areas2 = new Rectangle[] {
                new Rectangle(125, 584, 200, 185),new Rectangle(325, 584, 220, 185),
                new Rectangle(213, 552, 324, 33),new Rectangle(213, 519, 324, 33),
                new Rectangle(213, 455, 324, 33),new Rectangle(213, 422, 324, 33),
                new Rectangle(213, 389, 324, 33),new Rectangle(213, 300, 324, 90),
                new Rectangle(213, 210, 324, 90),new Rectangle(213, 173, 324, 33),
                new Rectangle(213, 80, 324, 90)
        };

        String[] IMAGES = map.get("demandPicUrl").toString().split("\\|");
        int len = IMAGES.length;
        if (len>6){
            len=6;
        }
        for (int i = 0;i<len;i++) {
            Image image = new Image(ImageDataFactory.create(pdfDir + IMAGES[i]));
            image.setAutoScale(true);
            switch (i) {
                case 0: {
                    new Canvas(new PdfCanvas(pdfDoc.getFirstPage()).stroke(), pdfDoc, areas1[3+i])
                            .add(image.setFixedPosition(125, 270));
                    break;
                }
                case 1: {
                    new Canvas(new PdfCanvas(pdfDoc.getFirstPage()).stroke(), pdfDoc, areas1[3+i])
                            .add(image.setFixedPosition(325, 270));
                    break;
                }
                case 2: {
                    new Canvas(new PdfCanvas(pdfDoc.getFirstPage()).stroke(), pdfDoc, areas1[3+i])
                            .add(image.setFixedPosition(125, 80));
                    break;
                }
                case 3: {
                    new Canvas(new PdfCanvas(pdfDoc.getFirstPage()).stroke(), pdfDoc, areas1[3+i])
                            .add(image.setFixedPosition(325, 80));
                    break;
                }
                case 4: {
                    new Canvas(new PdfCanvas(pdfDoc.getPage(2)).stroke(), pdfDoc, areas2[0])
                            .add(image.setFixedPosition(125, 584));
                    break;
                }
                case 5: {
                    new Canvas(new PdfCanvas(pdfDoc.getPage(2)).stroke(), pdfDoc, areas2[1])
                            .add(image.setFixedPosition(325, 584));
                    break;
                }

            }
        }
/*
Rectangle rect = new Rectangle(llx, lly, urx, ury);
   new Canvas(new PdfCanvas(pdfDoc.getFirstPage()).rectangle(areas1[0]).stroke(), pdfDoc, areas1[0])
    .add(p.setFixedPosition(llx, (ury + 2lly) / 2, urx));
*/
       
        Paragraph page2p0= new Paragraph(map.get("timeLimit").toString()).setFont(f);
        new Canvas(new PdfCanvas(pdfDoc.getPage(2)).stroke(), pdfDoc, areas2[2])
                .add(page2p0.setFixedPosition(213, 562, 324));



        Paragraph page2p2= new Paragraph(map.get("address").toString()).setFont(f);
        new Canvas(new PdfCanvas(pdfDoc.getPage(2)).stroke(), pdfDoc, areas2[3])
                .add(page2p2.setFixedPosition(213, 529, 324));


        Paragraph page2p3= new Paragraph(map.get("resolverCompanyName").toString()).setFont(f);
        new Canvas(new PdfCanvas(pdfDoc.getPage(2)).stroke(), pdfDoc, areas2[4])
                .add(page2p3.setFixedPosition(213, 465, 324));


        Paragraph page2p4= new Paragraph(map.get("resolverPhoneNo").toString()).setFont(f);
        new Canvas(new PdfCanvas(pdfDoc.getPage(2)).stroke(), pdfDoc, areas2[5])
                .add(page2p4.setFixedPosition(213, 432, 324));


        Paragraph page2p5= new Paragraph(map.get("resolverName").toString()).setFont(f);
        new Canvas(new PdfCanvas(pdfDoc.getPage(2)).stroke(), pdfDoc, areas2[6])
                .add(page2p5.setFixedPosition(213, 399, 324));


        Paragraph page2p6= new Paragraph(map.get("demandDesc").toString()).setFont(f);
        new Canvas(new PdfCanvas(pdfDoc.getPage(2)).stroke(), pdfDoc, areas2[7])
                .add(page2p6.setFixedPosition(213, 309, 324));


        Paragraph page2p7= new Paragraph(map.get("demandCause").toString()).setFont(f);
        new Canvas(new PdfCanvas(pdfDoc.getPage(2)).stroke(), pdfDoc, areas2[8])
                .add(page2p7.setFixedPosition(213, 219, 324));


        Paragraph page2p8= new Paragraph(map.get("damageLevel").toString()).setFont(f);
        new Canvas(new PdfCanvas(pdfDoc.getPage(2)).stroke(), pdfDoc, areas2[9])
                .add(page2p8.setFixedPosition(213, 186, 324));


        Paragraph page2p9= new Paragraph(map.get("maintenanceScheme").toString()).setFont(f);
        new Canvas(new PdfCanvas(pdfDoc.getPage(2)).stroke(), pdfDoc, areas2[10])
                .add(page2p9.setFixedPosition(213, 96, 324));



        //第三頁資料
        Rectangle[] areas3 = new Rectangle[] {
                new Rectangle(123, 584, 200, 185),new Rectangle(325, 584, 220, 185),
                new Rectangle(123, 399, 200, 185),new Rectangle(325, 399, 220, 185),
                new Rectangle(123, 214, 200, 185),new Rectangle(325, 214, 220, 185),
                new Rectangle(235, 144, 90, 33),new Rectangle(325, 144, 128, 33), new Rectangle(453, 144, 70, 33),
                new Rectangle(235, 111, 90, 33),new Rectangle(325, 111, 128, 33), new Rectangle(453, 111, 70, 33),
                new Rectangle(235, 78, 90, 33),new Rectangle(325, 78, 128, 33), new Rectangle(453, 78, 70, 33)
        };

        String[] fixReportImage = map.get("fixReportImage").toString().split("\\|");
        int length = fixReportImage.length;
        if (length>6){
            length=6;
        }
        for (int i = 0;i<length;i++) {
            Image image = new Image(ImageDataFactory.create(pdfDir + fixReportImage[i]));
            image.setAutoScale(true);
            switch (i) {
                case 0: {
                    new Canvas(new PdfCanvas(pdfDoc.getPage(3)).stroke(), pdfDoc, areas3[i])
                            .add(image.setFixedPosition(123, 584));
                    break;
                }
                case 1: {
                    new Canvas(new PdfCanvas(pdfDoc.getPage(3)).stroke(), pdfDoc, areas3[i])
                            .add(image.setFixedPosition(325, 584));
                    break;
                }
                case 2: {
                    new Canvas(new PdfCanvas(pdfDoc.getPage(3)).stroke(), pdfDoc, areas3[i])
                            .add(image.setFixedPosition(123, 399));
                    break;
                }
                case 3: {
                    new Canvas(new PdfCanvas(pdfDoc.getPage(3)).stroke(), pdfDoc, areas3[i])
                            .add(image.setFixedPosition(325, 399));
                    break;
                }
                case 4: {
                    new Canvas(new PdfCanvas(pdfDoc.getPage(3)).stroke(), pdfDoc, areas3[i])
                            .add(image.setFixedPosition(123, 214));
                    break;
                }
                case 5: {
                    new Canvas(new PdfCanvas(pdfDoc.getPage(3)).stroke(), pdfDoc, areas3[i])
                            .add(image.setFixedPosition(325, 214));
                    break;
                }

            }
        }


        //第四頁資料
        Rectangle[] areas4 = new Rectangle[] {
                new Rectangle(235, 736, 90, 33),new Rectangle(325, 736, 128, 33), new Rectangle(453, 736, 70, 33),
                new Rectangle(235, 703, 90, 33),new Rectangle(325, 703, 128, 33), new Rectangle(453, 703, 70, 33),
                new Rectangle(235, 670, 90, 33),new Rectangle(325, 670, 128, 33), new Rectangle(453, 670, 70, 33),
                new Rectangle(235, 640, 90, 33),new Rectangle(325, 640, 128, 33), new Rectangle(453, 640, 70, 33),
                new Rectangle(235, 607, 90, 33),new Rectangle(325, 607, 128, 33), new Rectangle(453, 607, 70, 33),
                new Rectangle(235, 576, 90, 33),new Rectangle(325, 576, 128, 33), new Rectangle(453, 576, 70, 33),
                new Rectangle(235, 544, 90, 33),new Rectangle(325, 544, 128, 33), new Rectangle(453, 544, 70, 33),
                new Rectangle(235, 511, 90, 33),new Rectangle(325, 511, 128, 33), new Rectangle(453, 511, 70, 33),
                new Rectangle(213, 481, 330, 33),
                new Rectangle(213, 417, 330, 32),
                new Rectangle(213, 385, 330, 31),
                new Rectangle(213, 295, 330, 95)
        };
       

        List<MaintenanceMaterial> maintenanceMaterialList = (List<MaintenanceMaterial>) map.get("maintenanceMaterialList");
        for (MaintenanceMaterial m:maintenanceMaterialList){
            switch (m.getMaterialName().trim()){
                case "手糊樹脂":{
                    Paragraph page3p0= new Paragraph(m.getManufacturer()).setFont(f);
                    new Canvas(new PdfCanvas(pdfDoc.getPage(3)).stroke(), pdfDoc, areas3[6])
                            .add(page3p0.setFixedPosition(235, 155, 90));

                    Paragraph page3p1= new Paragraph(m.getSpecificationType()).setFont(f);
                    new Canvas(new PdfCanvas(pdfDoc.getPage(3)).stroke(), pdfDoc, areas3[7])
                            .add(page3p1.setFixedPosition(325, 155, 128));

                    Paragraph page3p2= new Paragraph(m.getUsedAmount().toString()).setFont(f);
                    new Canvas(new PdfCanvas(pdfDoc.getPage(3)).stroke(), pdfDoc, areas3[8])
                            .add(page3p2.setFixedPosition(453, 155, 70));

                    break;
                }
                case "手糊樹脂固化劑":{
                    Paragraph page3p3= new Paragraph(m.getManufacturer()).setFont(f);
                    new Canvas(new PdfCanvas(pdfDoc.getPage(3)).stroke(), pdfDoc, areas3[9])
                            .add(page3p3.setFixedPosition(235, 122, 90));

                    Paragraph page3p4= new Paragraph(m.getSpecificationType()).setFont(f);
                    new Canvas(new PdfCanvas(pdfDoc.getPage(3)).stroke(), pdfDoc, areas3[10])
                            .add(page3p4.setFixedPosition(325, 122, 128));

                    Paragraph page3p5= new Paragraph(m.getUsedAmount().toString()).setFont(f);
                    new Canvas(new PdfCanvas(pdfDoc.getPage(3)).stroke(), pdfDoc, areas3[11])
                            .add(page3p5.setFixedPosition(453, 122, 70));

                    break;
                }
                case "粘接劑樹脂":{
                    Paragraph page3p6= new Paragraph(m.getManufacturer()).setFont(f);
                    new Canvas(new PdfCanvas(pdfDoc.getPage(3)).stroke(), pdfDoc, areas3[12])
                            .add(page3p6.setFixedPosition(235, 89, 90));

                    Paragraph page3p7= new Paragraph(m.getSpecificationType()).setFont(f);
                    new Canvas(new PdfCanvas(pdfDoc.getPage(3)).stroke(), pdfDoc, areas3[13])
                            .add(page3p7.setFixedPosition(325, 89, 128));

                    Paragraph page3p8= new Paragraph(m.getUsedAmount().toString()).setFont(f);
                    new Canvas(new PdfCanvas(pdfDoc.getPage(3)).stroke(), pdfDoc, areas3[14])
                            .add(page3p8.setFixedPosition(453, 89, 70));

                    break;
                }
                case "粘接劑固化劑":{
                    Paragraph page4p0= new Paragraph(m.getManufacturer()).setFont(f);
                    new Canvas(new PdfCanvas(pdfDoc.getPage(4)).stroke(), pdfDoc, areas4[0])
                            .add(page4p0.setFixedPosition(235, 746, 90));

                    Paragraph page4p1= new Paragraph(m.getSpecificationType()).setFont(f);
                    new Canvas(new PdfCanvas(pdfDoc.getPage(4)).stroke(), pdfDoc, areas4[1])
                            .add(page4p1.setFixedPosition(325, 746, 128));

                    Paragraph page4p2= new Paragraph(m.getUsedAmount().toString()).setFont(f);
                    new Canvas(new PdfCanvas(pdfDoc.getPage(4)).stroke(), pdfDoc, areas4[2])
                            .add(page4p2.setFixedPosition(453, 746, 70));

                    break;
                }
                case "灌注樹脂":{
                    Paragraph page4p3= new Paragraph(m.getManufacturer()).setFont(f);
                    new Canvas(new PdfCanvas(pdfDoc.getPage(4)).stroke(), pdfDoc, areas4[3])
                            .add(page4p3.setFixedPosition(235, 713, 90));

                    Paragraph page4p4= new Paragraph(m.getSpecificationType()).setFont(f);
                    new Canvas(new PdfCanvas(pdfDoc.getPage(4)).stroke(), pdfDoc, areas4[4])
                            .add(page4p4.setFixedPosition(325, 713, 128));

                    Paragraph page4p5= new Paragraph(m.getUsedAmount().toString()).setFont(f);
                    new Canvas(new PdfCanvas(pdfDoc.getPage(4)).stroke(), pdfDoc, areas4[5])
                            .add(page4p5.setFixedPosition(453, 713, 70));

                    break;
                }
                case "灌注樹脂固化劑":{
                    Paragraph page4p6= new Paragraph(m.getManufacturer()).setFont(f);
                    new Canvas(new PdfCanvas(pdfDoc.getPage(4)).stroke(), pdfDoc, areas4[6])
                            .add(page4p6.setFixedPosition(235, 680, 90));

                    Paragraph page4p7= new Paragraph(m.getSpecificationType()).setFont(f);
                    new Canvas(new PdfCanvas(pdfDoc.getPage(4)).stroke(), pdfDoc, areas4[7])
                            .add(page4p7.setFixedPosition(325, 680, 128));

                    Paragraph page4p8= new Paragraph(m.getUsedAmount().toString()).setFont(f);
                    new Canvas(new PdfCanvas(pdfDoc.getPage(4)).stroke(), pdfDoc, areas4[8])
                            .add(page4p8.setFixedPosition(453, 680, 70));

                    break;
                }
                case "芯材":{
                    Paragraph page4p9= new Paragraph(m.getManufacturer()).setFont(f);
                    new Canvas(new PdfCanvas(pdfDoc.getPage(4)).stroke(), pdfDoc, areas4[9])
                            .add(page4p9.setFixedPosition(235, 647, 90));

                    Paragraph page4p10= new Paragraph(m.getSpecificationType()).setFont(f);
                    new Canvas(new PdfCanvas(pdfDoc.getPage(4)).stroke(), pdfDoc, areas4[10])
                            .add(page4p10.setFixedPosition(325, 647, 128));


                    break;
                }
                case "油漆":{
                    Paragraph page4p12= new Paragraph(m.getManufacturer()).setFont(f);
                    new Canvas(new PdfCanvas(pdfDoc.getPage(4)).stroke(), pdfDoc, areas4[12])
                            .add(page4p12.setFixedPosition(235, 614, 90));

                    Paragraph page4p13= new Paragraph(m.getSpecificationType()).setFont(f);
                    new Canvas(new PdfCanvas(pdfDoc.getPage(4)).stroke(), pdfDoc, areas4[13])
                            .add(page4p13.setFixedPosition(325, 614, 128));

                    Paragraph page4p14= new Paragraph(m.getUsedAmount().toString()).setFont(f);
                    new Canvas(new PdfCanvas(pdfDoc.getPage(4)).stroke(), pdfDoc, areas4[14])
                            .add(page4p14.setFixedPosition(453, 614, 70));

                    break;
                }
                case "膩子":{
                    Paragraph page4p15= new Paragraph(m.getManufacturer()).setFont(f);
                    new Canvas(new PdfCanvas(pdfDoc.getPage(4)).stroke(), pdfDoc, areas4[15])
                            .add(page4p15.setFixedPosition(235, 581, 90));

                    Paragraph page4p16= new Paragraph(m.getSpecificationType()).setFont(f);
                    new Canvas(new PdfCanvas(pdfDoc.getPage(4)).stroke(), pdfDoc, areas4[16])
                            .add(page4p16.setFixedPosition(325, 581, 128));

                    Paragraph page4p17= new Paragraph(m.getUsedAmount().toString()).setFont(f);
                    new Canvas(new PdfCanvas(pdfDoc.getPage(4)).stroke(), pdfDoc, areas4[17])
                            .add(page4p17.setFixedPosition(453, 581, 70));

                    break;
                }
                case "玻纖布":{
                    Paragraph page4p18= new Paragraph(m.getManufacturer()).setFont(f);
                    new Canvas(new PdfCanvas(pdfDoc.getPage(4)).stroke(), pdfDoc, areas4[18])
                            .add(page4p18.setFixedPosition(235, 548, 90));

                    Paragraph page4p19= new Paragraph(m.getSpecificationType()).setFont(f);
                    new Canvas(new PdfCanvas(pdfDoc.getPage(4)).stroke(), pdfDoc, areas4[19])
                            .add(page4p19.setFixedPosition(325, 548, 128));

                    Paragraph page4p20= new Paragraph(m.getUsedAmount().toString()).setFont(f);
                    new Canvas(new PdfCanvas(pdfDoc.getPage(4)).stroke(), pdfDoc, areas4[20])
                            .add(page4p20.setFixedPosition(453, 548, 70));

                    break;
                }
                case "其它":{
                    Paragraph page4p21= new Paragraph(m.getManufacturer()).setFont(f);
                    new Canvas(new PdfCanvas(pdfDoc.getPage(4)).stroke(), pdfDoc, areas4[21])
                            .add(page4p21.setFixedPosition(235, 515, 90));

                    Paragraph page4p22= new Paragraph(m.getSpecificationType()).setFont(f);
                    new Canvas(new PdfCanvas(pdfDoc.getPage(4)).stroke(), pdfDoc, areas4[22])
                            .add(page4p22.setFixedPosition(325, 515, 128));

                    Paragraph page4p23= new Paragraph(m.getUsedAmount().toString()).setFont(f);
                    new Canvas(new PdfCanvas(pdfDoc.getPage(4)).stroke(), pdfDoc, areas4[23])
                            .add(page4p23.setFixedPosition(453, 515, 70));

                    break;
                }
                default:{
                    throw new RuntimeException("未知的材料");
                }
            }
        }
        Paragraph page4p24= new Paragraph(map.get("resolvedTime").toString()).setFont(f);
        new Canvas(new PdfCanvas(pdfDoc.getPage(4)).stroke(), pdfDoc, areas4[24])
                .add(page4p24.setFixedPosition(213, 491, 330));

        Paragraph page4p25= new Paragraph(map.get("acceptanceCheck").toString()).setFont(f);
        new Canvas(new PdfCanvas(pdfDoc.getPage(4)).stroke(), pdfDoc, areas4[25])
                .add(page4p25.setFixedPosition(213, 427, 330));
        
        Paragraph page4p26= new Paragraph(map.get("resolveGrade").toString()).setFont(f).setFontSize(16f);
        new Canvas(new PdfCanvas(pdfDoc.getPage(4)).stroke(), pdfDoc, areas4[26])
                .add(page4p26.setFixedPosition(213, 395, 330));

        Paragraph page4p27= new Paragraph(map.get("resolveEvaluate").toString()).setFont(f);
        new Canvas(new PdfCanvas(pdfDoc.getPage(4)).stroke(), pdfDoc, areas4[27])
                .add(page4p27.setFixedPosition(213, 300, 330));

        document.close();

        return reportPdfUrl;
    }

}
技術水平和英語水平都很菜,也只能用這麼粗笨的方法解決,如果大家有好的想法,歡迎大家一起討論.