先看效果图

这里说明一下这个页码是独立的覆盖在头部上的,不是写在头部里面  线条是默认的不需要的话设置

cell.setBorder(0);

需要引用的核心包有

        <dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.13.1</version></dependency><dependency><groupId>com.itextpdf</groupId><artifactId>itext-asian</artifactId><version>5.2.0</version></dependency>

代码 调用main方法生成 要指定生成位置 本段代码是指定 D:\document 文件夹下

package com.pdf;import java.io.File;
import java.io.FileOutputStream;
import java.util.Map;import com.itextpdf.text.Document;
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.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;@SuppressWarnings(value = {"unused"})
public class BasePDFWrite    {Document document = null;// 建立一个Document对象PdfWriter writer = null;public static Font headFont ;public static Font keyFont ;public static Font textfont_H ;public static Font textfont_B ;// 总页数PdfTemplate totalPage;static{BaseFont bfChinese_H;try {/*** 新建一个字体,iText的方法 STSongStd-Light 是字体,在iTextAsian.jar 中以property为后缀* UniGB-UCS2-H 是编码,在iTextAsian.jar 中以cmap为后缀 H 代表文字版式是 横版, 相应的 V 代表竖版*/bfChinese_H = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
//            bfChinese_H = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);headFont = new Font(bfChinese_H, 16, Font.BOLD);keyFont = new Font(bfChinese_H, 16, Font.BOLD);textfont_H = new Font(bfChinese_H, 8, Font.NORMAL);textfont_B = new Font(bfChinese_H, 8, Font.NORMAL);} catch (Exception e) {e.printStackTrace();}}/*** 设置页面属性* @param file*/public BasePDFWrite(File file) {
//      打印纸张尺寸:长241毫米,宽140毫米,纸张左右各留1厘米空间。
//      200*72/25.4=680
//      140*72/25.4=396//自定义纸张 前一版本
//      Rectangle rectPageSize = new Rectangle(1745,616); float width = 860;float height = 445;
//以 Windows 下的 96dpi 来计算,1 pt = px * 96/72 = px * 4/3float height1 =   (float) (height* 96/72) ;float width1 =   (float) (width* 96/72) ;//测试版
//      System.out.println("height1:"+height1+"=============width1:"+width1);Rectangle rectPageSize = new Rectangle(width1,height1); // 定义A4页面大小//Rectangle rectPageSize = new Rectangle(PageSize.A4);
//        rectPageSize = rectPageSize.rotate();// 加上这句可以实现页面的横置document = new Document(rectPageSize,0, 0, 0, 0);//页边空白
//        document.setMargins(-220, -200, 0, 0); try {writer = PdfWriter.getInstance(document,new FileOutputStream(file));writer.setPageEvent(new PageXofY());document.open();//            String mImgPath = "C://Users//admin//Desktop//55.png";
//          Image tImgCover = Image.getInstance(mImgPath);
//            /* 设置图片的位置 */
//            tImgCover.setAbsolutePosition(0, 0);
//            /* 设置图片的大小 */
//            tImgCover.scaleAbsolute(1160, 625);
//            document.add(tImgCover);             //加载图片} catch (Exception e) {e.printStackTrace();}}/*** 建表格(以列的数量建)* @param colNumber* @return*/public PdfPTable createTable(int colNumber){PdfPTable table = new PdfPTable(colNumber);try{table.setHorizontalAlignment(Element.ALIGN_CENTER);table.getDefaultCell().setBorder(0);table.setSpacingBefore(10);table.setWidthPercentage(100);}catch(Exception e){e.printStackTrace();}return table;}/*** 建表格(以列的宽度比建)* @param widths* @return*/public static PdfPTable createTable(float[] widths){PdfPTable table = new PdfPTable(widths);try{table.setHorizontalAlignment(Element.ALIGN_CENTER);table.getDefaultCell().setBorder(0);table.setSpacingBefore(10);table.setWidthPercentage(100);}catch(Exception e){e.printStackTrace();}return table;}/*** 表格中单元格* @param value* @param font* @param align  水平 设置* @return*/public static PdfPCell createCell(String value,Font font,int align){PdfPCell cell = new PdfPCell();cell.setVerticalAlignment(Element.ALIGN_MIDDLE);cell.setHorizontalAlignment(align);cell.setPhrase(new Phrase(value,font));// 默认  有边框   去0 无边框
//        cell.setBorder(0);return cell;}/*** 表格中单元格* @param value* @param font* @param align_v  垂直 设置* @param align_h  水平设置* @param colspan 跨行* @param rowspan 跨列* @return*/public static PdfPCell createCell(String value,Font font,int align_v,int align_h,int colspan,int rowspan){PdfPCell cell = new PdfPCell();cell.setVerticalAlignment(align_v);cell.setHorizontalAlignment(align_h);cell.setColspan(colspan);cell.setRowspan(rowspan);cell.setPhrase(new Phrase(value,font));// 默认  有边框    去0 无边框
//        cell.setBorder(0);return cell;}/*** * @param image* @param font* @param align* @param colspan* @param rowspan* @return*/public PdfPCell createCell(Image image, Font font, int align, int colspan,int rowspan) {PdfPCell cell = new PdfPCell();cell.setVerticalAlignment(Element.ALIGN_MIDDLE);cell.setHorizontalAlignment(align);cell.setColspan(colspan);cell.setRowspan(rowspan);cell.setFixedHeight(rowspan*25f);cell.setImage(image);return cell;}/*** 建短语* @param value* @param font* @return*/public Phrase createPhrase(String value,Font font){Phrase phrase = new Phrase();phrase.add(value);phrase.setFont(font);return phrase;}/*** 建段落* @param value* @param font* @param align* @return*/public Paragraph createParagraph(String value,Font font,int align){Paragraph paragraph = new Paragraph();paragraph.add(new Phrase(value,font));paragraph.setAlignment(align);return paragraph;}/*** 设置标题* @param tbNote* @param value* @param indentationLeft* @param alignment设置文字居中 0靠左   1,居中     2,靠右* @throws Exception*/private void pdfTitle(PdfPTable tbNote, String value, int indentationLeft,int alignment) throws Exception {PdfPCell cellNo = new PdfPCell();Paragraph pNo = new Paragraph(value, headFont);pNo.setAlignment(alignment); pNo.setIndentationLeft(indentationLeft);cellNo.addElement(pNo);cellNo.setBorder(0);tbNote.addCell(cellNo);}private void pdfTitleRight(PdfPTable tbNote, String value, int indentationRight,int alignment) throws Exception {PdfPCell cellNo = new PdfPCell();Paragraph pNo = new Paragraph(value, headFont);pNo.setAlignment(alignment); pNo.setIndentationRight(indentationRight);cellNo.addElement(pNo);cellNo.setBorder(0);tbNote.addCell(cellNo);}/*** 测试* @param args* @throws Exception*/public static void main(String[] args) throws Exception {try {Rectangle rectPageSize = new Rectangle(0,0);// 1.新建document对象   Document document = new Document(rectPageSize);// 建立一个Document对象
//            Document document = new Document(PageSize.A4);// 建立一个Document对象// 2.建立一个书写器(Writer)与document对象关联File file = new File("D:\\document\\"+"调拨单.pdf");file.createNewFile();new BasePDFWrite(file).printTransferOrder(null);System.out.println("=========完成==========");} catch (Exception e) {e.printStackTrace();}}public void printTransferOrder(Map<String, Object> map ) throws Exception{// 测试for (int i = 0; i < 2; i++) {generatePdfTableTest();}document.close();}/*** 单页* @throws Exception*/private void generatePdfTableTest() throws Exception {String operator = "张三疯";//页脚   PdfPTable table1 = BasePDFWrite.createTable(new float[] { 10, 25, 10,20 , 17,18});table1.setTotalWidth(document.right());table1.addCell(BasePDFWrite.createCell(operator,BasePDFWrite.headFont, Element.ALIGN_RIGHT));  //制单人table1.addCell(BasePDFWrite.createCell(" ",  BasePDFWrite.headFont, Element.ALIGN_CENTER));  table1.addCell(BasePDFWrite.createCell(" ",  BasePDFWrite.headFont , Element.ALIGN_CENTER,  Element.ALIGN_CENTER, 2, 0));  table1.addCell(BasePDFWrite.createCell(" ",  BasePDFWrite.headFont, Element.ALIGN_RIGHT));  table1.addCell(BasePDFWrite.createCell(" ",  BasePDFWrite.headFont, Element.ALIGN_LEFT));table1.writeSelectedRows(0, -1, 0,document.bottom() + 35, writer.getDirectContent());// 内容PdfPTable table = createTable(new float[] {12,35,15,10,20,8});//  ==================头 数据    start ==================String shipper ="中国体彩";String receiver = "广州东莞经销商";String sn = "19072503245966299";String desc = "打印测试";//换两行table.addCell(createCell(" ", headFont, Element.ALIGN_LEFT));table.addCell(createCell(" ", headFont , Element.ALIGN_CENTER,  Element.ALIGN_RIGHT, 4, 0));table.addCell(createCell(" ", headFont, Element.ALIGN_LEFT));table.addCell(createCell(" ", headFont, Element.ALIGN_LEFT));table.addCell(createCell(" ", headFont , Element.ALIGN_CENTER,  Element.ALIGN_RIGHT, 4, 0));table.addCell(createCell(" ", headFont, Element.ALIGN_LEFT));//第一行table.addCell(createCell(" ", headFont, Element.ALIGN_LEFT));table.addCell(createCell(" ", headFont , Element.ALIGN_CENTER,  Element.ALIGN_RIGHT, 4, 0));table.addCell(createCell(" ", headFont, Element.ALIGN_LEFT));//第二行table.addCell(createCell("2020-11-27", headFont, Element.ALIGN_RIGHT));  //开单日期table.addCell(createCell(shipper, headFont, Element.ALIGN_RIGHT)); //发货方table.addCell(createCell(receiver, headFont , Element.ALIGN_CENTER,  Element.ALIGN_RIGHT, 2, 0)); //收货方table.addCell(createCell(sn, headFont, Element.ALIGN_RIGHT));  // 调拨编号table.addCell(createCell(" ", headFont, Element.ALIGN_LEFT));//第三行table.addCell(createCell(desc, headFont , Element.ALIGN_CENTER,  Element.ALIGN_LEFT, 4, 0));   //备注table.addCell(createCell( "2020-11-27", headFont, Element.ALIGN_RIGHT)); //"打印日期"table.addCell(createCell(" ", headFont, Element.ALIGN_LEFT));// ==================头 数据    end ========================// 空两行table.addCell(createCell(" ", headFont, Element.ALIGN_LEFT));table.addCell(createCell("", headFont , Element.ALIGN_CENTER,  Element.ALIGN_RIGHT, 5, 0));table.addCell(createCell(" ", headFont, Element.ALIGN_LEFT));table.addCell(createCell("", headFont , Element.ALIGN_CENTER,  Element.ALIGN_RIGHT, 5, 0));table.addCell(createCell(" ", headFont, Element.ALIGN_LEFT));table.addCell(createCell("", headFont , Element.ALIGN_CENTER,  Element.ALIGN_RIGHT, 5, 0));table.addCell(createCell(" ", headFont, Element.ALIGN_LEFT));table.addCell(createCell("", headFont , Element.ALIGN_CENTER,  Element.ALIGN_RIGHT, 5, 0));table.addCell(createCell(" ", headFont, Element.ALIGN_LEFT));table.addCell(createCell("", headFont , Element.ALIGN_CENTER,  Element.ALIGN_RIGHT, 5, 0));table.addCell(createCell(" ", headFont, Element.ALIGN_LEFT));table.addCell(createCell("", headFont , Element.ALIGN_CENTER,  Element.ALIGN_RIGHT, 5, 0));//数据行for (int i = 1; i < 7; i++) {table.addCell(createCell("2190100552"+i, headFont, Element.ALIGN_LEFT));table.addCell(createCell("彩票 2019 ", headFont, Element.ALIGN_LEFT));table.addCell(createCell("张", headFont, Element.ALIGN_LEFT));table.addCell(createCell("100", headFont, Element.ALIGN_LEFT));table.addCell(createCell("00001", headFont, Element.ALIGN_RIGHT));table.addCell(createCell("", headFont, Element.ALIGN_RIGHT));}table.setHeaderRows(10);document.add(table);document.newPage(); } }

计算页码工具类

package com.pdf;import com.itextpdf.text.Document;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;public class PageXofY extends PdfPageEventHelper {/** The PdfTemplate that contains the total number of pages. */protected PdfTemplate total;/** The font that will be used. */protected BaseFont helv;/*** 字体大小*/protected int fontSize  =16 ;public void onOpenDocument(PdfWriter writer, Document document) {total = writer.getDirectContent().createTemplate(100, 100);try {helv = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);} catch (Exception e) {throw new ExceptionConverter(e);}}/*** @see com.lowagie.text.pdf.PdfPageEvent#onEndPage(com.lowagie.text.pdf.PdfWriter,*      com.lowagie.text.Document)*/public void onEndPage(PdfWriter writer, Document document) {PdfContentByte cb = writer.getDirectContent();cb.saveState();String text = "" + writer.getPageNumber() + " / ";float textX = document.right()-130;float textY =document.top()-55;cb.beginText();cb.setFontAndSize(helv, fontSize);//第几页  位置cb.setTextMatrix(textX, textY );cb.showText(text);cb.endText();// 总页数 位置cb.addTemplate(total, textX +20,  textY);cb.restoreState();}public void onCloseDocument(PdfWriter writer, Document document) {total.beginText();total.setFontAndSize(helv, fontSize);total.setTextMatrix(0, 0);total.showText(String.valueOf(writer.getPageNumber() - 1));total.endText();}}

下面啰嗦点说说关键代码所在的位置(搜索下面提供的关键代码定位代码位置)

1、复用头部  BasePDFWrite  类

table.setHeaderRows(10);

这是复用前10行的单元格,让其重新出现在下一页的最上面

2、页脚  BasePDFWrite  类

   //页脚   PdfPTable table1 = BasePDFWrite.createTable(new float[] { 10, 25, 10,20 , 17,18});table1.setTotalWidth(document.right());table1.addCell(BasePDFWrite.createCell(operator,BasePDFWrite.headFont, Element.ALIGN_RIGHT));  //制单人table1.addCell(BasePDFWrite.createCell(" ",  BasePDFWrite.headFont, Element.ALIGN_CENTER));  table1.addCell(BasePDFWrite.createCell(" ",  BasePDFWrite.headFont , Element.ALIGN_CENTER,  Element.ALIGN_CENTER, 2, 0));  table1.addCell(BasePDFWrite.createCell(" ",  BasePDFWrite.headFont, Element.ALIGN_RIGHT));  table1.addCell(BasePDFWrite.createCell(" ",  BasePDFWrite.headFont, Element.ALIGN_LEFT));//设置所在位置table1.writeSelectedRows(0, -1, 0,document.bottom() + 35, writer.getDirectContent());

每页都新增一个页脚,生成完页面后 在开一个新的页面

document.newPage();

3、动态生成 页码

首先得创建PageXofY 类

在BasePDFWrite 类 初始化 document 时设入值 (最好根据提供的关键代码搜索原文位置去看)

writer.setPageEvent(new PageXofY());

到这里就相当于在页面上打了一个标记

下面是计算页码的代码 上面效果图是   1/2  这样的  实际是拼凑出来的   两个部分   “1/” 第几页  + "2"总页数   位置的话需要大家慢慢调了

   public void onEndPage(PdfWriter writer, Document document) {PdfContentByte cb = writer.getDirectContent();cb.saveState();String text = "" + writer.getPageNumber() + " / ";float textX = document.right()-130;float textY =document.top()-55;cb.beginText();cb.setFontAndSize(helv, fontSize);//第几页  位置cb.setTextMatrix(textX, textY );cb.showText(text);cb.endText();// 总页数 位置cb.addTemplate(total, textX +20,  textY);cb.restoreState();}

java 动态生成pdf 页码相关推荐

  1. java动态生成pdf文件的方法

    java动态生成pdf文件 文章目录 java动态生成pdf文件 前言 一.生成pdf模板 二.使用步骤 1.使用jar包 2.pdf实现方法 总结 前言 java开发过程中难免会遇到生成文件的需求, ...

  2. Java动态生成pdf文件(用于实时生成电子证书)

    1.首先,新建一个word文档,内容如下,另存为pdf格式,我的命名:mytest.pdf. 2.用Adobe Acrobat Pro 打开刚刚制作的pdf文件.如下图: 3.点击创建–>PDF ...

  3. java动态生成pdf文件(使用itext编辑pdf)

  4. java根据pdf模版动态生成pdf

    java根据pdf模版动态生成pdf packagecom.utils;importjava.io.ByteArrayOutputStream;importjava.io.File;importjav ...

  5. java在linux生成pdf文件,从 Java 应用程序动态生成 PDF 文件

    简介: 如果您的应用程序需要动态生成 PDF 文档,那么您需要 iText 库.开源的 iText 库使得 PDF 的创建变得轻松易行.本文介绍了 iText 并提供了一个使用它从 Java 技术应用 ...

  6. Java读取pdf模板,并动态生成pdf文件,如动态生成准考证

    Java读取pdf模板,并动态生成pdf文件,如动态生成准考证 ​ 前几天遇到了一个生成准考证的需求,并提供用户下载,然后百度了一圈还是觉得使用itextpdf这个框架好用点.但是还需要找到一个能创建 ...

  7. 如何动态生成pdf文件?

    pdfService系统 一. 背景 在许多开发需求中都有动态生成pdf文件的需求,例如根据已有的json字符串渲染到一个表格中,然后生成对应的PDF文档,以往的解决方法是调用许多个接口生产pdf文件 ...

  8. java生成电子发票_C#/Java 动态生成电子发票

    电子发票是电商时代的产物,PDF发票是最常见的电子发票之一.在这篇文章中,我将给大家分享一个免费的动态生成PDF电子发票的C#方案,并在文章末尾附上Java解决方案. 典型的发票包含客户和供应商的名称 ...

  9. Java自动生成PDF并进行邮件群发

    这段时间,公司承办一个互联网峰会,需要对4000多位客户发送邀请函,其中包括一个pdf文件和一封手写信,PDF中将从公司数据库中读取所有客户姓名,自动生成到相应位置,前期尝试Java自动生成pdf文件 ...

最新文章

  1. iphone smtp服务器没有响应,电子邮件卡在iPhone或iPad上的发件箱?如何修复iOS中的未发送邮件 | MOS86...
  2. 基于深度学习的肺癌预测诊断
  3. php中pre标签,html中pre标签与code标签的作用与用法
  4. 2019-2020年度总结
  5. PHP 函数 - 返回值
  6. 我建了这个-现在呢? 如何在DigitalOcean Droplet上部署React App。
  7. Flink AggOperator 增量聚合函数
  8. 游标sql server_使用SQL Server游标–优点和缺点
  9. Java系统属性/环境变量
  10. MATLAB语言初步学习(五)
  11. win32 api应用如何输出日志log Windows程序设计 visualstudio vs无控制台如何输出
  12. SCM供应链管理系统的优点
  13. 电视android降低版本,电视猫旧版本下载-电视猫视频去升级版3.1.3 安卓版下载_飞翔下载...
  14. noi linux 默认密码,安装NOI Linux
  15. win10 nginx设置开机启动 --亲测有效
  16. 基于PCA算法生成平均脸
  17. 《人性的弱点》【美】戴尔 卡耐基 读书笔记
  18. C语言之动态内存开辟之malloc
  19. ilight app android,iLight pro
  20. Pycharm踩坑(一) FileNotFoundError: [Errno 2] No such file or directory: ‘../data/users.txt‘ 目录结构

热门文章

  1. Bakkt机构托管业务获批;吉林省高级人民法院成全国首批加入“司法链”法院
  2. 正则中文标点符号打包出错
  3. 五款开发Java电商系统的工具
  4. 互联网大厂的背景调查,你需要认真对待了!
  5. 2020年化工自动化控制仪表考试资料及化工自动化控制仪表操作证考试
  6. 妈妈的爱是在我遇到了难题做不出来的时候
  7. 钮文新:余额宝摧毁实业精神
  8. photoshop cs 18,lightroom 18, dreamweaver下载安装
  9. CAXA 输出 PDF
  10. java恒美服饰原材料采购预约配送系统计算机毕业设计MyBatis+系统+LW文档+源码+调试部署