您的位置:首页 > 其它

iText5 表格的生成

2016-07-14 22:07 267 查看
直接上代码-

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这个包我是引入进来了。

这种代码没啥好看的,直接用,按需修改就好。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: