package cn.picclife.mwx.salesupport.marketactivity.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException; import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter; public class PDFUtil { //页码事件
private static class PageXofYTest extends PdfPageEventHelper{
public PdfTemplate total; public BaseFont bfChinese; /**
* 重写PdfPageEventHelper中的onOpenDocument方法
*/
@Override
public void onOpenDocument(PdfWriter writer, Document document) {
// 得到文档的内容并为该内容新建一个模板
total = writer.getDirectContent().createTemplate(500, 500);
try { String prefixFont = "";
String os = System.getProperties().getProperty("os.name");
if(os.startsWith("win") || os.startsWith("Win")){
prefixFont = "C:\\Windows\\Fonts" + File.separator;
}else {
prefixFont = "/usr/share/fonts/chinese" + File.separator;
} // 设置字体对象为Windows系统默认的字体
bfChinese = BaseFont.createFont(prefixFont + "simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
} catch (Exception e) {
throw new ExceptionConverter(e);
}
} /**
* 重写PdfPageEventHelper中的onEndPage方法
*/
@Override
public void onEndPage(PdfWriter writer, Document document) {
// 新建获得用户页面文本和图片内容位置的对象
PdfContentByte pdfContentByte = writer.getDirectContent();
// 保存图形状态
pdfContentByte.saveState();
String text = writer.getPageNumber() + "/";
// 获取点字符串的宽度
float textSize = bfChinese.getWidthPoint(text, 9);
pdfContentByte.beginText();
// 设置随后的文本内容写作的字体和字号
pdfContentByte.setFontAndSize(bfChinese, 9); // 定位'X/'
float x = (document.right() + document.left()) / 2;
float y = 56f;
pdfContentByte.setTextMatrix(x, y);
pdfContentByte.showText(text);
pdfContentByte.endText(); // 将模板加入到内容(content)中- // 定位'Y'
pdfContentByte.addTemplate(total, x + textSize, y); pdfContentByte.restoreState();
} /**
* 重写PdfPageEventHelper中的onCloseDocument方法
*/
@Override
public void onCloseDocument(PdfWriter writer, Document document) {
total.beginText();
try {
String prefixFont = "";
String os = System.getProperties().getProperty("os.name");
if(os.startsWith("win") || os.startsWith("Win")){
prefixFont = "C:\\Windows\\Fonts" + File.separator;
}else {
prefixFont = "/usr/share/fonts/chinese" + File.separator;
} bfChinese = BaseFont.createFont(prefixFont + "simsun.ttc,0",BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
total.setFontAndSize(bfChinese, 9);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
total.setTextMatrix(0, 0);
// 设置总页数的值到模板上,并应用到每个界面
total.showText(String.valueOf(writer.getPageNumber() - 1));
total.endText();
}
} //页眉事件
private static class Header extends PdfPageEventHelper {
public static PdfPTable header; public Header(PdfPTable header) {
Header.header = header;
} @Override
public void onEndPage(PdfWriter writer, Document document) {
//把页眉表格定位
header.writeSelectedRows(0, -1, 36, 806, writer.getDirectContent());
} /**
* 设置页眉
* @param writer
* @param req
* @throws MalformedURLException
* @throws IOException
* @throws DocumentException
*/
public void setTableHeader(PdfWriter writer) throws MalformedURLException, IOException, DocumentException {
String imageAddress = "E://TESTPDF/";
PdfPTable table = new PdfPTable(1);
table.setTotalWidth(555);
PdfPCell cell = new PdfPCell();
cell.setBorder(0);
Image image01;
image01 = Image.getInstance(imageAddress + "testhead.png"); //图片自己传
//image01.scaleAbsolute(355f, 10f);
image01.setWidthPercentage(80);
cell.setPaddingLeft(30f);
cell.setPaddingTop(-20f);
cell.addElement(image01);
table.addCell(cell);
Header event = new Header(table);
writer.setPageEvent(event);
}
} //页脚事件
private static class Footer extends PdfPageEventHelper {
public static PdfPTable footer; @SuppressWarnings("static-access")
public Footer(PdfPTable footer) {
this.footer = footer;
} @Override
public void onEndPage(PdfWriter writer, Document document) {
//把页脚表格定位
footer.writeSelectedRows(0, -1, 38, 50, writer.getDirectContent());
} /**
* 页脚是图片
* @param writer
* @throws MalformedURLException
* @throws IOException
* @throws DocumentException
*/
public void setTableFooter(PdfWriter writer) throws MalformedURLException, IOException, DocumentException {
String imageAddress = "E://TESTPDF/";
PdfPTable table = new PdfPTable(1);
table.setTotalWidth(523);
PdfPCell cell = new PdfPCell();
cell.setBorder(1);
Image image01;
image01 = Image.getInstance(imageAddress + "testfooter.png"); //图片自己传
image01.scaleAbsoluteWidth(523);
image01.scaleAbsoluteHeight(30f);
image01.setWidthPercentage(100);
cell.addElement(image01);
table.addCell(cell);
Footer event = new Footer(table);
writer.setPageEvent(event);
} /**
* 页脚是文字
* @param writer
* @param songti09
*/
public void setTableFooter(PdfWriter writer, Font songti09) {
PdfPTable table = new PdfPTable(1);
table.setTotalWidth(520f);
PdfPCell cell = new PdfPCell();
cell.setBorder(1);
String string = "地址: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 网址: www.xxxxxxx.com 咨询热线: 400x-xxx-xxx";
Paragraph p = new Paragraph(string, songti09);
cell.setPaddingLeft(10f);
cell.setPaddingTop(-2f);
cell.addElement(p);
table.addCell(cell);
Footer event = new Footer(table);
writer.setPageEvent(event);
}
} public static void main(String[] args) throws Exception {
Document document = new Document(PageSize.A4, 48, 48, 60, 65);
// add index page.
String path = "test.pdf";
String dir = "E://TEST";
File file = new File(dir);
if (!file.exists()) {
file.mkdir();
}
path = dir + File.separator + path;
FileOutputStream os = new FileOutputStream(path);
PdfWriter writer = PdfWriter.getInstance(document, os); // 设置页面布局
writer.setViewerPreferences(PdfWriter.PageLayoutOneColumn);
// 为这篇文档设置页面事件(X/Y)
writer.setPageEvent(new PageXofYTest()); String prefixFont = "";
String oss = System.getProperties().getProperty("os.name");
if(oss.startsWith("win") || oss.startsWith("Win")){
prefixFont = "C:\\Windows\\Fonts" + File.separator;
}else {
prefixFont = "/usr/share/fonts/chinese" + File.separator;
}
BaseFont baseFont1 = BaseFont.createFont(prefixFont + "simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font songti09 = new Font(baseFont1, 9f); //宋体 小五 document.open(); //document.newPage();
PdfPTable pdfPTable = new PdfPTable(1);
// 为报告添加页眉,事件的发生是在生成报告之后,写入到硬盘之前
//Header headerTable = new Header(pdfPTable);
//headerTable.setTableHeader(writer);
//Footer footerTable = new Footer(pdfPTable);
//footerTable.setTableFooter(writer, songti09);
document.add(pdfPTable); for (int i = 0; i < 80; i++) {
document.add(new Paragraph("the first page"));
} //document.newPage();
document.add(new Paragraph("the second page")); document.close();
os.close();
}
}

itext生成pdf(附带页眉,页脚,页码)的更多相关文章

  1. itext 生成pdf文件添加页眉页脚

    原文来自:https://www.cnblogs.com/joann/p/5511905.html 我只是记录所有jar版本,由于版本冲突及不兼容很让人头疼的,一共需要5个jar, 其中itextpd ...

  2. c# iText 生成PDF 有文字,图片,表格,文字样式,对齐方式,页眉页脚,等等等,

    #region 下载说明书PDF protected void lbtnDownPDF_Click(object sender, EventArgs e) { int pid = ConvertHel ...

  3. 【Itext】7步制作Itext5页眉页脚pdf实现第几页共几页

    itext5页眉页脚工具类,实现page x of y 完美兼容各种格式大小文档A4/B5/B3,兼容各种文档格式自动计算页脚XY轴坐标 鉴于没人做的这么细致,自己就写了一个itext5页眉页脚工具类 ...

  4. iText + Freemarker实现pdf的导出,支持中文、css以及图片,页眉页脚,页眉添加图片

    本文为了记录自己的实现过程,具体的实现步骤是参考博文 https://www.cnblogs.com/youzhibing/p/7692366.html 来实现的,只是在他的基础上添加了页眉页脚及页眉 ...

  5. 【itext】7步制作兼容各种文档格式的Itext5页眉页脚 实现page x pf y

    itext5页眉页脚工具类,实现page x of y 完美兼容各种格式大小文档A4/B5/B3,兼容各种文档格式自动计算页脚XY轴坐标 鉴于没人做的这么细致,自己就写了一个itext5页眉页脚工具类 ...

  6. 怎么给PDF去除页眉页脚

    PDF文件我们现在都会使用到,但有时需编辑PDF文件的时候,小伙伴们都知道该怎么操作吗,不知道的小伙伴不用担心,今天小编就来跟大家分享一下怎么删除PDF文件的页眉页脚,我们一起来看看下面的文章吧 操作 ...

  7. 用什么方法给PDF添加页眉页脚

    我们所看到的书本中都会设置好有页眉页脚,那么电子书想要添加页眉页脚要怎么操作呢,用什么方法可以在PDF中添加页眉页脚呢,今天就为大家分享一下,如何在电子文件中添加页眉页脚,想知道的小伙伴们就一起来看看 ...

  8. PDF怎么去除页眉页脚,PDF页眉页脚编辑方法

    我们在使用文件的时候需要编辑页眉页脚的时候,这个时候我们应该怎么做呢,相信别的文件大家都知道怎么编辑了,PDF文件大家都知道吗,最开始接触这个文件的时候小编觉得很难,之后找到技巧之后也并没有很难,今天 ...

  9. itextpdf5设置页眉页脚、生成页码

    itextpdf生成页眉页脚,可以通过继承PdfPageEventHelper,重新里面的相关事件来进行. 常用的几个: onOpenDocument,文档打开时触发 onCloseDocument, ...

  10. ABBYY FineReader 15 如何为PDF文档添加页眉页脚

    页眉.页脚是文档页面顶部或底部重复出现的文本信息.很多用户会习惯在文档页面的顶部与底部区域添加页眉.页脚来展现页码.文档标题.作者姓名.品牌名称等附加信息.而ABBYY FineReader 15(W ...

随机推荐

  1. [笔记]如何将传统的回调函数转换为C#5.0支持的await格式

    C#5.0引入了编译器支持的 async 和 await 关键字,这就为开发者提供了使用同步思想写异步代码的方便. 但是有些传统函数仅提供了异步回调实现,如何对其封装,使其可以享受await的便利呢? ...

  2. linux eclipse的桌面快捷方式

    在桌面上创建一个eclipse.desktop [Desktop Entry] Encoding=UTF- Name=Eclipse Comment=Eclipse IDE Exec=/opt/Dev ...

  3. win10系统下载地址

    Win10正式版微软官方原版ISO系统镜像下载: Win10正式版32位简体中文版(含家庭版.专业版) 文件名: cn_windows_10_multiple_editions_x86_dvd_684 ...

  4. poj 3080 Blue Jeans【字符串处理+ 亮点是:字符串函数的使用】

    题目:http://poj.org/problem?id=3080 Sample Input 3 2 GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCA ...

  5. Shell脚本实现SSH免密登录及批量配置管理

    本节索引 场景分析 ssh免密登录 pssh工具批量管理 SHELL自动化脚本 本篇总结 场景分析 作为一个运维工程师,不是每个人工作的环境都想阿里.腾讯那样,动不动就上亿的PV量,上万台服务器.我们 ...

  6. spark总结3

    cd 到hadoop中 然后格式化      进入到 bin下 找到 hdfs  然后看看里面有哈参数: ./hdfs namenode -format   格式化 然后启动 sbin/start-d ...

  7. 【P2401】不等数列(DP)

    这个题乍一看就应该是DP,再看一眼数据范围,1000..那就应该是了.然后就向DP的方向想,经过对小数据的计算可以得出,如果我们用f[i][j]来表示前i个数有j个是填了"<" ...

  8. HBase-过滤器(各种过滤器及代码实现)

    过滤器简介 HBase过滤器提供了非常强大的特性来帮助用户提高其处理表中数据的效率. HBase中两种主要的数据读取函数是get和scan,它们都支持直接访问数据和通过指定起止行键访问数据的功能.可以 ...

  9. IOS 拨打电话:4006 701 855 苹果中国

    IOS 拨打电话:4006 701 855 苹果中国

  10. SpringBoot中使用Spring Data Jpa 实现简单的动态查询的两种方法

    软件152 尹以操 首先谢谢大佬的简书文章:http://www.jianshu.com/p/45ad65690e33# 这篇文章中讲的是spring中使用spring data jpa,使用了xml ...