forked from I3-YF/i3plus-mes-yfai
追溯报表页面样式调整
parent
5db1430759
commit
73350ef0b9
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,101 @@
|
||||
package cn.estsh.i3plus.ext.mes.apiservice.utils;
|
||||
|
||||
import cn.estsh.i3plus.ext.mes.pojo.constant.MesCommonConstant;
|
||||
import cn.estsh.i3plus.mes.apiservice.util.DateUtil;
|
||||
import com.itextpdf.text.*;
|
||||
import com.itextpdf.text.pdf.*;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
/**
|
||||
* @Description : pdf分页
|
||||
* @Reference :
|
||||
* @Author : junsheng.li
|
||||
* @CreateDate 2024/7/2 17:05
|
||||
* @Modify:
|
||||
**/
|
||||
public class PageNumPdfPageEvent extends PdfPageEventHelper {
|
||||
static int totalPages = 0;
|
||||
|
||||
@Override
|
||||
public void onEndPage(PdfWriter writer, Document document) {
|
||||
PdfContentByte pdfContent = writer.getDirectContent();
|
||||
pdfContent.saveState();
|
||||
pdfContent.beginText();
|
||||
try {
|
||||
int footerFontSize = 11;
|
||||
//BaseFont baseFont = BaseFont.createFont("C:\\Windows\\Fonts\\simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
|
||||
BaseFont baseFont = BaseFont.createFont(MesCommonConstant.FONDS_TEMP_PATH + "SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
|
||||
Font fontDetail = new Font(baseFont, footerFontSize, Font.NORMAL);
|
||||
pdfContent.setFontAndSize(baseFont, footerFontSize);
|
||||
|
||||
/*-----------------------------添加页码-------------------------------*/
|
||||
//页脚的页码 展示
|
||||
String footerNum = String.format("%d", writer.getPageNumber()) + "/" + totalPages;
|
||||
Phrase phrase = new Phrase(footerNum, fontDetail);
|
||||
//页码 纵坐标
|
||||
float y = document.bottom(-20);
|
||||
//页码 横轴 坐标 居右
|
||||
float pageX = document.right() - 20;
|
||||
//添加文本内容,进行展示页码
|
||||
ColumnText.showTextAligned(pdfContent, footerFontSize, phrase, pageX, y, 0);
|
||||
/*-----------------------------添加创建时间-------------------------------*/
|
||||
Phrase creatTimePhrase = new Phrase("" + DateUtil.formatDateApp(), fontDetail);
|
||||
|
||||
//页码 横轴 坐标 居右
|
||||
float createTimeX = (document.left() + document.right()) / 2 - 100;
|
||||
|
||||
ColumnText.showTextAligned(pdfContent, footerFontSize, creatTimePhrase, createTimeX, y, 0);
|
||||
|
||||
pdfContent.endText();
|
||||
pdfContent.restoreState();
|
||||
|
||||
} catch (DocumentException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static String addPageNum(String orgPdfPath, String outputPdfPath) {
|
||||
Document document = null;
|
||||
PdfReader reader = null;
|
||||
PdfWriter write = null;
|
||||
try {
|
||||
FileOutputStream fos = new FileOutputStream(outputPdfPath);
|
||||
document = new Document(PageSize.A4);
|
||||
|
||||
write = PdfWriter.getInstance(document, fos);
|
||||
write.setPageEvent(new PageNumPdfPageEvent());
|
||||
document.open();
|
||||
|
||||
PdfContentByte pdfContent = write.getDirectContent();
|
||||
reader = new PdfReader(orgPdfPath);
|
||||
|
||||
//总页数
|
||||
totalPages = reader.getNumberOfPages();
|
||||
|
||||
for (int i = 1; i <= totalPages; i++) {
|
||||
document.newPage();
|
||||
write.setPageEmpty(false);
|
||||
|
||||
PdfImportedPage page = write.getImportedPage(reader, i);
|
||||
pdfContent.addTemplate(page, 0, 0);
|
||||
|
||||
}
|
||||
|
||||
} catch (DocumentException | IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
assert document != null;
|
||||
document.close();
|
||||
assert reader != null;
|
||||
reader.close();
|
||||
write.close();
|
||||
}
|
||||
|
||||
return outputPdfPath;
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue