问题场景
用itext生成pdf时,需要把某部分内容固定在一个位置,比如盖章位置被固定在每页最下方。
环境
IDEA
jdk1.8
导入的jar包:itext-pdfa-5.5.6.jar、itext-xtra-5.5.6.jar、itext-5.5.6.jar、itext-asian.jar
code
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import java.io.FileOutputStream;
import java.io.IOException;/*** @Author: youxingyang* @Date: 2019/5/16* @Description:*/
public class TableStable {public static void main(String[] args) {Document document = new Document(PageSize.A4, 25, 25, 20, 38);// left right top bottomtry {PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("test.pdf"));document.open();// startBaseFont baseFont1 = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false);Font songti09 = new Font(baseFont1, 9f);String[] arrs = {"本检测仅对样品本身负责。","若对检测结果有疑问,请在收到xxxxxxx后00个工作日内与我们联系!","地址:走你。","电话:你猜啊。"};PdfPTable table = new PdfPTable(1);com.itextpdf.text.List list4 = new com.itextpdf.text.List(true, arrs.length);list4.setAutoindent(true);list4.setNumbered(true);for (int index = 0; index < arrs.length; index++) {Paragraph elements = new Paragraph();Chunk chunk1 = new Chunk(String.valueOf((index + 1)) + ".", songti09);elements.add(chunk1);char[] chars = arrs[index].toCharArray();for (int i = 0; i < chars.length; i++) {String ss = chars[i] + "";Chunk p0 = new Chunk(ss, songti09);elements.add(p0);}Chunk chunk = new Chunk("\n");elements.add(chunk);PdfPCell cell4 = new PdfPCell();cell4.addElement(elements);table.addCell(cell4);}PdfContentByte canvas = writer.getDirectContent();table.setTotalWidth(500f);/*** rowStart 0 起始行* rowEnd -1 表示全部行* xPos 表格横坐标- 从左向右开始计算* yPos 表格纵坐标- 从下向上开始计算* canvas 画布*/table.writeSelectedRows(0, -1, 60, 200, canvas);document.close();writer.close();} catch (DocumentException | IOException e) {e.printStackTrace();}}}