PDF插入页眉页脚图片

上传人:飞*** 文档编号:35703813 上传时间:2018-03-19 格式:DOCX 页数:4 大小:22.71KB
返回 下载 相关 举报
PDF插入页眉页脚图片_第1页
第1页 / 共4页
PDF插入页眉页脚图片_第2页
第2页 / 共4页
PDF插入页眉页脚图片_第3页
第3页 / 共4页
PDF插入页眉页脚图片_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

《PDF插入页眉页脚图片》由会员分享,可在线阅读,更多相关《PDF插入页眉页脚图片(4页珍藏版)》请在金锄头文库上搜索。

1、第一例:导出表格加图片/需下载 iText.jar 及 iTextAsian.jar 包03public void getExportPDF(List list, OutputStream os) throws MalformedURLException, IOException, DocumentException 04 try 05/itext 在 pdf 中输入中文字体(中文楷体)时:06bfChinese = BaseFont.createFont(“STSong-Light“, “UniGB-UCS2-H“, BaseFont.NOT_EMBEDDED); 07/ 设置之纸张为 A4

2、 纸,左右上下边距全部为 5, 08 document = new Document(PageSize.A4, 50, 50, 50, 50); 09/ 构造好的 pdf 文件输出位置 10 PdfWriter pdf = PdfWriter.getInstance(document, os); 11 catch (DocumentException e) 12 e.printStackTrace(); 13 catch (IOException e) 14 e.printStackTrace(); 15 16/生成 PDF 页脚 17 HeaderFooter footer = null;

3、18 HeaderFooter 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(“ 页

4、“, new com.lowagie.text.Font(bfChinese); 25/0 居左 1 居中 2 居右 26 header.setAlignment(0); 27 document.setHeader(header); 28 footer.setBorder(Rectangle.NO_BORDER); 29 footer.setAlignment(1); 30 document.setFooter(footer); 31/ 打开文件 32 document.open(); 33/创建表格 34PdfPTable table = new PdfPTable(2);/两列35 tab

5、le.setWidthPercentage(100); 36/传入的对象信息 37 count = list.size();38/避免因为最后一行数据由于不能满足总列数而导致 itext 自动将其去掉, 而生成一些多余的数39 int column = 2; 40 int persons = count; 41 int yushu = persons % 2; 42 int tmppersons = persons + (column - yushu); 43 String barCodeString = “; 44 String barCode = “; 45/对象信息输入 pdf 文件 4

6、6 for (int i = 0; i tmppersons; i+) 47 if (i persons) 48/构造个两列一行的表格 49 PdfPTable mytable = new PdfPTable(2); 50/插入图片 51Image image = Image.getInstance(“image 路径“); 52 mytable.addCell(image); 53 mytable.setWidthPercentage(80); 54String cntext = “测试“;55mess = new Paragraph(cntext, new com.lowagie.text

7、.Font(bfChinese,11); 56/第二列放文字信息 57 mytable.addCell(mess); 58/在单元格中放置构造好的一个表格 59 table.addCell(mytable); 60 else 61/避免因为最后一行数据由于不能满足总列数而导致 itext 自动将其去掉, 不知道有没有其他方法,目前用多余的空格填充 62 table.addCell(“ “); 63 64 65 document.add(table); 66 document.close(); 67 第二例:导出文字图片package com.test;import java.io.FileNo

8、tFoundException;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

9、 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“, Base

10、Font.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);/ 关闭文档对象,释放资源doc.close(); catch (FileNotFoundException e) e.printStackTrace(); catch (DocumentException e) e.printStackTrace();System.out.println(“OK“);

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 教育/培训

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号