在pdf中绘制表格与插入图片
2015-12-22 16:34
701 查看
使用iText.jar和itextasian.jar,可以在pdf中绘制表格和插入图片,效果如下:
图(1)在pdf中绘制表格和插入图片
本实例主要Chapter类的addSection()获取小节对象,然后,使用PdfTbale类创建表格对象,并将表格对象添加到小节中,从而实现在小节中添加表格的功能。向小节中添加表格的代码如下:
完整代码如下:
//Table.java
itext5.0.6.jar、itextasian1.5.2.jar、jcommon1.0.13.jar和jfreechart1.0.16.jar的下载地址:
http://download.csdn.net/detail/sanqima/9371147
图(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
相关文章推荐
- 按右键另存图片只能存BMP
- photoshop去除图片上的水印
- 表格标签table深入了解
- upload上传单张图片
- 图片引发的溢出危机(图)
- table 隔列(行)换色效果让表格结构更清淅
- table高级应用把表格进行到底(必看)
- PDF里的文字显示模糊的解决方法
- PDF编辑工具 Foxit PDF Editor 1.5 Build 2911 下载
- 没有文件大小限制并免费的PDF到HTML转换工具
- MicroAdobe PDF Editor 6.2 英文正式版附破解补丁 下载
- VB返回记录集结果到HTML表格的方法
- C#实现把彩色图片灰度化代码分享
- C#使用iTextSharp设置PDF所有页面背景图功能实例
- C#将图片和字节流互相转换并显示到页面上
- C#监控文件夹并自动给图片文件打水印的方法
- PDF的VBS小程序代码
- 表格高级使用技巧_把表格进行到底(必看)
- 纯CSS实现的当鼠标移上图片添加阴影效果代码
- C#实现打开画图的同时载入图片、最大化显示画图窗体的方法