iText5 表格的生成

直接上代码-

package com.scm.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;


import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.scm.model.Somain;

public class PdfSomainUtil {

    public static void DownloadPdf(List<Somain> list) {
        File file = new File("itext.pdf");//文件名为itext.pdf, 默认路径下。
        Document document = new Document();
        BaseFont bf = null;
        Font fontChinese = null;
        Font fontChar=new Font();
        fontChar.setSize(7);
        fontChar.setColor(BaseColor.DARK_GRAY);
        try {
            bf = BaseFont.createFont(
                    "D:\\myprojects\\scm\\WebRoot\\resource\\STXIHEI.TTF",//windows自带的字体文件,我放在了resource文件下
                    BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            fontChinese = new Font(bf, 10, Font.NORMAL);// 中文字体
            fontChinese.setSize(8);//字体大小
            PdfWriter pdfWriter=PdfWriter.getInstance(document, new FileOutputStream(file));// 文档输出流。
            document.open();
            PdfPTable table = new PdfPTable(new float[] {28,19, 15,27, 12,15, 14, 16});// 8列的表格以及单元格的宽度。
            table.setPaddingTop(2);// 顶部空白区高度
            table.setTotalWidth(360);//表格整体宽度
            PdfPCell cell;

            cell = new PdfPCell(new Phrase("Details of past one month on sale."));


            cell.setColspan(8);//占据八列
            cell.setRowspan(2);
            table.addCell(cell);
            table.addCell(new Paragraph("销售单号", fontChinese));
            table.addCell(new Paragraph("客户编号", fontChinese));
            table.addCell(new Paragraph("客户名称", fontChinese));
            table.addCell(new Paragraph("销售日期", fontChinese));
            table.addCell(new Paragraph("经手人", fontChinese));
            table.addCell(new Paragraph("总金额", fontChinese));
            table.addCell(new Paragraph("预付款", fontChinese));
            table.addCell(new Paragraph("购买方式", fontChinese));
            for(Somain s :list){//将集合内的对象循环写入到表格
                table.addCell(new Paragraph(s.getSoId(),fontChar));
                table.addCell(new Paragraph(s.getCustomerCode(),fontChar));
                table.addCell(new Paragraph(s.getName(),fontChar));
                table.addCell(new Paragraph(s.getCreateTime(),fontChar));
                table.addCell(new Paragraph(s.getAccount(),fontChar));
                table.addCell(new Paragraph(String.valueOf(s.getSoTotal()),fontChar));
                table.addCell(new Paragraph(String.valueOf(s.getPrePayFee()),fontChar));
                if(Integer.parseInt(s.getPayType())==1){
                    table.addCell(new Paragraph("预付款发货", fontChinese));
                }else if(Integer.parseInt(s.getPayType())==0){
                    table.addCell(new Paragraph("货到付款", fontChinese));
                }else{
                    table.addCell(new Paragraph("款到发货", fontChinese));
                }

            }



            document.add(table);
            document.close();
            pdfWriter.flush();
            System.out.println("document itext pdf write finished...100%");
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    



}


----------------------------------------------------------------------------------------------------------分隔线

为什么选择iText5不去选择iText7?

iText5稳定,社区活力好于iText7.iText7对中文的支持操作起来仍然麻烦。不过在本文中iTextAsian这个包我是引入进来了。


这种代码没啥好看的,直接用,按需修改就好。





  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值