首页 > 编程语言 > 详细

Java代码写PDF-保全批单

时间:2019-09-25 21:07:38      阅读:102      评论:0      收藏:0      [点我收藏+]

前言:最近自己要开发一个保全批单模块,由于自己在平时没有怎么接触过批单类型模块,甲方给了自己一套word模板,自己看了一下,个险的模板比较简单,但是团险一看,自己比较蒙圈,询问需求负责人说word中的excel类型的表格(要填充业务数据)是动态生成的。明白了之后,自己看到同事比较忙,自己免不了各种百度,经过了两天的鏖战,终于有所成效,先将流程和代码附上。

需求文档:

                技术分享图片

 

                         技术分享图片

 

                               技术分享图片

 

 

需要的jar包:(说真的:在jar包上我遇到的问题还是比较多的,版本冲突忒难解决。网友们也是各显神通,我个人的建议,你先将这几个包都引进来,最起码,先不让你的代码报错。下面我在说我遇到了什么棘手的问题以及处理手段)。

 1      <dependency>
 2             <groupId>com.itextpdf</groupId>
 3             <artifactId>itext-asian</artifactId>
 4             <version>5.2.0</version>
 5      </dependency>
 6      <dependency>
 7             <groupId>com.itextpdf</groupId>
 8             <artifactId>itextpdf</artifactId>
 9             <version>5.4.2</version>
10      </dependency>
11 
12         <!--解决pdf中文不显示-->
13      <dependency>
14             <groupId>com.lowagie</groupId>
15             <artifactId>itext</artifactId>
16             <version>2.1.7</version>
17      </dependency>

java生成pdf的代码(每一步基本上我都有详细的介绍??)

                                           代码中的deal()方法可以当作main方法使用,粘贴出来直接跑就行!

  1 package com.sinosoft.lis.mgubq;
  2 
  3 import com.lowagie.text.*;
  4 import com.lowagie.text.pdf.BaseFont;
  5 import com.lowagie.text.pdf.PdfPCell;
  6 import com.lowagie.text.pdf.PdfPTable;
  7 import com.lowagie.text.pdf.PdfWriter;
  8 import com.lowagie.text.pdf.draw.LineSeparator;
  9 import com.sinosoft.lis.pubfun.*;
 10 import com.sinosoft.lis.schema.LPPolicyHolderBSchema;
 11 import com.sinosoft.lis.schema.LPPolicyHolderSchema;
 12 import com.sinosoft.lis.vschema.LPPolicyHolderBSet;
 13 import com.sinosoft.utility.*;
 14 
 15 import java.awt.Color;
 16 import java.io.FileNotFoundException;
 17 import java.io.FileOutputStream;
 18 import java.io.IOException;
 19 import java.util.ArrayList;
 20 import java.util.List;
 21 
 22 import com.itextpdf.awt.geom.Dimension2D;
 23 import com.itextpdf.awt.geom.Rectangle2D;
 24 import com.lowagie.text.*;
 25 import com.lowagie.text.pdf.*;
 26 import com.lowagie.text.pdf.PdfImage;
 27 import com.lowagie.text.pdf.draw.LineSeparator;
 28 import com.spire.pdf.automaticfields.PdfCompositeField;
 29 import com.spire.pdf.automaticfields.PdfPageCountField;
 30 import com.spire.pdf.automaticfields.PdfPageNumberField;
 31 import com.spire.pdf.graphics.*;
 32 import org.springframework.boot.autoconfigure.security.SecurityProperties;
 33 
 34 import org.junit.Test;
 35 
 36 import java.io.FileOutputStream;
 37 
 38 /**
 39  * 投保人资料变更(团单)—AC
 40  */
 41 public class PEdorACPrintPDF {
 42 
 43     /**
 44      * 错误处理类,每个需要错误处理的类中都放置该类
 45      */
 46     public CErrors mErrors = new CErrors();
 47     /**
 48      * 往后面传输数据的容器
 49      */
 50     private VData mInputData = new VData();
 51     /**
 52      * 数据操作字符串
 53      */
 54     private String mOperate;
 55 
 56     /**
 57      * 业务处理相关变量
 58      */
 59     private final MMap mMap = new MMap();
 60     /**
 61      * 业务处理相关变量
 62      */
 63     private final VData mResult = new VData();
 64 
 65     private String mCurrentDate;
 66     private String mCurrentTime;
 67 
 68     private GlobalInput mG = new GlobalInput();
 69 
 70     private TransferData mTransferData = new TransferData();
 71 
 72     // 内部保单号
 73     private String mContNo = "";
 74     // 保全编码
 75     private String mEdorCode = "";
 76     private LPPolicyHolderSchema tLPPolicyHolderSchema;
 77 
 78     public PEdorACPrintPDF() {
 79         mCurrentDate = PubFun.getCurrentDate();
 80         mCurrentTime = PubFun.getCurrentTime();
 81     }
 82 
 83     /**
 84      * @param cInputData 输入的数据
 85      * @param cOperate   数据操作
 86      * @return 结果
 87      */
 88     public boolean submitData(VData cInputData, String cOperate) {
 89         this.mInputData = cInputData;
 90         this.mOperate = cOperate;
 91 
 92         if (!getInputData()) {
 93             return false;
 94         }
 95 
 96         if (!checkData()) {
 97             return false;
 98         }
 99 
100         // 进行业务处理
101         if (!dealData()) {
102             return false;
103         }
104 
105         if (!prepareOutputData()) {
106             return false;
107         }
108 
109         PubSubmit tPubSubmit = new PubSubmit();
110 
111         if (!tPubSubmit.submitData(mResult, "INSERT")) {
112             // @@错误处理
113             this.mErrors.copyAllErrors(tPubSubmit.mErrors);
114             CError tError = new CError();
115             tError.moduleName = "PEdorACDetailBL";
116             tError.functionName = "submitData";
117             tError.errorMessage = "数据提交失败!";
118             this.mErrors.addOneError(tError);
119             return false;
120         }
121         mInputData = null;
122         return true;
123     }
124 
125     /**
126      * @return 从输入数据中得到所有对象 输出:如果没有得到足够的业务数据对象,则返回false,否则返回true
127      */
128     private boolean getInputData() {
129         //获取save页面传过来的数据
130         mG = (GlobalInput) mInputData.getObjectByObjectName("GlobalInput", 0); // 按类取值
131         mTransferData = (TransferData) mInputData.getObjectByObjectName("TransferData", 0);
132         mOperate = (String) mTransferData.getValueByName("Operator");
133         tLPPolicyHolderSchema = (LPPolicyHolderSchema) mInputData.getObjectByObjectName("LPPolicyHolderSchema", 0);
134         return true;
135     }
136 
137     /**
138      * @return 从输入数据中得到所有对象 输出:如果没有得到足够的业务数据对象,则返回false,否则返回true
139      */
140     private boolean checkData() {
141         return true;
142     }
143 
144     /**
145      * @return 从输入数据中得到所有对象 输出:如果没有得到足够的业务数据对象,则返回false,否则返回true
146      */
147     private boolean dealData(){
148         try {
149             Document document = new Document();
150             //  Document document = new Document(PageSize.A4, 10, 10, 10, 10);
151             PdfWriter.getInstance(document, new FileOutputStream("C:\\YongHeGong\\pdfFile\\poi.PDF"));
152             //添加页码
153             BaseFont baseFontYeMa = BaseFont.createFont("C:\\Windows\\Fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
154             //可能使用到的字体
155             Font fontYeMa = new Font(baseFontYeMa, 14, Font.NORMAL);
156 
157             HeaderFooter footer = new HeaderFooter(new Phrase("第", fontYeMa), new Phrase("页", fontYeMa));
158             footer.setBorder(Rectangle.NO_BORDER);
159             // footer.setLeft(50f);
160             footer.setAlignment(1);//页码居中
161             document.setFooter(footer);
162 
163             document.open();
164             BaseFont baseFont = BaseFont.createFont("C:\\Windows\\Fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
165             //可能使用到的字体
166             Font font = new Font(baseFont, 14, Font.NORMAL);
167             Font fontTitle1 = new Font(baseFont, 20, Font.NORMAL);
168             Font fontTitle2 = new Font(baseFont, 20, Font.BOLD);
169             Font fontTableTitle = new Font(baseFont, 10, Font.BOLD);
170             Font fontTable = new Font(baseFont, 10, Font.NORMAL);
171             //添加图片
172             Image jpg = Image.getInstance("C:\\YongHeGong\\pdfFile\\logo.jpg");
173             jpg.setAlignment(Image.LEFT);
174             document.add(jpg);
175 
176             Paragraph tParagraph = new Paragraph("太平财产保险有限公司", fontTitle1);
177             tParagraph.setAlignment(1);
178             document.add(tParagraph);
179 
180             Paragraph mParagraph = new Paragraph("批改申请书", fontTitle2);
181             mParagraph.setAlignment(1);
182             document.add(mParagraph);
183             //添加空行
184             //   document.add(new Paragraph("\n"));
185 
186             document.add(new Paragraph("被保险人:乔峰", font));
187             document.add(new Paragraph("\n"));
188             document.add(new Paragraph("保单号码:201909251128", font));
189 
190 
191             //下滑线
192             LineSeparator UNDERLINE = new LineSeparator(1, 100, null, Element.ALIGN_CENTER, -2);
193             Paragraph p1 = new Paragraph("                                                             ");//当然下划线上面可以添加字体
194             p1.add(UNDERLINE);
195             document.add(p1);
196 
197             document.add(new Paragraph("\n"));
198             PdfPCell cell = new PdfPCell();
199             float[] width = {1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f};
200             PdfPTable tablePendInfos = new PdfPTable(width);
201             tablePendInfos.setWidthPercentage(99);// 设置宽度百分比
202 
203 
204             cell = new PdfPCell(new Paragraph("Plan ID", fontTableTitle));
205             cell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
206             cell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
207             tablePendInfos.addCell(cell);
208             //注意:不能采用这种方式进行循环
209             //这是一行一行的显示 显示19个测试数据1
210       /*  for (int i = 1; i < 20; i++) {
211             cell1 = new PdfPCell(new Paragraph("测试数据1", fontTable));
212             cell1.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
213             tablePendInfos.addCell(cell1);
214 
215         }*/
216 
217 
218             cell = new PdfPCell(new Paragraph("Primary Member ID", fontTableTitle));
219             cell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
220             cell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
221             tablePendInfos.addCell(cell);
222 
223 
224             cell = new PdfPCell(new Paragraph("Member ID", fontTableTitle));
225             cell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
226             cell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
227             tablePendInfos.addCell(cell);
228 
229 
230             cell = new PdfPCell(new Paragraph("Member Name", fontTableTitle));
231             cell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
232             cell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
233             tablePendInfos.addCell(cell);
234 
235 
236             cell = new PdfPCell(new Paragraph("ID Number", fontTableTitle));
237             cell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
238             cell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
239             tablePendInfos.addCell(cell);
240       /*  for (int i = 1; i < 20; i++) {
241             cell5 = new PdfPCell(new Paragraph("测试数据2", fontTable));
242             cell5.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
243             tablePendInfos.addCell(cell5);
244         }*/
245 
246 
247             cell = new PdfPCell(new Paragraph("DOB", fontTableTitle));
248             cell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
249             cell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
250             tablePendInfos.addCell(cell);
251 
252 
253             cell = new PdfPCell(new Paragraph("Gender", fontTableTitle));
254             cell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
255             cell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
256             tablePendInfos.addCell(cell);
257 
258 
259             cell = new PdfPCell(new Paragraph("Add or Terminate", fontTableTitle));
260             cell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
261             cell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
262             tablePendInfos.addCell(cell);
263 
264 
265             cell = new PdfPCell(new Paragraph("Effective Date", fontTableTitle));
266             cell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
267             cell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
268             tablePendInfos.addCell(cell);
269 
270 
271             cell = new PdfPCell(new Paragraph("Premium Amount", fontTableTitle));
272             cell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
273             cell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
274             tablePendInfos.addCell(cell);
275 
276             for (int i = 1; i <= 50; i++) {
277 
278                 for (int j = 1; j <= 10; j++) {
279 
280                     cell = new PdfPCell(new Paragraph("测试数据" + j + "-" + i, fontTable));
281                     cell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
282                     tablePendInfos.addCell(cell);
283 
284                 }
285             }
286         /*tElements = Root_Element.getChildren("PU");
287 
288         for (int i = 0; i < tElements.size(); i++) {
289             Element e = tElements.get(i);
290             List<Element> lists = e.getChildren("R");
291             for(int j = 0; j < lists.size(); j++){
292                 Element tt = lists.get(j);
293                 List<Element> listElement = tt.getChildren();
294                 for(int k = 0; k < listElement.size(); k++){
295                     Element tttElement = listElement.get(k);
296                     cell = new PdfPCell(new Paragraph(tttElement.getTextTrim(), FontChinese));
297                     cell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
298                     tablePendInfos.addCell(cell);
299                 }
300             }
301         }*/
302             document.add(tablePendInfos);
303             // 下面加个空行
304             document.add(new Paragraph("\n"));
305             document.add(new Paragraph(space(54) + "变更申请人:习大大", font));
306             document.add(new Paragraph("\n"));
307             LineSeparator UNDERLINE4 = new LineSeparator(1, 100, null, Element.ALIGN_CENTER, -2);
308             Paragraph p4 = new Paragraph(space(58) + "2019年09月25日", font);//当然下划线上面可以添加字体
309             p4.add(UNDERLINE4);
310             document.add(p4);
311             //    document.add(new Paragraph(space(58) + "2019年09月25日", font));
312             //   document.add(new Paragraph("\n"));
313             //下滑线
314      /*   LineSeparator UNDERLINE2 = new LineSeparator(1, 100, null, Element.ALIGN_CENTER, -2);
315         Paragraph p2 = new Paragraph("                                                             ");//当然下划线上面可以添加字体
316         p2.add(UNDERLINE2);
317         document.add(p2);*/
318             document.add(new Paragraph("核保意见:通过", font));
319             // document.add(new Paragraph("\n"));
320             //   document.add(new Paragraph(space(40) + "核保人:习大大"+space(5)+"2019年09月25日", font));
321 
322             //下滑线
323             LineSeparator UNDERLINE3 = new LineSeparator(1, 100, null, Element.ALIGN_CENTER, -2);
324             Paragraph p3 = new Paragraph(space(40) + "核保人:习大大" + space(5) + "2019年09月25日", font);//当然下划线上面可以添加字体
325             p3.add(UNDERLINE3);
326             document.add(p3);
327 
328             document.add(new Paragraph("经办人:习大大" + space(45) + "2019年09月25日", font));
329 
330             document.close();
331 
332 
333         } catch (DocumentException e) {
334             e.printStackTrace();
335         } catch (IOException e) {
336             e.printStackTrace();
337         }
338         return true;
339     }
340 
341     /**
342      * @return 从输入数据中得到所有对象 输出:如果没有得到足够的业务数据对象,则返回false,否则返回true
343      */
344     private boolean prepareOutputData() {
345         try {
346             this.mInputData = new VData();
347             this.mInputData.add(this.mMap);
348             mResult.add(mMap);
349         } catch (Exception ex) {
350             // @@错误处理
351             CError tError = new CError();
352             tError.moduleName = "BQAcceptApplyPoolBL";
353             tError.functionName = "prepareOutputData";
354             tError.errorMessage = "在准备往后层处理所需要的数据时出错。";
355             this.mErrors.addOneError(tError);
356             return false;
357         }
358         return true;
359     }
360 
361     /**
362      * @return 结果
363      */
364     public VData getResult() {
365         return this.mResult;
366     }
367 
368     /**
369      * 输出指定数量空格
370      *
371      * @param i
372      * @return
373      */
374     public static String space(int i) {
375         StringBuffer sb = new StringBuffer();
376         for (int a = 0; a < i; a++) {
377             sb.append(" ");
378         }
379         return sb.toString();
380     }
381 
382     @Test
383     public void test() {
384         String tt = PubFun1.CreateMaxNo("EDORACCEPTNO", PubFun.getCurrentDate());
385         System.out.println("------------- " + tt);
386     }
387 }

我遇到的问题:

    BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);//我一开始写的

   报错:error message:
              Font ‘STSong-Light‘ with ‘UniGB-UCS2-H‘ is not recognized.

BaseFont baseFontYeMa = BaseFont.createFont("C:\\Windows\\Fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);//推荐使用

   小总结:报这个错的原因是jar中的文件路径不对

    附图:

 

             技术分享图片

 

 

     意思是要将lowagie改成itextpdf,也就是路径不对!哈哈,原来sun公司的开发人员也。。。。。。。啊!

     但是你如果一改了路径,你代码中的cell类绝对就引用不到了,所以,我建议,先将这些类都引用进来,网友们说jar包版本不对,有的说用这两个,有的说用那两个,我因为jar         包   问题搞了半天,也没有搞出来,当然我们百度,要学会以最快的速度解决问题,下来再慢慢研究原因,所以我建议直接引用本地的字体,这样省事多了,再说将两个文件改         成 相同的名字,你也改不了呀,哈哈!

 

最终实现效果:

           技术分享图片

 

                技术分享图片

 

               技术分享图片

 

Java代码写PDF-保全批单

原文:https://www.cnblogs.com/dongyaotou/p/11586878.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!