您的位置:首页 > 其它

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

2015-12-22 16:34 701 查看
  使用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);

//第二行:张三、24、3500
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);

//第三行:李四、30、4500
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息