在pdf中绘制表格与插入图片

81 篇文章 0 订阅

  使用iText.jar和itextasian.jar,可以在pdf中绘制表格和插入图片,效果如下: 

这里写图片描述
图(1)在pdf中绘制表格和插入图片

  本实例主要Chapter类的addSection()获取小节对象,然后,使用PdfTbale类创建表格对象,并将表格对象添加到小节中,从而实现在小节中添加表格的功能。向小节中添加表格的代码如下:  

//创建列数为3的表格
            PdfPTable table2 = new PdfPTable(3);
            table.addCell("1,3");
            table.addCell("2,3");
            table.addCell("3,3");

  完整代码如下:  
  //Table.java

package com.pdf;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;


public class Table {    
    public static void main(String[] args) {

        //创建pdf文档对象
        Document document = new Document();     
        try {
            //将文件输出流与pdf对象,进行关联
            PdfWriter.getInstance(document, new FileOutputStream("src/pdfwen/table.pdf"));

            //打开文档
            document.open();
            //字体类型为宋体
            BaseFont Chinese = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            //红色、字号15的加粗字体
            Font fontChinese1 = new Font(Chinese, 15, Font.BOLDITALIC,BaseColor.RED);
            //蓝色、字号15的加粗字体
            Font fontChinese2 = new Font(Chinese, 15, Font.BOLDITALIC, BaseColor.BLUE);
            //黑色、字号12的普通字体
            Font fontChinese3 = new Font(Chinese, 12, Font.NORMAL, BaseColor.BLACK);
            //黑色、字号12的普通字体
            Font fontChinese4 = new Font(Chinese, 12, Font.NORMAL, BaseColor.BLACK);

            Paragraph paragraph = new Paragraph("章节",fontChinese1); //创建段落对象
            Chapter chapter = new Chapter(paragraph,1); //创建章节对象
            paragraph = new Paragraph("小节",fontChinese2);
            //创建并加入小节对象
            Section section = chapter.addSection(paragraph);

            //创建表格对象
            PdfPTable table = new PdfPTable(3);
            PdfPCell cell = new PdfPCell(); 

            //第一行标题:姓名、年龄、工资
            Paragraph zhi = new Paragraph("姓名",fontChinese3);       
            cell.setPhrase(zhi);
            //单元格水平居中对齐
            cell.setUseAscender(true);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);            

            zhi = new Paragraph("年龄",fontChinese3);
            cell.setPhrase(zhi);
            cell.setUseAscender(true);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);

            zhi = new Paragraph("工资",fontChinese3);
            cell.setPhrase(zhi);
            cell.setUseAscender(true);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);

            //第二行:张三、243500
            zhi = new Paragraph("张三",fontChinese3);
            cell.setPhrase(zhi);
            cell.setUseAscender(true);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);

            zhi = new Paragraph("24",fontChinese3);
            cell.setPhrase(zhi);
            cell.setUseAscender(true);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);

            zhi = new Paragraph("3500",fontChinese3);
            cell.setPhrase(zhi);
            cell.setUseAscender(true);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);

            //第三行:李四、304500
            zhi = new Paragraph("李四",fontChinese3);
            cell.setPhrase(zhi);
            cell.setUseAscender(true);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);

            zhi = new Paragraph("30",fontChinese3);
            cell.setPhrase(zhi);
            cell.setUseAscender(true);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);

            zhi = new Paragraph("4500",fontChinese3);
            cell.setPhrase(zhi);
            cell.setUseAscender(true);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);


//          table.addCell(new Paragraph("张三",fontChinese3));                        
//          table.addCell("22");
//          table.addCell("3500");
//          
//          table.addCell(new Paragraph("李四",fontChinese3));
//          table.addCell("34");
//          table.addCell("4200");      


            paragraph = new Paragraph("\n\n工资表:\n\n",fontChinese3);
            section.add(paragraph);
            section.add(table); //将表格添加到小节中
            paragraph = new Paragraph("\n\n添加的图片:\n\n",fontChinese4);
            Image img = Image.getInstance("src/img/image.jpg");
            //图片缩小到40%
            img.scalePercent(40);
            img.setAlignment(Element.ALIGN_CENTER);
            section.add(paragraph);
            section.add(img);

            //先将section添加到chapter,再将chapter添加到文档document中
            document.add(chapter);

            //关闭文档
            document.close();


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (DocumentException e1) {
            e1.printStackTrace();
        }catch (IOException e2) {
            e2.printStackTrace();
        }


    }   

}

  itext5.0.6.jar、itextasian1.5.2.jar、jcommon1.0.13.jar和jfreechart1.0.16.jar的下载地址:
  http://download.csdn.net/detail/sanqima/9371147

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sanqima

一键三连,多多益善

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值