|
@@ -0,0 +1,213 @@
|
|
|
+package org.springblade.manager.utils;
|
|
|
+
|
|
|
+import com.spire.ms.System.Collections.ArrayList;
|
|
|
+import com.sun.xml.bind.v2.util.FlattenIterator;
|
|
|
+import org.apache.pdfbox.pdmodel.PDDocument;
|
|
|
+import org.apache.pdfbox.pdmodel.PDPage;
|
|
|
+import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
|
|
+import org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState;
|
|
|
+import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
|
|
+import org.apache.pdfbox.pdmodel.graphics.PDLineDashPattern;
|
|
|
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
|
|
|
+import org.apache.pdfbox.pdmodel.interactive.measurement.PDViewportDictionary;
|
|
|
+import org.apache.pdfbox.text.PDFTextStripper;
|
|
|
+import org.apache.pdfbox.text.PDFTextStripperByArea;
|
|
|
+import org.apache.pdfbox.text.TextPosition;
|
|
|
+import org.springblade.common.utils.CommonUtil;
|
|
|
+import org.springblade.common.utils.FileUtils;
|
|
|
+import org.springblade.core.tool.utils.Func;
|
|
|
+import org.springblade.core.tool.utils.ResourceUtil;
|
|
|
+import org.springblade.manager.vo.PDFIndexInfo;
|
|
|
+
|
|
|
+import java.awt.geom.Point2D;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+public class CreateDashedLine {
|
|
|
+
|
|
|
+ public static void mai1n(String[] args) throws Exception {
|
|
|
+ // 获取值的坐标
|
|
|
+ // List<String> s_data39 = new java.util.ArrayList<>();
|
|
|
+
|
|
|
+ List<String> s_data34 = new java.util.ArrayList<>();
|
|
|
+ s_data34.add("2.8");
|
|
|
+ s_data34.add("7.66");
|
|
|
+/* s_data34.add("14.03");
|
|
|
+ s_data34.add("14.56");
|
|
|
+ s_data34.add("16.28");
|
|
|
+ s_data34.add("");
|
|
|
+ s_data34.add("16.42");
|
|
|
+ s_data34.add("16.57");
|
|
|
+ s_data34.add("16.73");
|
|
|
+ s_data34.add("16.9");
|
|
|
+ s_data34.add("17.07");
|
|
|
+ s_data34.add("17.26");*/
|
|
|
+
|
|
|
+ String pdfUrl = "/Users/hongchuangyanfa/Desktop/excel/1750071431349174274.pdf";
|
|
|
+
|
|
|
+ // getPDFInfo(pdfUrl,s_data39,"1");
|
|
|
+ getPDFInfo(pdfUrl,s_data34,"2");
|
|
|
+
|
|
|
+ System.out.println("");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static void getPDFInfo(String pdfUrl,List<String> s_data,String type) throws Exception {
|
|
|
+ PDDocument document = PDDocument.load(new File(pdfUrl));
|
|
|
+ //获取文件流
|
|
|
+ InputStream inputStream = new FileInputStream(new File(pdfUrl));
|
|
|
+ //转换
|
|
|
+ byte[] bytes = CommonUtil.InputStreamToBytes(inputStream);
|
|
|
+ List<PDFIndexInfo> keywordPostions = PdfAddimgUtil.findKeywordPostions(bytes, "累计%");
|
|
|
+
|
|
|
+ // 获取 实际完成=累计% 的坐标
|
|
|
+ PDFIndexInfo pdfIndexInfo = keywordPostions.get(0);
|
|
|
+ float s_with = pdfIndexInfo.getDataInfo()[1];
|
|
|
+ float s_heiht = pdfIndexInfo.getDataInfo()[2];
|
|
|
+
|
|
|
+ // 获取 计划完成 = 累计% 的坐标
|
|
|
+ PDFIndexInfo jPdf = keywordPostions.get(1);
|
|
|
+ float j_with = jPdf.getDataInfo()[1];
|
|
|
+ float j_heiht = jPdf.getDataInfo()[2];
|
|
|
+
|
|
|
+ System.out.println(s_with+"__"+s_heiht);
|
|
|
+ System.out.println(j_with+"__"+j_heiht);
|
|
|
+
|
|
|
+ List<PDFIndexInfo> allwordPostions = PdfAddimgUtil.findAllwordPostions(bytes);
|
|
|
+ PDFIndexInfo pdfIndex = allwordPostions.get(0);
|
|
|
+ List<float[]> listData = pdfIndex.getListData();
|
|
|
+ String context = pdfIndex.getPkeyid();
|
|
|
+
|
|
|
+ // 获取月份的y轴
|
|
|
+ int moth_y = context.indexOf("1234567891011121");
|
|
|
+ float[] moth = listData.get(moth_y);
|
|
|
+ System.out.println(moth[2]);
|
|
|
+ List<PDFIndexInfo> bf_list = PdfAddimgUtil.findKeywordPostions(bytes, "%");
|
|
|
+ float bf_x = 0f;
|
|
|
+ float bf_y = 0f;
|
|
|
+ for(PDFIndexInfo bf:bf_list){
|
|
|
+ if(bf.getDataInfo()[2]==moth[2]){
|
|
|
+ bf_x = bf.getDataInfo()[1];
|
|
|
+ bf_y = bf.getDataInfo()[2];
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ System.out.println("百分"+bf_x);
|
|
|
+ System.out.println("百分"+bf_y);
|
|
|
+
|
|
|
+ Map<String , float[]> data = new HashMap<>();
|
|
|
+ for(int i=20 ; i>=0 ; i--){
|
|
|
+ //测试数据
|
|
|
+ String key = i * 5 +"";
|
|
|
+ List<String> strList = Func.toStrList(key);
|
|
|
+ for (String text : strList) {
|
|
|
+ for (int pos = 0; pos < context.length(); ) {
|
|
|
+ int positionIndex = context.indexOf(text, pos);
|
|
|
+ if (positionIndex == -1) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ float[] postions = listData.get(positionIndex);
|
|
|
+ if((postions[1]==bf_x || Math.abs(postions[1]-bf_x)<=0.002) && postions[2]>bf_y){
|
|
|
+ data.put(key,postions);
|
|
|
+ bf_y =postions[2];
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ pos = positionIndex + 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ Map<String , float[]> s_data_xy = new HashMap<>();
|
|
|
+
|
|
|
+ for(String sval:s_data){
|
|
|
+ //测试数据
|
|
|
+ List<String> strList = Func.toStrList(sval);
|
|
|
+ for (String text : strList) {
|
|
|
+ for (int pos = 0; pos < context.length(); ) {
|
|
|
+ int positionIndex = context.indexOf(text, pos);
|
|
|
+ if (positionIndex == -1) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ float[] postions = listData.get(positionIndex);
|
|
|
+ if("1".equals(type)){
|
|
|
+ if((postions[2]==s_heiht || Math.abs(postions[2]-s_heiht)<=0.002 ) && postions[1]>s_with){
|
|
|
+ s_data_xy.put(sval,postions);
|
|
|
+ s_with = postions[1];
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if("2".equals(type)){
|
|
|
+ if((postions[2]==j_heiht || Math.abs(postions[2]-j_heiht)<=0.002 ) && postions[1]>s_with){
|
|
|
+ s_data_xy.put(sval,postions);
|
|
|
+ s_with = postions[1];
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ pos = positionIndex + 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ float pageHeight = document.getPage(0).getMediaBox().getHeight();
|
|
|
+ float pageWidth = document.getPage(0).getMediaBox().getWidth();
|
|
|
+ float oney = (data.get("0")[2] -data.get("5")[2])/5 * pageHeight;
|
|
|
+ //实线坐标
|
|
|
+ List<Float> points = new ArrayList();
|
|
|
+ for(String sval:s_data){
|
|
|
+ float[] floats = s_data_xy.get(sval);
|
|
|
+ if(floats!=null){
|
|
|
+ points.add( pageWidth * (floats[1])+2);
|
|
|
+ Double floor = Math.floor(Func.toDouble(sval) / 5)*5;
|
|
|
+
|
|
|
+ float add = (float) ((Func.toDouble(sval)-floor ) * oney);
|
|
|
+
|
|
|
+ String da2 = floor.intValue()+"";
|
|
|
+ float[] floats1 = data.get(da2);
|
|
|
+ points.add( pageHeight- pageHeight * (floats1[2]) + add );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ PDFFill(points,pdfUrl,type);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void PDFFill(List<Float> points,String fileUrl,String type) throws Exception {
|
|
|
+ // 创建一个PDF文档
|
|
|
+ PDDocument document = PDDocument.load(new File(fileUrl));
|
|
|
+
|
|
|
+ // 创建一个页面
|
|
|
+ PDPage page = document.getPage(0);
|
|
|
+ // 纸张大小
|
|
|
+
|
|
|
+ try (PDPageContentStream contentStream = new PDPageContentStream(document, page,PDPageContentStream.AppendMode.APPEND, true, true)) {
|
|
|
+ // 设置线宽和颜色
|
|
|
+ PDExtendedGraphicsState extendedGraphicsState = new PDExtendedGraphicsState();
|
|
|
+ extendedGraphicsState.setLineWidth(1f);
|
|
|
+ contentStream.setGraphicsStateParameters(extendedGraphicsState);
|
|
|
+ float[] a={4,2};
|
|
|
+ contentStream.setLineDashPattern(a, 2);
|
|
|
+
|
|
|
+ // 定义点集合,表示折线上的点
|
|
|
+ List<Point2D.Float> points1 = new java.util.ArrayList<>();
|
|
|
+ for (int i = 0; i < points.size(); i += 2) {
|
|
|
+ points1.add(new Point2D.Float(points.get(i), points.get(i + 1)));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 绘制折线
|
|
|
+ Point2D.Float current = null;
|
|
|
+ for (Point2D.Float point : points1) {
|
|
|
+ if (current != null) {
|
|
|
+ contentStream.drawLine(current.x, current.y, point.x, point.y);
|
|
|
+ }
|
|
|
+ current = point;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ document.save("/Users/hongchuangyanfa/Desktop/excel/test_info.pdf");
|
|
|
+ document.close();
|
|
|
+ System.out.println("完成");
|
|
|
+ }
|
|
|
+}
|