zoukankan      html  css  js  c++  java
  • 使用itext生成pdf的,各种布局

    代码如下,jar包为itext.jar,itextAsia.jar,最好都是最新的 ;2张图片也在最后贴出,把图片放到D盘可以直接生成制定格式的pdf。
    最后生成的pdf如下:
    这里写图片描述

    代码如下:

      1 package com.itext.test;
      2 
      3 import java.io.FileOutputStream;
      4 
      5 import com.itextpdf.text.BaseColor;
      6 import com.itextpdf.text.Document;
      7 import com.itextpdf.text.Element;
      8 import com.itextpdf.text.Image;
      9 import com.itextpdf.text.PageSize;
     10 import com.itextpdf.text.Paragraph;
     11 import com.itextpdf.text.pdf.BaseFont;
     12 import com.itextpdf.text.pdf.PdfPCell;
     13 import com.itextpdf.text.pdf.PdfPTable;
     14 import com.itextpdf.text.pdf.PdfWriter;
     15 
     16 
     17 public class BaoXiaoDan {
     18     public static void main(String[] args) {
     19 
     20         try 
     21         {
     22              Document document = new Document(PageSize.A4.rotate()); 
     23              PdfWriter.getInstance(document, new FileOutputStream("D:\Helloworld.PDF"));
     24 
     25             //设置字体
     26             BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);   
     27             com.itextpdf.text.Font FontChinese24 = new com.itextpdf.text.Font(bfChinese, 24, com.itextpdf.text.Font.BOLD);
     28             com.itextpdf.text.Font FontChinese18 = new com.itextpdf.text.Font(bfChinese, 18, com.itextpdf.text.Font.BOLD); 
     29             com.itextpdf.text.Font FontChinese16 = new com.itextpdf.text.Font(bfChinese, 16, com.itextpdf.text.Font.BOLD);
     30             com.itextpdf.text.Font FontChinese12 = new com.itextpdf.text.Font(bfChinese, 12, com.itextpdf.text.Font.NORMAL);
     31             com.itextpdf.text.Font FontChinese11Bold = new com.itextpdf.text.Font(bfChinese, 11, com.itextpdf.text.Font.BOLD);
     32             com.itextpdf.text.Font FontChinese11 = new com.itextpdf.text.Font(bfChinese, 11, com.itextpdf.text.Font.ITALIC);
     33             com.itextpdf.text.Font FontChinese11Normal = new com.itextpdf.text.Font(bfChinese, 11, com.itextpdf.text.Font.NORMAL);
     34 
     35             document.open();
     36             //table1
     37             PdfPTable table1 = new PdfPTable(3);
     38             PdfPCell cell11 = new PdfPCell(new Paragraph("费用报销",FontChinese24));
     39             cell11.setVerticalAlignment(Element.ALIGN_MIDDLE);
     40             cell11.setHorizontalAlignment(Element.ALIGN_CENTER);
     41             cell11.setBorder(0);
     42             String imagePath = "D:/alibaba.jpg";
     43             Image image1 = Image.getInstance(imagePath); 
     44 
     45             Image image2 = Image.getInstance(imagePath); 
     46             //设置每列宽度比例   
     47             int width11[] = {35,40,25};
     48             table1.setWidths(width11); 
     49             table1.getDefaultCell().setBorder(0);
     50             table1.addCell(image1);  
     51             table1.addCell(cell11);  
     52             table1.addCell(image2);
     53             document.add(table1);
     54             //加入空行
     55             Paragraph blankRow1 = new Paragraph(18f, " ", FontChinese18); 
     56             document.add(blankRow1);
     57 
     58             //table2
     59             PdfPTable table2 = new PdfPTable(2);
     60             //设置每列宽度比例   
     61             int width21[] = {2,98};
     62             table2.setWidths(width21); 
     63             table2.getDefaultCell().setBorder(0);
     64             PdfPCell cell21 = new PdfPCell(new Paragraph("报销概要",FontChinese16));
     65             String imagePath2 = "D:/boder.jpg";
     66             Image image21 = Image.getInstance(imagePath2); 
     67             cell21.setBorder(0);
     68             table2.addCell(image21);
     69             table2.addCell(cell21); 
     70             document.add(table2);
     71             //加入空行
     72             Paragraph blankRow2 = new Paragraph(18f, " ", FontChinese18); 
     73             document.add(blankRow2);
     74 
     75             //table3
     76             PdfPTable table3 = new PdfPTable(3);
     77             int width3[] = {40,35,25};
     78             table3.setWidths(width3); 
     79             PdfPCell cell31 = new PdfPCell(new Paragraph("申请人:"+"XXX",FontChinese11Normal));
     80             PdfPCell cell32 = new PdfPCell(new Paragraph("日期:"+"2011-11-11",FontChinese11Normal));
     81             PdfPCell cell33 = new PdfPCell(new Paragraph("报销单号:"+"123456789",FontChinese11Normal));
     82             cell31.setBorder(0);
     83             cell32.setBorder(0);
     84             cell33.setBorder(0);
     85             table3.addCell(cell31);
     86             table3.addCell(cell32);
     87             table3.addCell(cell33);
     88             document.add(table3);
     89             //加入空行
     90             Paragraph blankRow31 = new Paragraph(18f, " ", FontChinese11); 
     91             document.add(blankRow31);
     92 
     93             //table4
     94             PdfPTable table4 = new PdfPTable(2);
     95             int width4[] = {40,60};
     96             table4.setWidths(width4); 
     97             PdfPCell cell41 = new PdfPCell(new Paragraph("公司:"+"XXX",FontChinese11Normal));
     98             PdfPCell cell42 = new PdfPCell(new Paragraph("部门:"+"XXX",FontChinese11Normal));
     99             cell41.setBorder(0);
    100             cell42.setBorder(0);
    101             table4.addCell(cell41);
    102             table4.addCell(cell42);
    103             document.add(table4);
    104             //加入空行
    105             Paragraph blankRow41 = new Paragraph(18f, " ", FontChinese11); 
    106             document.add(blankRow41);
    107 
    108             //table5
    109             PdfPTable table5 = new PdfPTable(1);
    110             PdfPCell cell51 = new PdfPCell(new Paragraph("报销说明:"+"XXX",FontChinese11));
    111             cell51.setBorder(0);
    112             table5.addCell(cell51);
    113             document.add(table5);
    114             //加入空行
    115             Paragraph blankRow51 = new Paragraph(18f, " ", FontChinese18); 
    116             document.add(blankRow51);
    117 
    118             //table6
    119             PdfPTable table6 = new PdfPTable(2);
    120             table6.getDefaultCell().setBorder(0);
    121             table6.setWidths(width21); 
    122             PdfPCell cell61 = new PdfPCell(new Paragraph("报销明细",FontChinese16));
    123             cell61.setBorder(0);
    124             table6.addCell(image21);
    125             table6.addCell(cell61); 
    126             document.add(table6);
    127             //加入空行
    128             Paragraph blankRow4 = new Paragraph(18f, " ", FontChinese16); 
    129             document.add(blankRow4);
    130 
    131             //table7
    132             PdfPTable table7 = new PdfPTable(6);
    133             BaseColor lightGrey = new BaseColor(0xCC,0xCC,0xCC);
    134             int width7[] = {20,18,13,20,14,15};
    135             table7.setWidths(width7); 
    136             PdfPCell cell71 = new PdfPCell(new Paragraph("费用类型",FontChinese11Bold));
    137             PdfPCell cell72 = new PdfPCell(new Paragraph("费用发生时间",FontChinese11Bold));
    138             PdfPCell cell73 = new PdfPCell(new Paragraph("详细信息",FontChinese11Bold));
    139             PdfPCell cell74 = new PdfPCell(new Paragraph("消费金币/币种",FontChinese11Bold));
    140             PdfPCell cell75 = new PdfPCell(new Paragraph("报销汇率",FontChinese11Bold));
    141             PdfPCell cell76 = new PdfPCell(new Paragraph("报销金额",FontChinese11Bold));
    142             //表格高度
    143             cell71.setFixedHeight(25);
    144             cell72.setFixedHeight(25);
    145             cell73.setFixedHeight(25);
    146             cell74.setFixedHeight(25);
    147             cell75.setFixedHeight(25);
    148             cell76.setFixedHeight(25);
    149             //水平居中
    150             cell71.setHorizontalAlignment(Element.ALIGN_CENTER);
    151             cell72.setHorizontalAlignment(Element.ALIGN_CENTER);
    152             cell73.setHorizontalAlignment(Element.ALIGN_CENTER);
    153             cell74.setHorizontalAlignment(Element.ALIGN_CENTER);
    154             cell75.setHorizontalAlignment(Element.ALIGN_CENTER);
    155             cell76.setHorizontalAlignment(Element.ALIGN_CENTER);
    156             //垂直居中
    157             cell71.setVerticalAlignment(Element.ALIGN_MIDDLE);
    158             cell72.setVerticalAlignment(Element.ALIGN_MIDDLE);
    159             cell73.setVerticalAlignment(Element.ALIGN_MIDDLE);
    160             cell74.setVerticalAlignment(Element.ALIGN_MIDDLE);
    161             cell75.setVerticalAlignment(Element.ALIGN_MIDDLE);
    162             cell76.setVerticalAlignment(Element.ALIGN_MIDDLE);
    163             //边框颜色
    164             cell71.setBorderColor(lightGrey);
    165             cell72.setBorderColor(lightGrey);
    166             cell73.setBorderColor(lightGrey);
    167             cell74.setBorderColor(lightGrey);
    168             cell75.setBorderColor(lightGrey);
    169             cell76.setBorderColor(lightGrey);
    170             //去掉左右边框
    171             cell71.disableBorderSide(8);
    172             cell72.disableBorderSide(4);
    173             cell72.disableBorderSide(8);
    174             cell73.disableBorderSide(4);
    175             cell73.disableBorderSide(8);
    176             cell74.disableBorderSide(4);
    177             cell74.disableBorderSide(8);
    178             cell75.disableBorderSide(4);
    179             cell75.disableBorderSide(8);
    180             cell76.disableBorderSide(4);
    181             table7.addCell(cell71);
    182             table7.addCell(cell72);
    183             table7.addCell(cell73);
    184             table7.addCell(cell74);
    185             table7.addCell(cell75);
    186             table7.addCell(cell76);
    187             document.add(table7);
    188 
    189                     //table8
    190                     PdfPTable table8 = new PdfPTable(6);
    191                     int width8[] = {20,18,13,20,14,15};
    192                     table8.setWidths(width8); 
    193                     PdfPCell cell81 = new PdfPCell(new Paragraph("差旅报销",FontChinese12));
    194                     PdfPCell cell82 = new PdfPCell(new Paragraph("2011-11-11",FontChinese12));
    195                     PdfPCell cell83 = new PdfPCell(new Paragraph("XXX",FontChinese12));
    196                     PdfPCell cell84 = new PdfPCell(new Paragraph("XXX",FontChinese12));
    197                     PdfPCell cell85 = new PdfPCell(new Paragraph("XXX",FontChinese12));
    198                     PdfPCell cell86 = new PdfPCell(new Paragraph("XXX",FontChinese12));
    199                     //表格高度
    200                     cell81.setFixedHeight(25);
    201                     cell82.setFixedHeight(25);
    202                     cell83.setFixedHeight(25);
    203                     cell84.setFixedHeight(25);
    204                     cell85.setFixedHeight(25);
    205                     cell86.setFixedHeight(25);
    206                     //水平居中
    207                     cell81.setHorizontalAlignment(Element.ALIGN_CENTER);
    208                     cell82.setHorizontalAlignment(Element.ALIGN_CENTER);
    209                     cell83.setHorizontalAlignment(Element.ALIGN_CENTER);
    210                     cell84.setHorizontalAlignment(Element.ALIGN_CENTER);
    211                     cell85.setHorizontalAlignment(Element.ALIGN_CENTER);
    212                     cell86.setHorizontalAlignment(Element.ALIGN_CENTER);
    213                     //垂直居中
    214                     cell81.setVerticalAlignment(Element.ALIGN_MIDDLE);
    215                     cell82.setVerticalAlignment(Element.ALIGN_MIDDLE);
    216                     cell83.setVerticalAlignment(Element.ALIGN_MIDDLE);
    217                     cell84.setVerticalAlignment(Element.ALIGN_MIDDLE);
    218                     cell85.setVerticalAlignment(Element.ALIGN_MIDDLE);
    219                     cell86.setVerticalAlignment(Element.ALIGN_MIDDLE);
    220                     //边框颜色
    221                     cell81.setBorderColor(lightGrey);
    222                     cell82.setBorderColor(lightGrey);
    223                     cell83.setBorderColor(lightGrey);
    224                     cell84.setBorderColor(lightGrey);
    225                     cell85.setBorderColor(lightGrey);
    226                     cell86.setBorderColor(lightGrey);
    227                     //去掉左右边框
    228                     cell81.disableBorderSide(8);
    229                     cell82.disableBorderSide(4);
    230                     cell82.disableBorderSide(8);
    231                     cell83.disableBorderSide(4);
    232                     cell83.disableBorderSide(8);
    233                     cell84.disableBorderSide(4);
    234                     cell84.disableBorderSide(8);
    235                     cell85.disableBorderSide(4);
    236                     cell85.disableBorderSide(8);
    237                     cell86.disableBorderSide(4);
    238                     table8.addCell(cell81);
    239                     table8.addCell(cell82);
    240                     table8.addCell(cell83);
    241                     table8.addCell(cell84);
    242                     table8.addCell(cell85);
    243                     table8.addCell(cell86);
    244                     document.add(table8);
    245               //加入空行
    246               Paragraph blankRow5 = new Paragraph(18f, " ", FontChinese18); 
    247               document.add(blankRow5);
    248 
    249             //table9
    250             PdfPTable table9 = new PdfPTable(3);
    251             int width9[] = {30,50,20};
    252             table9.setWidths(width9);
    253             PdfPCell cell91 = new PdfPCell(new Paragraph("",FontChinese12));
    254             PdfPCell cell92 = new PdfPCell(new Paragraph("收到的报销金额",FontChinese12));
    255             PdfPCell cell93 = new PdfPCell(new Paragraph("1000",FontChinese24));
    256             cell92.setHorizontalAlignment(Element.ALIGN_RIGHT);
    257             cell92.setVerticalAlignment(Element.ALIGN_MIDDLE);
    258             cell93.setHorizontalAlignment(Element.ALIGN_LEFT);
    259             cell93.setVerticalAlignment(Element.ALIGN_MIDDLE);
    260             cell91.setBorder(0);
    261             cell92.setBorder(0);
    262             cell93.setBorder(0);
    263             table9.addCell(cell91); 
    264             table9.addCell(cell92); 
    265             table9.addCell(cell93); 
    266             document.add(table9);
    267 
    268              document.close();
    269 
    270         } catch (Exception ex) 
    271         {
    272           ex.printStackTrace();
    273         }
    274     }
    275 }
    只有当你忍痛前行后,你猜能知道,所谓的痛不过尔尔!
  • 相关阅读:
    软件工程之项目管理核心框架
    JPA @Column
    centos 安装 nodejs vue 工具链.
    c语言 打印二进制数
    Python import 导入指定目录的某块
    最近的一点思考,关于高手/大师/学霸
    同步与非同步,阻塞与非阻塞。
    Spring MVC 配置
    Java Web框架的基本组件
    add函数
  • 原文地址:https://www.cnblogs.com/lijianli/p/9546388.html
Copyright © 2011-2022 走看看