Commit 32809d38 authored by ZWT's avatar ZWT

feat(零碳): 长庆演示系统新增功能

1.修改第三方日累计数据推送表表结构,增加日累计储能放电量字段,同时修改代码对应实体及mapper文件,修改相关接口增加储能日累计放电量接收逻辑;
2.修改首页井场收益分析模块接口,修改获取储能累计放电量逻辑;
3.设计并创建井口日用电趋势表,生成对应实体类及mapper文件;
4.统计分析模块,新增本月累计节电经济效益查询接口,添加线上接口文档并完成接口冒烟测试;
5.统计分析模块,新增本月累计减碳量查询接口,添加线上接口文档并完成接口冒烟测试;
6.统计分析模块,新增光伏发电趋势查询接口,添加线上接口文档并完成接口冒烟测试;
7.统计分析模块,新增月度总览查询接口,添加线上接口文档并完成接口冒烟测试;

BREAKING CHANGE: 无

Closes 无

[skip ci]
parent 5533ed77
......@@ -5,6 +5,10 @@ package pps.core.common.constant;
* @date 2023/2/17 0017 13:04
*/
public class LoginConstant {
public static final Long LOGIN_FLAG_SIMULATE = 1000000L; //模拟登陆
/**
* 模拟登陆
*/
public static final Long LOGIN_FLAG_SIMULATE = 1000000L;
}
package pps.core.common.pdf;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.tool.xml.XMLWorkerFontProvider;
//这里重写字体
public class FontProviderUtil extends XMLWorkerFontProvider {
@Override
public Font getFont(final String fontname, final String encoding,
final boolean embedded, final float size, final int style,
final BaseColor color) {
BaseFont bf = null;
try {
bf = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
} catch (Exception e) {
e.printStackTrace();
}
Font font = new Font(bf, size, style, color);
font.setColor(color);
return font;
}
}
\ No newline at end of file
package pps.core.common.pdf;
import com.itextpdf.text.pdf.BaseFont;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class HtmlPdfUtils {
public static void html2pdf(String htmlContent, File pdfFile) throws Exception {
OutputStream os = new FileOutputStream(pdfFile);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(htmlContent);
// 中文
ITextFontResolver fontResolver = renderer.getFontResolver();
BaseFont bfChinese;
bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
fontResolver.addFont("D:\\pdf\\msyh.ttc", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.layout();
renderer.createPDF(os);
os.close();
}
public static void main(String[] args) throws Exception {
File file = new File("D:/pdf/测试.pdf"); // 创建文件对象
try {
html2pdf("<!DOCTYPE html>\n" +
"<html lang=\"en\">\n" +
" <head>\n" +
" <meta charset=\"utf-8\" />\n" +
" <title>PPS</title>\n" +
"\t\t<style>\n" +
"\t\t\ttable{\n" +
"\t\t\t\twidth: 100%;\n" +
"\t\t\t\tborder-collapse: collapse;\n" +
"\t\t\t\tborder-spacing: 0;\n" +
"\t\t\t}\n" +
"\t\t\ttable, th, td{\n" +
"\t\t\t\tborder: 1px solid #000000;\n" +
"\t\t\t\ttext-align: center;\n" +
"\t\t\t}\n" +
"\t\t\tth{\n" +
"\t\t\t\twidth: auto;\n" +
"\t\t\t\tline-height: 3;\n" +
"\t\t\t\tfont-weight: bold;\n" +
"\t\t\t}\n" +
"\t\t\ttd{\n" +
"\t\t\t\twidth: auto;\n" +
"\t\t\t\theight: 33px;\n" +
"\t\t\t\tline-height: 2;\n" +
"\t\t\t}\n" +
"\t\t</style>\n" +
" </head>\n" +
" <body>\n" +
" <div>[f_gas_voucher_detail_process@voucherDetail][f_gas_voucher_process@voucher]</div>\n" +
"\t<div style=\"padding: 16px;box-sizing: border-box;background-color: #ffffff\">\n" +
"\t\t<h1 style=\"text-align: center\">32#阀室</h1>\n" +
"\t\t<h1 style=\"text-align: center\">天然气计量交接凭证(B032分输阀室)</h1>\n" +
"\t\t<h1 style=\"border-bottom: 1px solid #999;margin: 10px 0;\"></h1>\n" +
"\t\t<div style=\"overflow:hidden;margin: 10px 0;\">\n" +
"\t\t\t<div style=\"float: left;width: 50%;\">填写日期:2022-09-22</div>\n" +
"\t\t\t<div style=\"float: left;width: 50%;\">凭证编号:@voucher.voucher_code0</div>\n" +
"\t\t</div>\n" +
"\t\t<table border=\"1\">\n" +
"\t\t\t<tr>\n" +
"\t\t\t\t<td>承运单位:</td>\n" +
"\t\t\t\t<td colspan=\"6\">32#阀室</td>\n" +
"\t\t\t</tr>\n" +
"\t\t\t<tr>\n" +
"\t\t\t\t<td>接收单位:</td>\n" +
"\t\t\t\t<td colspan=\"6\">1</td>\n" +
"\t\t\t</tr>\n" +
"\t\t\t<tr>\n" +
"\t\t\t\t<td>计量时间:</td>\n" +
"\t\t\t\t<td colspan=\"6\">2022年09月21日 08:00 至 2022年09月22日 08:00</td>\n" +
"\t\t\t</tr>\n" +
"\t\t\t<tr>\n" +
"\t\t\t\t<th>计量交接地点</th>\n" +
"\t\t\t\t<th>流量计位号</th>\n" +
"\t\t\t\t<th>前日日累(体积量)Nm3</th>\n" +
"\t\t\t\t<th>前日日累(能量)MJ</th>\n" +
"\t\t\t\t<th>平均高位发热量MJ/m3</th>\n" +
"\t\t\t\t<th>计量气量Nm3</th>\n" +
"\t\t\t\t<th>计量能量MJ</th>\n" +
"\t\t\t</tr>\n" +
"\t\t\t<tr>\n" +
"\t\t\t\t<td>B032分输阀室</td>\n" +
"\t\t\t\t<td>默认流量计</td>\n" +
"\t\t\t\t<td>3</td>\n" +
"\t\t\t\t<td>3</td>\n" +
"\t\t\t\t<td>3</td>\n" +
"\t\t\t\t<td>3</td>\n" +
"\t\t\t\t<td>3</td>\n" +
"\t\t\t</tr>\n" +
"\t\t\t<tr>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t</tr>\n" +
"\t\t\t<tr>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t</tr>\n" +
"\t\t\t<tr>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t</tr>\n" +
"\t\t\t<tr>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t</tr>\n" +
"\t\t\t<tr>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t</tr>\n" +
"\t\t\t<tr>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t</tr>\n" +
"\t\t\t<tr>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t</tr>\n" +
"\t\t\t<tr>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t</tr>\n" +
"\t\t\t<tr>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t\t<td> </td>\n" +
"\t\t\t</tr>\n" +
"\t\t\t<tr>\n" +
"\t\t\t\t<td colspan=\"2\">合计交接气量(小写)Nm3</td>\n" +
"\t\t\t\t<td colspan=\"2\">3</td>\n" +
"\t\t\t\t<td>合计交接能量(小写)MJ</td>\n" +
"\t\t\t\t<td colspan=\"2\">3</td>\n" +
"\t\t\t</tr>\n" +
"\t\t\t<tr>\n" +
"\t\t\t\t<td colspan=\"2\">合计交接气量(大写)Nm3</td>\n" +
"\t\t\t\t<td colspan=\"2\">叁标方</td>\n" +
"\t\t\t\t<td>合计交接能量(大写)MJ</td>\n" +
"\t\t\t\t<td colspan=\"2\">叁标方</td>\n" +
"\t\t\t</tr>\n" +
"\t\t\t<tr>\n" +
"\t\t\t\t<td>备注:</td>\n" +
"\t\t\t\t<td colspan=\"6\">1</td>\n" +
"\t\t\t</tr>\n" +
"\t\t\t<tr>\n" +
"\t\t\t\t<td colspan=\"2\">供气单位(盖章)</td>\n" +
"\t\t\t\t<td colspan=\"2\" style=\"opacity: 0;\"></td>\n" +
"\t\t\t\t<td>接气单位(盖章)</td>\n" +
"\t\t\t\t<td colspan=\"2\"></td>\n" +
"\t\t\t</tr>\n" +
"\t\t\t<tr>\n" +
"\t\t\t\t<td colspan=\"2\">供气方计量员(签字)</td>\n" +
"\t\t\t\t<td colspan=\"2\"></td>\n" +
"\t\t\t\t<td>接气方计量员(签字)</td>\n" +
"\t\t\t\t<td colspan=\"2\"></td>\n" +
"\t\t\t</tr>\n" +
"\t\t\t<tr>\n" +
"\t\t\t\t<td>争议量(+/-):</td>\n" +
"\t\t\t\t<td colspan=\"6\"></td>\n" +
"\t\t\t</tr>\n" +
"\t\t</table>\n" +
"\t</div>\n" +
" </body>\n" +
"</html>\n", file); // 调用封装好的工具类
} catch (Exception e) {
e.printStackTrace();
} finally {
System.err.println("转换完成");
}
}
}
package pps.core.common.pdf;
import gui.ava.html.parser.HtmlParser;
import gui.ava.html.parser.HtmlParserImpl;
import gui.ava.html.renderer.ImageRenderer;
import gui.ava.html.renderer.ImageRendererImpl;
public class HtmlToPng {
public static void main(String[] args) {
HtmlParser htmlParser = new HtmlParserImpl();
//加载html模版
htmlParser.loadHtml("<!DOCTYPE html>\n" +
"<html lang=\"en\">\n" +
" <head>\n" +
" <meta charset=\"utf-8\" />\n" +
" <title>PPS</title>\n" +
"\t\t<style>\n" +
"\t\t\ttable{\n" +
"\t\t\t\twidth: 100%;\n" +
"\t\t\t\tborder-collapse: collapse;\n" +
"\t\t\t\tborder-spacing: 0;\n" +
"\t\t\t}\n" +
"\t\t\ttable, th, td{\n" +
"\t\t\t\tborder: 1px solid #000000;\n" +
"\t\t\t\ttext-align: center;\n" +
"\t\t\t}\n" +
"\t\t\tth{\n" +
"\t\t\t\twidth: auto;\n" +
"\t\t\t\tline-height: 3;\n" +
"\t\t\t\tfont-weight: bold;\n" +
"\t\t\t\tcolor: red;\n" +
"\t\t\t}\n" +
"\t\t\ttd{\n" +
"\t\t\t\twidth: auto;\n" +
"\t\t\t\tline-height: 2;\n" +
"\t\t\t}\n" +
"\t\t</style>\n" +
" </head>\n" +
" <body>\n" +
"\t<div style=\"padding: 16px;box-sizing: border-box;background-color: #ffffff\">\n" +
"\t\t<div style=\"text-align: center\">@mOuName</div>\n" +
"\t\t<div style=\"text-align: center\">天然气计量交接凭证(中卫首站)</div>\n" +
"\t\t<div style=\"border-bottom: 1px solid #999;margin: 10px 0;\"></div>\n" +
"\t\t<div style=\"margin-bottom: 10px\">\n" +
"\t\t\t<div style=\"display:inline-block;width: 50%;\">填写日期:2022-07-07</div>\n" +
"\t\t\t<div style=\"display:inline-block;width: 50%;\">凭证编号:@voucherCode</div>\n" +
"\t\t</div>\n" +
"\t\t<table border=\"1\">\n" +
"\t\t\t<tr>\n" +
"\t\t\t\t<td>承运单位:</td>\n" +
"\t\t\t\t<td colspan=\"6\">@mOuName</td>\n" +
"\t\t\t</tr>\n" +
"\t\t\t<tr>\n" +
"\t\t\t\t<td>托运单位:</td>\n" +
"\t\t\t\t<td colspan=\"6\"></td>\n" +
"\t\t\t</tr>\n" +
"\t\t\t<tr>\n" +
"\t\t\t\t<td>接收单位:</td>\n" +
"\t\t\t\t<td colspan=\"6\">@cOuName</td>\n" +
"\t\t\t</tr>\n" +
"\t\t\t<tr>\n" +
"\t\t\t\t<td>计量时间:</td>\n" +
"\t\t\t\t<td colspan=\"6\">2022年7月5日 08:00 至 2022年7月6日 08:00</td>\n" +
"\t\t\t</tr>\n" +
"\t\t\t<tr>\n" +
"\t\t\t\t<th>计量交接地点</th>\n" +
"\t\t\t\t<th>流量计位号</th>\n" +
"\t\t\t\t<th>前日日累(体积量)Nm3</th>\n" +
"\t\t\t\t<th>前日日累(能量)MJ</th>\n" +
"\t\t\t\t<th>平均高位发热量MJ/m3</th>\n" +
"\t\t\t\t<th>计量气量Nm3</th>\n" +
"\t\t\t\t<th>计量能量MJ</th>\n" +
"\t\t\t</tr>\n" +
"\t\t\t<tr>\n" +
"\t\t\t\t<td>@detailStationName0</td>\n" +
"\t\t\t\t<td>@detailEquipmentName0</td>\n" +
"\t\t\t\t<td>@detailGasVolumeYesterday0</td>\n" +
"\t\t\t\t<td>156140210</td>\n" +
"\t\t\t\t<td>6436881280</td>\n" +
"\t\t\t\t<td>11009989685</td>\n" +
"\t\t\t\t<td>13242421311</td>\n" +
"\t\t\t</tr>\n" +
"\t\t\t<tr>\n" +
"\t\t\t\t<td>@detailStationName1</td>\n" +
"\t\t\t\t<td>@detailEquipmentName1</td>\n" +
"\t\t\t\t<td>@detailGasVolumeYesterday1</td>\n" +
"\t\t\t\t<td>156140210</td>\n" +
"\t\t\t\t<td>6436881280</td>\n" +
"\t\t\t\t<td>11009989685</td>\n" +
"\t\t\t\t<td>13242421311</td>\n" +
"\t\t\t</tr>\n" +
"\t\t\t<tr>\n" +
"\t\t\t\t<td>@detailStationName2</td>\n" +
"\t\t\t\t<td>@detailEquipmentName2</td>\n" +
"\t\t\t\t<td>@detailGasVolumeYesterday2</td>\n" +
"\t\t\t\t<td>156140210</td>\n" +
"\t\t\t\t<td>6436881280</td>\n" +
"\t\t\t\t<td>11009989685</td>\n" +
"\t\t\t\t<td>13242421311</td>\n" +
"\t\t\t</tr>\n" +
"\t\t\t<tr>\n" +
"\t\t\t\t<td>@detailStationName3</td>\n" +
"\t\t\t\t<td>@detailEquipmentName3</td>\n" +
"\t\t\t\t<td>@detailGasVolumeYesterday3</td>\n" +
"\t\t\t\t<td>156140210</td>\n" +
"\t\t\t\t<td>6436881280</td>\n" +
"\t\t\t\t<td>11009989685</td>\n" +
"\t\t\t\t<td>13242421311</td>\n" +
"\t\t\t</tr>\n" +
"\t\t\t<tr>\n" +
"\t\t\t\t<td colspan=\"2\">合计交接气量(小写)Nm3</td>\n" +
"\t\t\t\t<td colspan=\"2\">94010</td>\n" +
"\t\t\t\t<td>合计交接能量(小写)MJ</td>\n" +
"\t\t\t\t<td colspan=\"2\">94010</td>\n" +
"\t\t\t</tr>\n" +
"\t\t\t<tr>\n" +
"\t\t\t\t<td colspan=\"2\">合计交接气量(大写)Nm3</td>\n" +
"\t\t\t\t<td colspan=\"2\">玖万肆仟零壹拾标方</td>\n" +
"\t\t\t\t<td>合计交接能量(大写)MJ</td>\n" +
"\t\t\t\t<td colspan=\"2\">玖万肆仟零壹拾标方</td>\n" +
"\t\t\t</tr>\n" +
"\t\t\t<tr>\n" +
"\t\t\t\t<td>备注:</td>\n" +
"\t\t\t\t<td colspan=\"6\">@remarks</td>\n" +
"\t\t\t</tr>\n" +
"\t\t\t<tr>\n" +
"\t\t\t\t<td colspan=\"2\">供气单位(盖章)</td>\n" +
"\t\t\t\t<td colspan=\"2\" style=\"opacity: 0;\">供气</td>\n" +
"\t\t\t\t<td>接气单位(盖章)</td>\n" +
"\t\t\t\t<td colspan=\"2\"></td>\n" +
"\t\t\t</tr>\n" +
"\t\t\t<tr>\n" +
"\t\t\t\t<td colspan=\"2\">供气方计量员(签字)</td>\n" +
"\t\t\t\t<td colspan=\"2\"></td>\n" +
"\t\t\t\t<td>接气方计量员(签字)</td>\n" +
"\t\t\t\t<td colspan=\"2\"></td>\n" +
"\t\t\t</tr>\n" +
"\t\t\t<tr>\n" +
"\t\t\t\t<td>争议量(+/-):</td>\n" +
"\t\t\t\t<td colspan=\"6\"></td>\n" +
"\t\t\t</tr>\n" +
"\t\t</table>\n" +
"\t</div>\n" +
" </body>\n" +
"</html>\n");
//把html写入到图片
ImageRenderer imageRenderer = new ImageRendererImpl(htmlParser);
imageRenderer.saveImage("D:\\pdf\\hello-world.png");
}
}
package pps.core.common.pdf;
import gui.ava.html.exception.RenderException;
import gui.ava.html.parser.DocumentHolder;
import gui.ava.html.renderer.FormatNameUtil;
import gui.ava.html.renderer.ImageRendererImpl;
import org.xhtmlrenderer.util.FSImageWriter;
import java.awt.image.BufferedImage;
import java.io.*;
public class ImageRendererSubImpl extends ImageRendererImpl {
public ImageRendererSubImpl(DocumentHolder documentHolder) {
super(documentHolder);
}
private String getImageFormat(String filename) {
if (this.getImageFormat() != null) {
return this.getImageFormat();
} else {
return filename != null ? FormatNameUtil.formatForFilename(filename) : FormatNameUtil.getDefaultFormat();
}
}
private FSImageWriter getImageWriter(String imageFormat) {
FSImageWriter imageWriter = new FSImageWriter(imageFormat);
imageWriter.setWriteCompressionMode(this.getWriteCompressionMode());
imageWriter.setWriteCompressionQuality(this.getWriteCompressionQuality());
imageWriter.setWriteCompressionType(this.getWriteCompressionType());
return imageWriter;
}
public void saveImage(File file) {
try {
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file));
this.save(outputStream, file.getName(), true);
} catch (IOException var3) {
throw new RenderException("IOException while rendering image to " + file.getAbsolutePath(), var3);
}
}
public void saveImage(String filename) {
this.saveImage(new File(filename));
}
private void save(OutputStream outputStream, String filename, boolean closeStream) {
try {
String imageFormat = this.getImageFormat(filename);
FSImageWriter imageWriter = this.getImageWriter(imageFormat);
BufferedImage bufferedImage = this.getBufferedImage(getImageType(imageFormat));
imageWriter.write(bufferedImage, outputStream);
} catch (IOException var15) {
throw new RenderException("IOException while rendering image", var15);
} finally {
if (closeStream) {
try {
outputStream.close();
} catch (IOException var14) {
;
}
}
}
}
/**
* 获取图像类型
* 根据图像的格式
*/
public int getImageType(String imageFormat) {
if ("jpg".equalsIgnoreCase(imageFormat)) {
return BufferedImage.TYPE_3BYTE_BGR;
}
if ("bmp".equalsIgnoreCase(imageFormat)) {
return BufferedImage.TYPE_INT_RGB;
}
return BufferedImage.BITMASK;
}
}
package pps.core.common.pdf;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
/**
* 使用时,需要修改 ImageTransformPDF()函数中的输入图片和输出pdf
*/
public class ImageTransformPDF {
public static void main(String[] args) {
ImageTransformPDF();
}
public static String ImageTransformPDF() {
new ImageTransformPDF().imgOfPdf("D:\\pdf\\png2.pdf", "D:\\pdf\\hello-world.png");
return "200!ok";
}
//为终极函数做铺垫
public static File Pdf(ArrayList<String> imageUrllist, String mOutputPdfFileName) {
Document doc = new Document(PageSize.A4, 0, 0, 0, 0); //new一个pdf文档
try {
PdfWriter.getInstance(doc, new FileOutputStream(mOutputPdfFileName)); //pdf写入
doc.open();//打开文档
for (int i = 0; i < imageUrllist.size(); i++) { //循环图片List,将图片加入到pdf中
doc.newPage(); //在pdf创建一页
Image png1 = Image.getInstance(imageUrllist.get(i)); //通过文件路径获取image
float heigth = png1.getHeight();
float width = png1.getWidth();
int percent = getPercent2(heigth, width);
png1.setAlignment(Image.MIDDLE);
png1.scalePercent(percent + 3);// 表示是原来图像的比例;
doc.add(png1);
}
doc.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
File mOutputPdfFile = new File(mOutputPdfFileName); //输出流
if (!mOutputPdfFile.exists()) {
mOutputPdfFile.deleteOnExit();
return null;
}
return mOutputPdfFile; //反回文件输出流
}
public static int getPercent(float h, float w) {
int p = 0;
float p2 = 0.0f;
if (h > w) {
p2 = 297 / h * 100;
} else {
p2 = 210 / w * 100;
}
p = Math.round(p2);
return p;
}
public static int getPercent2(float h, float w) {
int p = 0;
float p2 = 0.0f;
p2 = 530 / w * 100;
p = Math.round(p2);
return p;
}
/**
* @Description: 通过图片路径及生成pdf路径,将图片转成pdf
* @Author: zd
* @Date: 2019/9/29
*/
public void imgOfPdf(String filepath, String imgUrl) {
try {
ArrayList<String> imageUrllist = new ArrayList<String>(); //图片list集合
String[] imgUrls = imgUrl.split(",");
for (int i = 0; i < imgUrls.length; i++) {
imageUrllist.add(imgUrls[i]);
}
String pdfUrl = filepath; //输出pdf文件路径
File file = this.Pdf(imageUrllist, pdfUrl);//生成pdf
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package pps.core.common.pdf;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import gui.ava.html.parser.HtmlParser;
import gui.ava.html.parser.HtmlParserImpl;
import gui.ava.html.renderer.ImageRenderer;
import gui.ava.html.renderer.ImageRendererImpl;
import org.apache.commons.codec.binary.Base64;
import sun.misc.BASE64Decoder;
import java.io.*;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class Itext5HtmlToPDF {
public static void main(String[] args) throws Exception {
String base64 = htmlToPDF("<!DOCTYPE html>\n" +
"<html lang=\"en\">\n" +
"<head>\n" +
"\t<meta charset=\"utf-8\"/>\n" +
"\t<title>PPS</title>\n" +
"\t<style>\n" +
"\t\tbody {\n" +
"\t\t\tfont-family: sans-serif, 'Microsoft Yahei';\n" +
"\t\t}\n" +
"\n" +
"\t\t.container {\n" +
"\t\t/*\twidth: 1240px;\n" +
"\t\t\theight: 1754px;\n" +
"\t\t\tmargin: 0 auto;*/\n" +
"\t\t\tpadding: 16px;\n" +
"\t\t\tbox-sizing: border-box;\n" +
"\t\t\tbackground-color: #ffffff;\n" +
"\t\t\tline-height: 1.5;\n" +
"\t\t\tfont-size: 16px;\n" +
"\t\t\tfont-weight: bold;\n" +
"\t\t}\n" +
"\n" +
"\t\t.container h1 {\n" +
"\t\t\ttext-align: center;\n" +
"\t\t\tline-height: 1;\n" +
"\t\t\tfont-size: 28px;\n" +
"\t\t\tfont-weight: 900;\n" +
"\t\t}\n" +
"\n" +
"\t\t.container .datecode {\n" +
"\t\t\toverflow: hidden;\n" +
"\t\t}\n" +
"\n" +
"\t\t.container .datecode span {\n" +
"\t\t\tfloat: left;\n" +
"\t\t}\n" +
"\n" +
"\t\t.container .datecode span:last-child {\n" +
"\t\t\tfloat: right;\n" +
"\t\t}\n" +
"\t\t.left{\n" +
"\t\t\ttext-align: left;\n" +
"\t\t}\n" +
"\t\ttable {\n" +
"\t\t\twidth: 100%;\n" +
"\t\t\tborder-collapse: collapse;\n" +
"\t\t\tborder-spacing: 0;\n" +
"\t\t}\n" +
"\n" +
"\t\ttable, th, td {\n" +
"\t\t\tline-height: 2;\n" +
"\t\t\tborder: 1px solid #000000;\n" +
"\t\t\ttext-align: center;\n" +
"\t\t\tpadding: 0 5px;\n" +
"\t\t}\n" +
"\n" +
"\t\tth {\n" +
"\t\t\twidth: auto;\n" +
"\t\t\tfont-weight: bold;\n" +
"\t\t}\n" +
"\n" +
"\t\ttd {\n" +
"\t\t\twidth: auto;\n" +
"\t\t}\n" +
"\t</style>\n" +
"</head>\n" +
"<body>\n" +
"<div class=\"container\">\n" +
"\t<h1>国家管网集团联合管道有限责任公司西气东输分公司</h1>\n" +
"\t<h1>天然气计量交接凭证(中卫首站)</h1>\n" +
"\t<div class=\"datecode\">\n" +
"\t\t<span>填写日期:2022-07-07</span>\n" +
"\t\t<span>凭证编号:185--220708</span>\n" +
"\t</div>\n" +
"\t<table cellspacing=\"0\">\n" +
"\t\t<tr>\n" +
"\t\t\t<td class=\"left\" colspan=\"2\">承运单位:国家管网集团联合管道有限责任公司西气东输分公司</td>\n" +
"\t\t\t<td class=\"left\" colspan=\"2\">托运单位:</td>\n" +
"\t\t\t<td class=\"left\" colspan=\"3\">接收单位:江苏华电昆山热电有限公司</td>\n" +
"\t\t</tr>\n" +
"\t\t<tr>\n" +
"\t\t\t<td class=\"left\" colspan=\"7\">计量时间:2022年7月5日 08:00 至 2022年7月6日 08:00</td>\n" +
"\t\t</tr>\n" +
"\t\t<tr>\n" +
"\t\t\t<th>计量交接地点</th>\n" +
"\t\t\t<th>流量计位号</th>\n" +
"\t\t\t<th>前日日累(体积量)Nm3</th>\n" +
"\t\t\t<th>前日日累(能量)MJ</th>\n" +
"\t\t\t<th>平均高位发热量MJ/m3</th>\n" +
"\t\t\t<th>计量气量Nm3</th>\n" +
"\t\t\t<th>计量能量MJ</th>\n" +
"\t\t</tr>\n" +
"\t\t<tr>\n" +
"\t\t\t<td>中卫首站</td>\n" +
"\t\t\t<td>FM501</td>\n" +
"\t\t\t<td>156140210</td>\n" +
"\t\t\t<td>156140210</td>\n" +
"\t\t\t<td>6436881280</td>\n" +
"\t\t\t<td>11009989685</td>\n" +
"\t\t\t<td>13242421311</td>\n" +
"\t\t</tr>\n" +
"\t\t<tr>\n" +
"\t\t\t<td>中卫首站</td>\n" +
"\t\t\t<td>FM501</td>\n" +
"\t\t\t<td>156140210</td>\n" +
"\t\t\t<td>156140210</td>\n" +
"\t\t\t<td>6436881280</td>\n" +
"\t\t\t<td>11009989685</td>\n" +
"\t\t\t<td>13242421311</td>\n" +
"\t\t</tr>\n" +
"\t\t<tr>\n" +
"\t\t\t<td>中卫首站</td>\n" +
"\t\t\t<td>FM501</td>\n" +
"\t\t\t<td>156140210</td>\n" +
"\t\t\t<td>156140210</td>\n" +
"\t\t\t<td>6436881280</td>\n" +
"\t\t\t<td>11009989685</td>\n" +
"\t\t\t<td>13242421311</td>\n" +
"\t\t</tr>\n" +
"\t\t<tr>\n" +
"\t\t\t<td>中卫首站</td>\n" +
"\t\t\t<td>FM501</td>\n" +
"\t\t\t<td>156140210</td>\n" +
"\t\t\t<td>156140210</td>\n" +
"\t\t\t<td>6436881280</td>\n" +
"\t\t\t<td>11009989685</td>\n" +
"\t\t\t<td>13242421311</td>\n" +
"\t\t</tr>\n" +
"\t\t<tr>\n" +
"\t\t\t<td colspan=\"2\">合计交接气量(小写)Nm3</td>\n" +
"\t\t\t<td colspan=\"2\">94010</td>\n" +
"\t\t\t<td>合计交接能量(小写)MJ</td>\n" +
"\t\t\t<td colspan=\"2\">94010</td>\n" +
"\t\t</tr>\n" +
"\t\t<tr>\n" +
"\t\t\t<td colspan=\"2\">合计交接气量(大写)Nm3</td>\n" +
"\t\t\t<td colspan=\"2\">玖万肆仟零壹拾标方</td>\n" +
"\t\t\t<td>合计交接能量(大写)MJ</td>\n" +
"\t\t\t<td colspan=\"2\">玖万肆仟零壹拾标方</td>\n" +
"\t\t</tr>\n" +
"\t\t<tr>\n" +
"\t\t\t<td>备注:</td>\n" +
"\t\t\t<td colspan=\"6\"></td>\n" +
"\t\t</tr>\n" +
"\t\t<tr>\n" +
"\t\t\t<td colspan=\"2\">供气单位(盖章)</td>\n" +
"\t\t\t<td colspan=\"2\" style=\"opacity: 0;\">供气</td>\n" +
"\t\t\t<td>接气单位(盖章)</td>\n" +
"\t\t\t<td colspan=\"2\"></td>\n" +
"\t\t</tr>\n" +
"\t\t<tr>\n" +
"\t\t\t<td colspan=\"2\">供气方计量员(签字)</td>\n" +
"\t\t\t<td colspan=\"2\"></td>\n" +
"\t\t\t<td>接气方计量员(签字)</td>\n" +
"\t\t\t<td colspan=\"2\"></td>\n" +
"\t\t</tr>\n" +
"\t\t<tr>\n" +
"\t\t\t<td>争议量(+/-):</td>\n" +
"\t\t\t<td colspan=\"6\"></td>\n" +
"\t\t</tr>\n" +
"\t</table>\n" +
"</div>\n" +
"</body>\n" +
"</html>\n", new HashMap<>());
saveBase64strToFile(base64, "D:\\pdf\\test" + new Date().getTime() + ".pdf");
}
public static String replaceContent(String htmlContent, Map<String, String> map) {
for (Map.Entry<String, String> entry : map.entrySet()) {
htmlContent = htmlContent.replaceAll(entry.getKey(), entry.getValue());
}
return htmlContent;
}
public static ByteArrayOutputStream htmlSaveImage(String htmlContent) {
HtmlParser htmlParser = new HtmlParserImpl();
//加载html模版
htmlParser.loadHtml(htmlContent);
//把html写入到图片
ImageRenderer imageRenderer = new ImageRendererImpl(htmlParser);
imageRenderer.setWidth(2048);
imageRenderer.setHeight(1536);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
imageRenderer.saveImage(byteArrayOutputStream, true);
return byteArrayOutputStream;
}
public static String imageToPDF(ByteArrayOutputStream imageByte) throws Exception {
Document document = new Document();
//第二步,为该Document创建一个Writer实例:
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
PdfWriter writer = PdfWriter.getInstance(document, byteArrayOutputStream);
//第三步,打开当前Document
document.open();
//第四步,为当前Document添加内容:
//替换内容
document.newPage(); //在pdf创建一页
Image png1 = Image.getInstance(imageByte.toByteArray()); //通过文件路径获取image
float heigth = png1.getHeight();
float width = png1.getWidth();
int percent = getPercent2(heigth, width);
png1.setAlignment(Image.MIDDLE);
png1.scalePercent(percent + 3);// 表示是原来图像的比例;
document.add(png1);
//第五步,关闭Document
document.close();
byte[] buffer = byteArrayOutputStream.toByteArray();
InputStream sbs = new ByteArrayInputStream(buffer);
return getBase64FromInputStream(sbs);
}
public static String htmlToPDF(String htmlContent, Map<String, String> map) throws Exception {
Document document = new Document();
//第二步,为该Document创建一个Writer实例:
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
PdfWriter writer = PdfWriter.getInstance(document, byteArrayOutputStream);
//第三步,打开当前Document
document.open();
//第四步,为当前Document添加内容:
//替换内容
XMLWorkerHelper.getInstance().parseXHtml(writer, document,
new ByteArrayInputStream(htmlContent.getBytes("UTF-8")),
null, Charset.defaultCharset(), new FontProviderUtil());
//第五步,关闭Document
document.close();
byte[] buffer = byteArrayOutputStream.toByteArray();
InputStream sbs = new ByteArrayInputStream(buffer);
return getBase64FromInputStream(sbs);
}
public static String getBase64FromInputStream(InputStream in) {
// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
byte[] data = null;
// 读取图片字节数组
try {
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[100];
int rc = 0;
while ((rc = in.read(buff, 0, 100)) > 0) {
swapStream.write(buff, 0, rc);
}
data = swapStream.toByteArray();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return new String(Base64.encodeBase64(data));
}
public static void saveBase64strToFile(String base64str, String outPath) {
if (base64str == null) {
return;
}
BASE64Decoder decoder = new BASE64Decoder();
try {
byte[] b = decoder.decodeBuffer(base64str);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {
b[i] += 256;
}
}
OutputStream out = new FileOutputStream(outPath);
out.write(b);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static int getPercent2(float h, float w) {
int p = 0;
float p2 = 0.0f;
p2 = 530 / w * 100;
p = Math.round(p2);
return p;
}
}
package pps.core.common.pdf;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
public class ItextPDFRepalceUtil {
//输入的模版路径
public static String inputFilePath = "D:\\pdf\\合同2.pdf";
//插入的文件路径
public static String imgFilePath = "D:\\pdf\\签名.png";
//输出路径
public static String out = "D:\\pdf\\out6.pdf";
public static String keyword = "${甲方盖章}";//测试标记
public static String keyword2 = "${乙方盖章}";//测试标记
public static void main(String[] args) throws IOException, DocumentException {
//replaceImg();
//replaceText();
addImg("D:\\pdf\\1111.pdf", "D:\\pdf\\2222.pdf", "D:\\pdf\\4444.png");
}
//插入图片的例子
public static void replaceImg() throws IOException, DocumentException {
//查找位置
float[] position = ItextPDFUtil.getAddImagePositionXY(inputFilePath, keyword);
if (position.length == 0)
return;
//Read file using PdfReader
PdfReader pdfReader = new PdfReader(inputFilePath);
//Modify file using PdfReader
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream(out));
Image image = Image.getInstance(imgFilePath);
//Fixed Positioning
// image.scaleToFit(100, 50);
image.scaleAbsolute(100, 20);
//Scale to new height and new width of image
image.setAbsolutePosition(position[1] - 50, position[2] + 25);
PdfContentByte content = pdfStamper.getUnderContent((int) position[0]);
content.addImage(image);
pdfStamper.close();
}
//修改文字的示例
public static void replaceText() {
//1.给定文件
File pdfFile = new File(inputFilePath);
//2.定义一个byte数组,长度为文件的长度
byte[] pdfData = new byte[(int) pdfFile.length()];
//3.IO流读取文件内容到byte数组
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(pdfFile);
inputStream.read(pdfData);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
}
}
}
//4.指定关键字
String replace = " 王文博 先生";
//5.调用方法,给定关键字和文件
List<float[]> positions = null;
try {
positions = ItextPDFUtil.findKeywordPostions(pdfData, keyword2);
} catch (IOException e) {
e.printStackTrace();
}
PdfReader pdfReader = null;
PdfStamper stamper = null;
try {
pdfReader = new PdfReader(pdfData);
stamper = new PdfStamper(pdfReader, new FileOutputStream(out));
if (positions != null) {
for (int i = 0; i < positions.size(); i++) {
float[] position = positions.get(i);
PdfContentByte canvas = stamper.getOverContent((int) position[0]);
//修改背景色
canvas.saveState();
canvas.setColorFill(BaseColor.WHITE);
canvas.rectangle(position[1], position[2], position[3] + 10, position[4] - 1);
canvas.fill();
canvas.restoreState();
//替换关键字
canvas.beginText();
BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
canvas.setFontAndSize(bf, 13);
canvas.setTextMatrix(position[1] + 3, position[2]);/*修正背景与文本的相对位置*/
canvas.showText(replace);
canvas.endText();
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} finally {
if (stamper != null)
try {
stamper.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (pdfReader != null) {
pdfReader.close();
}
}
}
public static void addImg(String inPdfFile, String outPdfFile, String imgPath) {
try {
PdfReader reader = new PdfReader(inPdfFile, "PDF".getBytes());
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(outPdfFile));
int pages = reader.getNumberOfPages();
// 设置透明度
PdfGState gs = new PdfGState();
gs.setStrokeOpacity(0.9f);
gs.setFillOpacity(0.9f);
for (int i = 1; i <= pages; i++) {
Rectangle cropBox = reader.getCropBox(i);
Image img = Image.getInstance(imgPath);// 插入业务章
//设置缩放比例
img.scaleAbsolute(115, 115);
//设置业务章的位置
img.setAbsolutePosition((float) (cropBox.getWidth() * 0.7), (float) (cropBox.getHeight() * 0.65));
PdfContentByte over = stamp.getOverContent(i);
over.setGState(gs);
over.addImage(img);
}
stamp.close();// 关闭
File tempfile = new File(inPdfFile);
if (tempfile.exists()) {
tempfile.delete();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package pps.core.common.pdf;
import com.itextpdf.awt.geom.Rectangle2D;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ItextPDFUtil {
public static final float defaultHeight = 16;
public static final float fixHeight = 10;
/**
* @description: 查找插入签名图片的最终位置,因为是插入签名图片,所以之前的关键字应只会出现一次
* 这里直接使用了第一次查找到关键字的位置,并返回该关键字之后的坐标位置
* @return: float[0]:页码,float[1]:最后一个字的x坐标,float[2]:最后一个字的y坐标
*/
public static float[] getAddImagePositionXY(String pdfName, String keyword) throws IOException {
float[] temp = new float[3];
List<float[]> positions = findKeywordPostions(pdfName, keyword);
if (positions.size() == 0)
return new float[0];
temp[0] = positions.get(0)[0];
temp[1] = positions.get(0)[1] + positions.get(0)[3];
temp[2] = positions.get(0)[2] - (positions.get(0)[3] / 2);
return temp;
}
/**
* findKeywordPostions
* 返回查找到关键字的首个文字的左上角坐标值
*
* @param pdfName
* @param keyword
* @return List<float [ ]> : float[0]:pageNum float[1]:x float[2]:y
* @throws IOException
*/
public static List<float[]> findKeywordPostions(String pdfName, String keyword) throws IOException {
File pdfFile = new File(pdfName);
byte[] pdfData = new byte[(int) pdfFile.length()];
FileInputStream inputStream = new FileInputStream(pdfFile);
//从输入流中读取pdfData.length个字节到字节数组中,返回读入缓冲区的总字节数,若到达文件末尾,则返回-1
inputStream.read(pdfData);
inputStream.close();
List<float[]> result = new ArrayList<>();
List<PdfPageContentPositions> pdfPageContentPositions = getPdfContentPostionsList(pdfData);
for (PdfPageContentPositions pdfPageContentPosition : pdfPageContentPositions) {
List<float[]> charPositions = findPositions(keyword, pdfPageContentPosition);
if (charPositions == null || charPositions.size() < 1) {
continue;
}
result.addAll(charPositions);
}
return result;
}
/**
* findKeywordPostions
*
* @param pdfData 通过IO流 PDF文件转化的byte数组
* @param keyword 关键字
* @return List<float [ ]> : float[0]:pageNum float[1]:x float[2]:y
* @throws IOException
*/
public static List<float[]> findKeywordPostions(byte[] pdfData, String keyword) throws IOException {
List<float[]> result = new ArrayList<float[]>();
List<PdfPageContentPositions> pdfPageContentPositions = getPdfContentPostionsList(pdfData);
for (PdfPageContentPositions pdfPageContentPosition : pdfPageContentPositions) {
List<float[]> charPositions = findPositions(keyword, pdfPageContentPosition);
if (charPositions == null || charPositions.size() < 1) {
continue;
}
result.addAll(charPositions);
}
return result;
}
private static List<PdfPageContentPositions> getPdfContentPostionsList(byte[] pdfData) throws IOException {
PdfReader reader = new PdfReader(pdfData);
List<PdfPageContentPositions> result = new ArrayList<PdfPageContentPositions>();
int pages = reader.getNumberOfPages();
for (int pageNum = 1; pageNum <= pages; pageNum++) {
float width = reader.getPageSize(pageNum).getWidth();
float height = reader.getPageSize(pageNum).getHeight();
PdfRenderListener pdfRenderListener = new PdfRenderListener(pageNum, width, height);
//解析pdf,定位位置
PdfContentStreamProcessor processor = new PdfContentStreamProcessor(pdfRenderListener);
PdfDictionary pageDic = reader.getPageN(pageNum);
PdfDictionary resourcesDic = pageDic.getAsDict(PdfName.RESOURCES);
try {
processor.processContent(ContentByteUtils.getContentBytesForPage(reader, pageNum), resourcesDic);
} catch (IOException e) {
reader.close();
throw e;
}
String content = pdfRenderListener.getContent();
List<CharPosition> charPositions = pdfRenderListener.getcharPositions();
List<float[]> positionsList = new ArrayList<float[]>();
for (CharPosition charPosition : charPositions) {
float[] positions = new float[]{charPosition.getPageNum(), charPosition.getX(), charPosition.getY(), charPosition.getWidth(), charPosition.getHeight()};
positionsList.add(positions);
}
PdfPageContentPositions pdfPageContentPositions = new PdfPageContentPositions();
pdfPageContentPositions.setContent(content);
pdfPageContentPositions.setPostions(positionsList);
result.add(pdfPageContentPositions);
}
reader.close();
return result;
}
private static List<float[]> findPositions(String keyword, PdfPageContentPositions pdfPageContentPositions) {
List<float[]> result = new ArrayList<float[]>();
String content = pdfPageContentPositions.getContent();
List<float[]> charPositions = pdfPageContentPositions.getPositions();
for (int pos = 0; pos < content.length(); ) {
int positionIndex = content.indexOf(keyword, pos);
if (positionIndex == -1) {
break;
}
float[] postions = charPositions.get(positionIndex);
//此处较为关键通过第一个关键字计算出整个关键字的宽度
for (int i = 1; i < keyword.length(); i++) {
float[] postionsNew = charPositions.get(positionIndex + i);
postions[3] = postions[3] + postionsNew[3];
}
result.add(postions);
pos = positionIndex + 1;
}
return result;
}
private static class PdfPageContentPositions {
private String content;
private List<float[]> positions;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public List<float[]> getPositions() {
return positions;
}
public void setPostions(List<float[]> positions) {
this.positions = positions;
}
}
private static class PdfRenderListener implements RenderListener {
private int pageNum;
private float pageWidth;
private float pageHeight;
private StringBuilder contentBuilder = new StringBuilder();
private List<CharPosition> charPositions = new ArrayList<CharPosition>();
public PdfRenderListener(int pageNum, float pageWidth, float pageHeight) {
this.pageNum = pageNum;
this.pageWidth = pageWidth;
this.pageHeight = pageHeight;
}
public void beginTextBlock() {
}
public void renderText(TextRenderInfo renderInfo) {
List<TextRenderInfo> characterRenderInfos = renderInfo.getCharacterRenderInfos();
for (TextRenderInfo textRenderInfo : characterRenderInfos) {
String word = textRenderInfo.getText();
if (word.length() > 1) {
word = word.substring(word.length() - 1, word.length());
}
Rectangle2D.Float rectangle = textRenderInfo.getAscentLine().getBoundingRectange();
float x = (float) rectangle.getX();
float y = (float) rectangle.getY();
// float x = (float)rectangle.getCenterX();
// float y = (float)rectangle.getCenterY();
// double x = rectangle.getMinX();
// double y = rectangle.getMaxY();
//这两个是关键字在所在页面的XY轴的百分比
float xPercent = Math.round(x / pageWidth * 10000) / 10000f;
float yPercent = Math.round((1 - y / pageHeight) * 10000) / 10000f;
// CharPosition charPosition = new CharPosition(pageNum, xPercent, yPercent);
CharPosition charPosition = new CharPosition(pageNum, x, y - fixHeight, (float) rectangle.getWidth(), (float) (rectangle.getHeight() == 0 ? defaultHeight : rectangle.getHeight()));
charPositions.add(charPosition);
contentBuilder.append(word);
}
}
public void endTextBlock() {
}
public void renderImage(ImageRenderInfo renderInfo) {
}
public String getContent() {
return contentBuilder.toString();
}
public List<CharPosition> getcharPositions() {
return charPositions;
}
}
private static class CharPosition {
private int pageNum = 0;
private float x = 0;
private float y = 0;
private float width;
private float height;
public CharPosition(int pageNum, float x, float y, float width, float height) {
this.pageNum = pageNum;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public int getPageNum() {
return pageNum;
}
public float getX() {
return x;
}
public float getY() {
return y;
}
public float getWidth() {
return width;
}
public float getHeight() {
return height;
}
@Override
public String toString() {
return "CharPosition{" +
"pageNum=" + pageNum +
", x=" + x +
", y=" + y +
", width=" + width +
", height=" + height +
'}';
}
}
}
package pps.core.common.pdf;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontProvider;
import com.itextpdf.text.pdf.BaseFont;
public class MyFontProvider implements FontProvider {
private BaseColor bc;
private String fontname;
private String encoding;
private boolean embedded;
private boolean cached;
private float size;
private int style;
private BaseFont baseFont;
public MyFontProvider() {
}
public MyFontProvider(BaseColor bc, String fontname, String encoding, boolean embedded, boolean cached, float size,
int style, BaseFont baseFont) {
super();
this.bc = bc;
this.fontname = fontname;
this.encoding = encoding;
this.embedded = embedded;
this.cached = cached;
this.size = size;
this.style = style;
this.baseFont = baseFont;
}
public BaseColor getBc() {
return bc;
}
public void setBc(BaseColor bc) {
this.bc = bc;
}
public String getFontname() {
return fontname;
}
public void setFontname(String fontname) {
this.fontname = fontname;
}
public String getEncoding() {
return encoding;
}
public void setEncoding(String encoding) {
this.encoding = encoding;
}
public boolean isEmbedded() {
return embedded;
}
public void setEmbedded(boolean embedded) {
this.embedded = embedded;
}
public boolean isCached() {
return cached;
}
public void setCached(boolean cached) {
this.cached = cached;
}
public float getSize() {
return size;
}
public void setSize(float size) {
this.size = size;
}
public int getStyle() {
return style;
}
public void setStyle(int style) {
this.style = style;
}
public BaseFont getBaseFont() {
return baseFont;
}
public void setBaseFont(BaseFont baseFont) {
this.baseFont = baseFont;
}
public Font getFont(String arg0, String arg1, boolean arg2, float arg3, int arg4, BaseColor arg5) {
Font font = null;
if (baseFont == null) {
font = new Font();
} else {
font = new Font(baseFont);
}
font.setColor(arg5);
font.setFamily(fontname);
font.setSize(size);
font.setStyle(arg4);
return font;
}
public boolean isRegistered(String arg0) {
// TODO Auto-generated method stub
return true;
}
}
package pps.core.common.pdf;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PatternUtils {
public static final String fangkuohao = "(\\[[^\\]]*\\])";
public static final String specialList = "([\\s\\S]*)";
private static final String xiaokuohao = "(?<=\\()(.+?)(?=\\))";
public static void main(String[] args) {
}
public static List<String> patternData(String data, String p) {
List<String> list = new ArrayList<>();
Pattern pattern = Pattern.compile(p);
Matcher matcher = pattern.matcher(data);
while ((matcher.find()))
list.add(matcher.group());
return list;
}
}
package pps.core.common.pdf;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import java.io.*;
public class UpdatePdfUtil {
/**
* @param bytes 数组
* @param contentText 添加的文字
* @return
* @throws Exception
*/
public static InputStream addText(byte[] bytes, String contentText)
throws Exception {
ByteArrayOutputStream byteArrayOutputStream = null;
PdfReader pdfReader = null;
PdfStamper stamper = null;
try {
byteArrayOutputStream = new ByteArrayOutputStream();
pdfReader = new PdfReader(bytes);
Rectangle rectangle = pdfReader.getPageSize(1);
stamper = new PdfStamper(pdfReader, byteArrayOutputStream);
PdfContentByte canvas = stamper.getUnderContent(1);
//替换关键字
canvas.beginText();
BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
canvas.setFontAndSize(bf, 13);
canvas.showTextAligned(Element.ALIGN_CENTER, contentText, rectangle.getWidth() - 50, rectangle.getHeight() - 20, 0);/*修正背景与文本的相对位置*/
canvas.endText();
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} finally {
if (byteArrayOutputStream != null) {
try {
byteArrayOutputStream.close();
} catch (IOException e) {
}
}
if (stamper != null)
try {
stamper.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (pdfReader != null) {
pdfReader.close();
}
}
return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
}
public static void main(String[] args) throws Exception {
File pdfFile = new File("D:\\pdf\\test1664334027252.pdf");
//2.定义一个byte数组,长度为文件的长度
byte[] pdfData = new byte[(int) pdfFile.length()];
//3.IO流读取文件内容到byte数组
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(pdfFile);
inputStream.read(pdfData);
addText(pdfData, "dasfad");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
}
}
}
}
}
......@@ -119,45 +119,6 @@
<finalName>${project.artifactId}</finalName>
<plugins>
<!--<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>install</id>
<phase>install</phase>
<configuration>
<target>
<copy todir="../lib" overwrite="true">
<fileset dir="${project.build.directory}/current/lib/"
erroronmissingdir="false">
<include name="*.jar" />
</fileset>
</copy>
&lt;!&ndash;<move file="${project.build.directory}/current/lib/deploy-pps-analysis.jar"
tofile="../lib/deploy-pps-analysis.jar" />&ndash;&gt;
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>clean</id>
<phase>clean</phase>
<configuration>
<target>
<delete file="../lib/deploy-pps-all.jar" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
......
......@@ -44,34 +44,6 @@
<env>test</env>
</properties>
</profile>
<!--xcd开发环境 -->
<profile>
<id>sit</id>
<properties>
<env>sit</env>
</properties>
</profile>
<!--xcd测试环境 -->
<profile>
<id>uat</id>
<properties>
<env>uat</env>
</properties>
</profile>
<!--vpn连接开发环境 -->
<profile>
<id>vpn</id>
<properties>
<env>vpn</env>
</properties>
</profile>
<!--xcd开发环境 -->
<profile>
<id>stg</id>
<properties>
<env>stg</env>
</properties>
</profile>
</profiles>
<build>
<filters>
......@@ -79,44 +51,6 @@
</filters>
<finalName>${project.artifactId}</finalName>
<plugins>
<!--<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>install</id>
<phase>install</phase>
<configuration>
<target>
<copy todir="../lib" overwrite="true">
<fileset dir="${project.build.directory}/current/lib/"
erroronmissingdir="false">
<include name="*.jar" />
</fileset>
</copy>
&lt;!&ndash;<move file="${project.build.directory}/current/lib/deploy-pps-analysis.jar"
tofile="../lib/deploy-pps-analysis.jar" />&ndash;&gt;
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>clean</id>
<phase>clean</phase>
<configuration>
<target>
<delete file="../lib/deploy-pps-gateway.jar" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
......
......@@ -79,81 +79,14 @@
<env>test</env>
</properties>
</profile>
<!--xcd开发环境 -->
<profile>
<id>sit</id>
<properties>
<env>sit</env>
</properties>
</profile>
<!--xcd测试环境 -->
<profile>
<id>uat</id>
<properties>
<env>uat</env>
</properties>
</profile>
<!--vpn连接开发环境 -->
<profile>
<id>vpn</id>
<properties>
<env>vpn</env>
</properties>
</profile>
<!--xcd开发环境 -->
<profile>
<id>stg</id>
<properties>
<env>stg</env>
</properties>
</profile>
</profiles>
<build>
<filters>
<filter>../filters/filter-${env}.properties</filter>
</filters>
<finalName>${project.artifactId}</finalName>
<plugins>
<!--<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>install</id>
<phase>install</phase>
<configuration>
<target>
<copy todir="../lib" overwrite="true">
<fileset dir="${project.build.directory}/current/lib/"
erroronmissingdir="false">
<include name="*.jar" />
</fileset>
</copy>
&lt;!&ndash;<move file="${project.build.directory}/current/lib/deploy-pps-analysis.jar"
tofile="../lib/deploy-pps-analysis.jar" />&ndash;&gt;
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>clean</id>
<phase>clean</phase>
<configuration>
<target>
<delete file="../lib/deploy-pps-task.jar" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
......
......@@ -40,11 +40,6 @@
<artifactId>huawei-gauss-db</artifactId>
<version>1.0</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>pps</groupId>-->
<!-- <artifactId>pps-core-framedata</artifactId>-->
<!-- <version>${pps.version}</version>-->
<!-- </dependency>-->
<dependency>
<groupId>xstartup</groupId>
<artifactId>xstartup-toolkit-kafka</artifactId>
......@@ -80,81 +75,14 @@
<env>test</env>
</properties>
</profile>
<!--xcd开发环境 -->
<profile>
<id>sit</id>
<properties>
<env>sit</env>
</properties>
</profile>
<!--xcd测试环境 -->
<profile>
<id>uat</id>
<properties>
<env>uat</env>
</properties>
</profile>
<!--vpn连接开发环境 -->
<profile>
<id>vpn</id>
<properties>
<env>vpn</env>
</properties>
</profile>
<!--xcd开发环境 -->
<profile>
<id>stg</id>
<properties>
<env>stg</env>
</properties>
</profile>
</profiles>
<build>
<filters>
<filter>../filters/filter-${env}.properties</filter>
</filters>
<finalName>${project.artifactId}</finalName>
<plugins>
<!--<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>install</id>
<phase>install</phase>
<configuration>
<target>
<copy todir="../lib" overwrite="true">
<fileset dir="${project.build.directory}/current/lib/"
erroronmissingdir="false">
<include name="*.jar" />
</fileset>
</copy>
&lt;!&ndash;<move file="${project.build.directory}/current/lib/deploy-pps-analysis.jar"
tofile="../lib/deploy-pps-analysis.jar" />&ndash;&gt;
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>clean</id>
<phase>clean</phase>
<configuration>
<target>
<delete file="../lib/deploy-pps-task.jar" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
......
......@@ -98,34 +98,6 @@
<env>test</env>
</properties>
</profile>
<!--sit -->
<profile>
<id>sit</id>
<properties>
<env>sit</env>
</properties>
</profile>
<!--vpn连接开发环境 -->
<profile>
<id>vpn</id>
<properties>
<env>vpn</env>
</properties>
</profile>
<!--uat -->
<profile>
<id>uat</id>
<properties>
<env>uat</env>
</properties>
</profile>
<!--xcd开发环境 -->
<profile>
<id>stg</id>
<properties>
<env>stg</env>
</properties>
</profile>
</profiles>
<build>
......@@ -135,44 +107,6 @@
<finalName>${project.artifactId}</finalName>
<plugins>
<!--<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>install</id>
<phase>install</phase>
<configuration>
<target>
<copy todir="../lib" overwrite="true">
<fileset dir="${project.build.directory}/current/lib/"
erroronmissingdir="false">
<include name="*.jar" />
</fileset>
</copy>
&lt;!&ndash;<move file="${project.build.directory}/current/lib/deploy-pps-analysis.jar"
tofile="../lib/deploy-pps-analysis.jar" />&ndash;&gt;
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>clean</id>
<phase>clean</phase>
<configuration>
<target>
<delete file="../lib/deploy-pps-system.jar" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
......
......@@ -80,81 +80,14 @@
<env>test</env>
</properties>
</profile>
<!--xcd开发环境 -->
<profile>
<id>sit</id>
<properties>
<env>sit</env>
</properties>
</profile>
<!--xcd测试环境 -->
<profile>
<id>uat</id>
<properties>
<env>uat</env>
</properties>
</profile>
<!--vpn连接开发环境 -->
<profile>
<id>vpn</id>
<properties>
<env>vpn</env>
</properties>
</profile>
<!--xcd开发环境 -->
<profile>
<id>stg</id>
<properties>
<env>stg</env>
</properties>
</profile>
</profiles>
<build>
<filters>
<filter>../filters/filter-${env}.properties</filter>
</filters>
<finalName>${project.artifactId}</finalName>
<plugins>
<!--<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>install</id>
<phase>install</phase>
<configuration>
<target>
<copy todir="../lib" overwrite="true">
<fileset dir="${project.build.directory}/current/lib/"
erroronmissingdir="false">
<include name="*.jar" />
</fileset>
</copy>
&lt;!&ndash;<move file="${project.build.directory}/current/lib/deploy-pps-analysis.jar"
tofile="../lib/deploy-pps-analysis.jar" />&ndash;&gt;
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>clean</id>
<phase>clean</phase>
<configuration>
<target>
<delete file="../lib/deploy-pps-task.jar" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment