import com.itextpdf.text.*; import com.itextpdf.text.pdf.*; import com.ltkj.LtkjApplication; import com.ltkj.hosp.mapper.TestMapper; import com.ltkj.web.config.pdfutils.MyHeaderFooter; import lombok.extern.slf4j.Slf4j; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Paths; /** * @Company: 西安路泰科技有限公司 * @Author: lige * @Date: 2023/11/16 16:42 */ @RunWith(SpringRunner.class) @SpringBootTest(classes = LtkjApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @Slf4j @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class}) public class lgTest { @Resource private TestMapper testMapper; @Test public void lige() { // try { // PdfReader reader = new PdfReader("original.pdf"); // PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("output.pdf")); // // 获取 PDF 中的页数 // int pageCount = reader.getNumberOfPages();// 添加水印 // System.out.println(pageCount); // for (int i = 1; i <= pageCount; i++) { // final int numberOfPages = reader.getNumberOfPages(i); // } // stamper.close(); // reader.close(); // // } catch (Exception e) { // e.printStackTrace(); // } } /** * @param srcPdfPath 源PDF文件路径 * @param tagetPdfPath 加了页码的PDF文件路径 * @description 给PDF文件添加页码 */ public static void addPageNum(String srcPdfPath, String tagetPdfPath) { try { // 输出文件 流 FileOutputStream fos = new FileOutputStream(tagetPdfPath); // 读取 源PDF文件,进行一页一页复制,才能触发 添加页码的 页面监听事件 PdfReader reader = new PdfReader(srcPdfPath); // 获取 源文件总页数 int num = reader.getNumberOfPages(); // 新建文档,默认A4大小 Document document = new Document(PageSize.A4); PdfWriter writer = PdfWriter.getInstance(document, fos); // 设置页面监听事件,必须在open方法前 writer.setPageEvent(new PdfNumPageEvent(num)); document.open(); // PDF内容体 PdfContentByte pdfContent = writer.getDirectContent(); //System.out.println("总页数:" + num); // 页面数是从1开始的 for (int i = 1; i <= num; i++) { document.newPage(); // 设置空页码进行展示 writer.setPageEmpty(false); PdfImportedPage page = writer.getImportedPage(reader, i); // 复制好的页面,添加到内容去,触发事件监听 pdfContent.addTemplate(page, 0, 42); } document.close(); reader.close(); } catch (Exception e) { e.printStackTrace(); } } /** * @description 针对页码生成的PDF事件监听 **/ public static class PdfNumPageEvent extends PdfPageEventHelper { private int total; PdfNumPageEvent() { } PdfNumPageEvent(int num) { this.total = num; } @Override public void onEndPage(PdfWriter writer, Document document) { try { // PDF文档内容 PdfContentByte pdfContent = writer.getDirectContent(); pdfContent.saveState(); pdfContent.beginText(); int footerFontSize = 10; // 解决页码中文无法显示 或者 显示为乱码的问题 // 但是必须引入jar包 itext-asian-5.2.0.jar BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false); Font fontDetail = new Font(baseFont, footerFontSize, Font.NORMAL); pdfContent.setFontAndSize(baseFont, footerFontSize); // 页脚的页码 展示 String footerNum = String.format("第%d页,共" + total + "页", writer.getPageNumber()); Phrase phrase = new Phrase(footerNum, fontDetail); // 页码的 横轴 坐标 居中 float x = (document.left() + document.right()) / 2 + 10; // 页码的 纵轴 坐标 float y = document.bottom(-20); // 添加文本内容,进行展示页码 ColumnText.showTextAligned(pdfContent, Element.ALIGN_CENTER, phrase, x, y, 0); pdfContent.endText(); pdfContent.restoreState(); } catch (Exception e) { e.printStackTrace(); } } } public static void main(String[] args) { Long a = 12L; final long l = a + 10000; final String s = "V" + l; System.out.println(s); } }