利用iText相关jar包进行pdf绘制

工作中有需求需要将回单进行绘制生成pdf以便打印,所以记录下来

准备工作:

相关jar包:
示例:
iText-2.1.7.jar //核心jar
字体包:
iTextAsian.jar
iTextAsianCmaps.jar

ps:不同版本的iText的功能有所差异


Java类示例:

package com.test;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public class TestCreatePdf {
    public static void main(String[] args) throws Exception {
        try {
            String FileName = "C:\\Users\\Administrator.SC-201606041031\\Desktop\\test.pdf";
            Map map = getMap();

            File srcFile = new File(FileName);
            byte[] fileByte = new byte[1024];

            FileName = srcFile.getName();

            BufferedInputStream bufferedInputStream;
            bufferedInputStream = new BufferedInputStream(new FileInputStream(srcFile));

            ByteArrayOutputStream out = new ByteArrayOutputStream();

            while (bufferedInputStream.read(fileByte) != -1) {
                out.write(fileByte);
            }

            BaseFont   bf = BaseFont.createFont( "STSong-Light", "UniGB-UCS2-H",  BaseFont.NOT_EMBEDDED);//创建字体 
            Font  font = new Font(bf,9,Font.BOLD);//使用字体 
            Font  font1 = new Font(bf,8);//使用字体
            Font font2 = new Font(bf,12,Font.BOLD);//使用字体
            Document document = new Document(); 
            PdfWriter.getInstance(document, out);
              try { 
                  document.open(); 
                  Image image = Image.getInstance("C:\\Users\\Administrator.SC-201606041031\\Desktop\\101.png");
                  //获取印章图片路径
                  Image image1 = Image.getInstance("C:\\Users\\Administrator.SC-201606041031\\Desktop\\101.png");
                  image.setAlignment(Image.ALIGN_LEFT);
                   image.scaleAbsolute(200f, 30f);
                   image1.setAlignment(Image.ALIGN_LEFT);
                   image1.scaleAbsolute(75, 55);

                  float[] widths = {15f,25f,15f ,25f,15f,25f};// 设置表格的列宽和列数 默认是6列   
                  float[] midWidths = {15f,30f,15f,30f};
                  PdfPTable table = new PdfPTable(widths);
                  table.setSpacingBefore(20f);   
                  table.setWidthPercentage(100);   

                  float[] widths1 = {10f,10f,40f ,10f,10f,40f};
                  PdfPTable table1 = new PdfPTable(widths1); 
                  table1.setSpacingBefore(20f);   
                  table1.setWidthPercentage(100);// 设置表格宽度为100%   

                  // 第一行 
               // 第一行 
                  PdfPCell cell = null;

                  cell = new PdfPCell(image);//   
                  cell.setHorizontalAlignment(Element.ALIGN_LEFT); 
                  cell.setVerticalAlignment(Element.ALIGN_CENTER); //单元垂直对齐方式
                  cell.setBorderWidth(0);
                  cell.setPaddingBottom(15);
                  cell.setColspan(3);
                  table.addCell(cell); 

                  cell = new PdfPCell(new Paragraph("电子回单",font2));
                  cell.setHorizontalAlignment(Element.ALIGN_LEFT); 
                  cell.setVerticalAlignment(Element.ALIGN_LEFT);
                  cell.setBorderWidth(0);
                  cell.setPaddingTop(15);
                  cell.setColspan(3);
                  table.addCell(cell);

                  cell = new PdfPCell(new Paragraph("业务类型",font));
                  cell.setHorizontalAlignment(Element.ALIGN_CENTER); 
                  cell.setVerticalAlignment(Element.ALIGN_CENTER);
                  table.addCell(cell); 

                  cell = new PdfPCell(new Paragraph(map.get("TransChannel").toString(),font1));
                  cell.setHorizontalAlignment(Element.ALIGN_LEFT); 
                  cell.setVerticalAlignment(Element.ALIGN_CENTER);
                  table.addCell(cell); 

                  cell = new PdfPCell(new Paragraph("记账日期",font));
                  cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                  cell.setVerticalAlignment(Element.ALIGN_CENTER);
                  table.addCell(cell); 

                  cell = new PdfPCell(new Paragraph(map.get("TransDate").toString(),font1));   
                  cell.setHorizontalAlignment(Element.ALIGN_LEFT); 
                  cell.setVerticalAlignment(Element.ALIGN_CENTER);
                  table.addCell(cell);

                  cell = new PdfPCell(new Paragraph("回单编号",font));
                  cell.setHorizontalAlignment(Element.ALIGN_CENTER); 
                  cell.setVerticalAlignment(Element.ALIGN_CENTER);
                  table.addCell(cell); 

                  cell = new PdfPCell(new Paragraph(map.get("TransJnlNo").toString(),font1));   
                  cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                  cell.setVerticalAlignment(Element.ALIGN_CENTER);
                  table.addCell(cell);

                  PdfPTable midTable = new PdfPTable(midWidths);
                 //第二行
                  cell = new PdfPCell(new Paragraph("代发工资名称",font));//   
                  cell.setHorizontalAlignment(Element.ALIGN_CENTER); 
                  cell.setVerticalAlignment(Element.ALIGN_CENTER);
                  midTable.addCell(cell); 

                  cell = new PdfPCell(new Paragraph(map.get("TransName").toString(),font1));
                  cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                  cell.setVerticalAlignment(Element.ALIGN_CENTER);
                  midTable.addCell(cell);

                  cell = new PdfPCell(new Paragraph("付款账号",font));
                  cell.setHorizontalAlignment(Element.ALIGN_CENTER); 
                  cell.setVerticalAlignment(Element.ALIGN_CENTER);
                  midTable.addCell(cell); 

                  cell = new PdfPCell(new Paragraph(map.get("TransName").toString(),font1));
                  cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                  cell.setVerticalAlignment(Element.ALIGN_CENTER);
                  midTable.addCell(cell);
                  //第三行
                  cell = new PdfPCell(new Paragraph("总笔数",font));//   
                  cell.setHorizontalAlignment(Element.ALIGN_CENTER); 
                  cell.setVerticalAlignment(Element.ALIGN_CENTER);
                  midTable.addCell(cell); 

                  cell = new PdfPCell(new Paragraph(map.get("TransName").toString(),font1));
                  cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                  cell.setVerticalAlignment(Element.ALIGN_CENTER);
                  midTable.addCell(cell);

                  cell = new PdfPCell(new Paragraph("总金额",font));
                  cell.setHorizontalAlignment(Element.ALIGN_CENTER); 
                  cell.setVerticalAlignment(Element.ALIGN_CENTER);
                  midTable.addCell(cell); 

                  cell = new PdfPCell(new Paragraph(map.get("TransName").toString(),font1));
                  cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                  cell.setVerticalAlignment(Element.ALIGN_CENTER);
                  midTable.addCell(cell);
                  //第四行
                  cell = new PdfPCell(new Paragraph("总手续费",font));//   
                  cell.setHorizontalAlignment(Element.ALIGN_CENTER); 
                  cell.setVerticalAlignment(Element.ALIGN_CENTER);
                  midTable.addCell(cell); 

                  cell = new PdfPCell(new Paragraph(map.get("TransName").toString(),font1));
                  cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                  cell.setVerticalAlignment(Element.ALIGN_CENTER);
                  midTable.addCell(cell);

                  cell = new PdfPCell(new Paragraph("交易日期",font));
                  cell.setHorizontalAlignment(Element.ALIGN_CENTER); 
                  cell.setVerticalAlignment(Element.ALIGN_CENTER);
                  midTable.addCell(cell); 

                  cell = new PdfPCell(new Paragraph(map.get("TransName").toString(),font1));
                  cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                  cell.setVerticalAlignment(Element.ALIGN_CENTER);
                  midTable.addCell(cell);
                  //第五行
                  cell = new PdfPCell(new Paragraph("成功笔数",font));//   
                  cell.setHorizontalAlignment(Element.ALIGN_CENTER); 
                  cell.setVerticalAlignment(Element.ALIGN_CENTER);
                  midTable.addCell(cell); 

                  cell = new PdfPCell(new Paragraph(map.get("TransName").toString(),font1));
                  cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                  cell.setVerticalAlignment(Element.ALIGN_CENTER);
                  midTable.addCell(cell);

                  cell = new PdfPCell(new Paragraph("成功金额",font));
                  cell.setHorizontalAlignment(Element.ALIGN_CENTER); 
                  cell.setVerticalAlignment(Element.ALIGN_CENTER);
                  midTable.addCell(cell); 

                  cell = new PdfPCell(new Paragraph(map.get("TransName").toString(),font1));
                  cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                  cell.setVerticalAlignment(Element.ALIGN_CENTER);
                  midTable.addCell(cell);
                  //第六行
                  cell = new PdfPCell(new Paragraph("失败笔数",font));//   
                  cell.setHorizontalAlignment(Element.ALIGN_CENTER); 
                  cell.setVerticalAlignment(Element.ALIGN_CENTER);
                  midTable.addCell(cell); 

                  cell = new PdfPCell(new Paragraph(map.get("TransName").toString(),font1));
                  cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                  cell.setVerticalAlignment(Element.ALIGN_CENTER);
                  midTable.addCell(cell);

                  cell = new PdfPCell(new Paragraph("失败金额",font));
                  cell.setHorizontalAlignment(Element.ALIGN_CENTER); 
                  cell.setVerticalAlignment(Element.ALIGN_CENTER);
                  midTable.addCell(cell); 

                  cell = new PdfPCell(new Paragraph(map.get("TransName").toString(),font1));
                  cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                  cell.setVerticalAlignment(Element.ALIGN_CENTER);
                  midTable.addCell(cell);
                  //第7行
                  cell = new PdfPCell(new Paragraph("批次状态",font));//   
                  cell.setHorizontalAlignment(Element.ALIGN_CENTER); 
                  cell.setVerticalAlignment(Element.ALIGN_CENTER);
                  midTable.addCell(cell); 

                  cell = new PdfPCell(new Paragraph(map.get("TransName").toString(),font1));
                  cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                  cell.setVerticalAlignment(Element.ALIGN_CENTER);
                  midTable.addCell(cell);

                  cell = new PdfPCell(new Paragraph("授权状态",font));
                  cell.setHorizontalAlignment(Element.ALIGN_CENTER); 
                  cell.setVerticalAlignment(Element.ALIGN_CENTER);
                  midTable.addCell(cell); 

                  cell = new PdfPCell(new Paragraph(map.get("TransName").toString(),font1));
                  cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                  cell.setVerticalAlignment(Element.ALIGN_CENTER);
                  cell.setColspan(2);
                  midTable.addCell(cell);
                  cell = new PdfPCell(midTable);
                  cell.setColspan(6);
                  table.addCell(cell);

                  cell = new PdfPCell(new Paragraph("附加信息及用途 :",font));   
                  cell.setHorizontalAlignment(Element.ALIGN_LEFT); 
                  cell.setVerticalAlignment(Element.ALIGN_CENTER);
                  cell.setBorderWidthBottom(0);
                  cell.setColspan(3);
                  table.addCell(cell); 

                  cell = new PdfPCell(new Paragraph("银行盖章:",font));
                  cell.setHorizontalAlignment(Element.ALIGN_LEFT); 
                  cell.setVerticalAlignment(Element.ALIGN_CENTER);
                  cell.setBorderWidthBottom(0);
                  cell.setColspan(3);
                  table.addCell(cell);


                  cell = new PdfPCell(new Paragraph(map.get("Remark").toString(),font1));
                  cell.setHorizontalAlignment(Element.ALIGN_LEFT); 
                  cell.setVerticalAlignment(Element.ALIGN_CENTER);
                  cell.setBorderWidthTop(0);
                  cell.setColspan(3);
                  table.addCell(cell); 

                //处理图章显示
                  float[] widths2 = {25f,15f,25f};
                  PdfPTable sealTable = new PdfPTable(widths2);

                  cell = new PdfPCell(image1);
                  cell.setHorizontalAlignment(Element.ALIGN_RIGHT); 
                  cell.setVerticalAlignment(Element.ALIGN_RIGHT);
                  cell.setBorderWidth(0);
                  cell.setPaddingRight(5);
                  cell.setPaddingBottom(5);
                  cell.setColspan(3);
                  sealTable.addCell(cell);

                  cell = new PdfPCell();
                  cell.setHorizontalAlignment(Element.ALIGN_RIGHT); 
                  cell.setVerticalAlignment(Element.ALIGN_RIGHT);
                  cell.setBorderWidthTop(0);
                  cell.setColspan(3);
                  sealTable.setHorizontalAlignment(Element.ALIGN_RIGHT);
                  cell.addElement(sealTable);
                  table.addCell(cell);

                //第九行
                  cell = new PdfPCell(new Paragraph("重要提示:本回单不作为收付款方交易确认的最终依据。",font));   
                  cell.setHorizontalAlignment(Element.ALIGN_LEFT); 
                  cell.setVerticalAlignment(Element.ALIGN_CENTER);
                  cell.setColspan(6);
                  cell.setBorderWidth(0);
                  table.addCell(cell);
                  cell = new PdfPCell();
                  cell.setColspan(6);
                  cell.setPaddingBottom(15);
                  cell.setBorderWidth(0);
                  table.addCell(cell);


                  float[] lastWidths = {15f,25f,15f ,25f,15f,25f};// 设置表格的列宽和列数 默认是6列   
                  PdfPTable lastTable = new PdfPTable(lastWidths);
                  table.setSpacingBefore(20f);   
                  table.setWidthPercentage(100);

                  for(int i=0;i<10;i++){
                      cell = new PdfPCell(new Paragraph("名字a",font));
                      cell.setHorizontalAlignment(Element.ALIGN_CENTER); 
                      cell.setVerticalAlignment(Element.ALIGN_CENTER);
                      lastTable.addCell(cell); 

                      cell = new PdfPCell(new Paragraph(map.get("TransChannel").toString(),font1));
                      cell.setHorizontalAlignment(Element.ALIGN_LEFT); 
                      cell.setVerticalAlignment(Element.ALIGN_CENTER);
                      lastTable.addCell(cell); 

                      cell = new PdfPCell(new Paragraph("名字b",font));
                      cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                      cell.setVerticalAlignment(Element.ALIGN_CENTER);
                      lastTable.addCell(cell); 

                      cell = new PdfPCell(new Paragraph(map.get("TransDate").toString(),font1));   
                      cell.setHorizontalAlignment(Element.ALIGN_LEFT); 
                      cell.setVerticalAlignment(Element.ALIGN_CENTER);
                      lastTable.addCell(cell);

                      cell = new PdfPCell(new Paragraph("名字c",font));
                      cell.setHorizontalAlignment(Element.ALIGN_CENTER); 
                      cell.setVerticalAlignment(Element.ALIGN_CENTER);
                      lastTable.addCell(cell); 

                      cell = new PdfPCell(new Paragraph(map.get("TransJnlNo").toString(),font1));   
                      cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                      cell.setVerticalAlignment(Element.ALIGN_CENTER);
                      lastTable.addCell(cell);
                  }

                  document.add(table);
                  document.add(lastTable);
                  document.close();
                  if(bufferedInputStream!=null)
                  bufferedInputStream.close();
             } catch (Exception e) { 
                 System.out.println("file create exception" + e); 
             }
             String resultPath="C:\\Users\\Administrator.SC-201606041031\\Desktop\\result1.pdf";
              OutputStream os = new FileOutputStream(resultPath);
              os.write(out.toByteArray());
              os.flush();
              os.close();
              out.close();

        }  catch (IOException e) {

        } catch (DocumentException e1) {
            e1.printStackTrace();
        }
    }
    public static Map getMap(){
        Map map = new HashMap<>();
        map.put("DCFlag","C");
        map.put("DCFlagKey","test");
        map.put("TransName","test");
        map.put("TransDate","test");
        map.put("TransJnlNo","test");
        map.put("JnlOtherAcName","test");
        map.put("JnlAcName","test");
        map.put("JnlOtherAcNo","test");
        map.put("JnlAcNo","test");
        map.put("Currency","test");
        map.put("OtherBankName","test");
        map.put("Amount","test");
        map.put("AmountFmt","test");
        map.put("TransChannel","test");
        map.put("Remark","test");
        map.put("TransDeptName","test");
        map.put("TransDeptId","test");

        return map;
    }
}


猜你喜欢

转载自blog.csdn.net/csdn15679160266/article/details/81055068