zjh
2024-04-25 a74b5ffd9c25edd8096220920934e3e42f62cc23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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);
 
    }
}