java 导出PDF插入页眉,页脚,图片,表格.docx_第1页
java 导出PDF插入页眉,页脚,图片,表格.docx_第2页
java 导出PDF插入页眉,页脚,图片,表格.docx_第3页
java 导出PDF插入页眉,页脚,图片,表格.docx_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第一例:导出表格加图片/需下载iText.jar及iTextAsian.jar包03public void getExportPDF(List list, OutputStream os) throws MalformedURLException, IOException, DocumentException04try 05/itext在pdf中输入中文字体(中文楷体)时:06bfChinese = BaseFont.createFont(STSong-Light, UniGB-UCS2-H, BaseFont.NOT_EMBEDDED);07/ 设置之纸张为A4纸,左右上下边距全部为5, 08document = new Document(PageSize.A4, 50, 50, 50, 50);09/ 构造好的pdf文件输出位置10PdfWriter pdf = PdfWriter.getInstance(document, os);11 catch (DocumentException e) 12e.printStackTrace();13 catch (IOException e) 14e.printStackTrace();1516/生成PDF页脚17HeaderFooter footer = null;18HeaderFooter header = null;19/页眉信息20String headerStr = 此处为页眉;21/生成PDF页眉22header=new HeaderFooter(new Phrase(new Paragraph(headerStr, new com.lowagie.text.Font(bfChinese),false); 23/生成PDF页脚,第多少页24footer=new HeaderFooter(new Phrase( 第 , new com.lowagie.text.Font(bfChinese),new Phrase( 页, new com.lowagie.text.Font(bfChinese);25/0居左1居中2居右26header.setAlignment(0);27document.setHeader(header);28footer.setBorder(Rectangle.NO_BORDER);29footer.setAlignment(1);30document.setFooter(footer);31/ 打开文件32document.open();33/创建表格34PdfPTable table = new PdfPTable(2);/两列35table.setWidthPercentage(100);36/传入的对象信息37count = list.size();38/避免因为最后一行数据由于不能满足总列数而导致itext自动将其去掉,而生成一些多余的数39int column = 2;40int persons = count;41int yushu = persons % 2;42int tmppersons = persons + (column - yushu); 43String barCodeString = ;44String barCode = ;45/对象信息输入pdf文件46for (int i = 0; i tmppersons; i+) 47if (i persons) 48/构造个两列一行的表格49PdfPTable mytable = new PdfPTable(2);50/插入图片51Image image = Image.getInstance(image路径);52mytable.addCell(image);53mytable.setWidthPercentage(80);54String cntext = 测试;55mess = new Paragraph(cntext, new com.lowagie.text.Font(bfChinese,11); 56/第二列放文字信息 57mytable.addCell(mess);58/在单元格中放置构造好的一个表格 59table.addCell(mytable);60 else 61/避免因为最后一行数据由于不能满足总列数而导致itext自动将其去掉,不知道有没有其他方法,目前用多余的空格填充 62table.addCell( ); 63 64 65document.add(table);66document.close();67第二例:导出文字图片package com.test;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import com.lowagie.text.Document;import com.lowagie.text.DocumentException;import com.lowagie.text.Font;import com.lowagie.text.Paragraph;import com.lowagie.text.pdf.BaseFont;import com.lowagie.text.pdf.PdfWriter;public class HelloWorld public static void main(String args) throws IOException /创建一个文档对象Document doc = new Document();try / 定义输出位置并把文档对象装入输出对象中PdfWriter.getInstance(doc, new FileOutputStream(c:/hello.pdf);/ 打开文档对象doc.open();/ 设置中文字体BaseFont bfChinese = BaseFont.createFont(STSong-Light, UniGB-UCS2-H, BaseFont.NOT_EMBEDDED);Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);/ 加入文字“HelloWorld - 中国北京,我的2008 .”String str = HelloWorld - 中国北京, 我的2008 .;Paragraph tt = new Paragraph(str, FontChinese);doc.add(tt);/ 加入图片Deepinpl.jpgImage jpg = Image.getInstance(c:/Deepinpl.jpg);jpg.setAlignment(Image.ALIGN_CENTER);doc.add(jpg

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论