update 文档拆分

This commit is contained in:
lijiarao
2023-09-21 17:03:00 +08:00
parent 7cd4c1eff2
commit f20bde5e55
10 changed files with 247 additions and 59 deletions
@@ -1,5 +1,6 @@
package com.jero.modules.split.service.impl;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itextpdf.text.ListLabel;
import com.jero.common.exception.JeroBootException;
@@ -14,8 +15,10 @@ import com.jero.modules.split.enums.SplitFileTypeTypeEnum;
import com.jero.modules.split.mapper.SarFileSplitMenuEOMapper;
import com.jero.modules.split.service.IFileSplitItemsEOService;
import com.jero.modules.split.service.ISarFileSplitItemsValEOService;
import com.jero.modules.split.util.AsposeUtil;
import com.jero.modules.split.util.FileSplitUtils;
import com.jero.modules.split.util.ReadWordTable;
import com.jero.modules.split.vo.TitleNumberVO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.openxml4j.util.ZipSecureFile;
@@ -43,6 +46,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.List;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -139,16 +143,21 @@ public class FileSpiltService {
OSSFile ossFile = ossFileList.get(0);
String readFilePath = ossFile.getUrl();
//if (StringUtils.isEmpty(readFilePath) || !MinioUtil.doesObjectExist(readFilePath)) {
// throw new JeroBootException("当前文件未找到,请重新选择!");
//}
if (StringUtils.isEmpty(readFilePath) || !MinioUtil.doesObjectExist(readFilePath)) {
throw new JeroBootException("当前文件未找到,请重新选择!");
}
//try (InputStream is = MinioUtil.download(readFilePath);) {
readFilePath = "E:\\Data\\Downloads\\GB_4785_2019_CN.docx";
try (InputStream is = new FileInputStream(readFilePath);) {//需要将文件路更改为word文档所在路径。
try (InputStream is = MinioUtil.download(readFilePath);InputStream is1 = MinioUtil.download(readFilePath)) {
//readFilePath = "E:\\Data\\Downloads\\wordtest\\GB7258标准修订test.docx";
//try (InputStream is = new FileInputStream(readFilePath);InputStream is1 = new FileInputStream(readFilePath)) {//需要将文件路更改为word文档所在路径。
ZipSecureFile.setMinInflateRatio(-1.0d);
XWPFDocument doc = new XWPFDocument(is);
TitleNumberVO titleNumberVO = AsposeUtil.getNumber(is1);
//TitleNumberVO titleNumberVO = new TitleNumberVO();
Map<String, String> titleNumberMap = titleNumberVO.getTitleNumberMap();
List<String> emptyTitleList = titleNumberVO.getEmptyTitleList();
Pattern ptest = Pattern.compile("^1范围");
Matcher matcher;
@@ -182,8 +191,9 @@ public class FileSpiltService {
if (messageList.size() > 0) {
addItermsConditionsImage(messageList.get(messageList.size() - 1), itemValList, p, imageBundleList);
}
continue;
}
continue;
}
paragraphString = paragraphString.replaceAll("\r\n","");
paragraphString = paragraphString.replaceAll("\n","");
@@ -208,8 +218,8 @@ public class FileSpiltService {
if (messageList.size() > 0) {
addItermsConditionsImage(messageList.get(messageList.size() - 1), itemValList, p, imageBundleList);
}
continue;
}
continue;
}
if (StringUtils.isNotEmpty(p.getNumLevelText()) && p.getNumLevelText().indexOf(".") > 0) {
String numberParent = p.getNumLevelText().substring(0, p.getNumLevelText().lastIndexOf("."));
@@ -230,7 +240,9 @@ public class FileSpiltService {
if (messageNumber == 0) {
ptest = Pattern.compile("^1\\s{0,50}范围");
matcher = ptest.matcher(paragraphString);
while (matcher.find()) {
ptest = Pattern.compile("^\\s{0,50}范围");
Matcher matcher1 = ptest.matcher(paragraphString);;
while (matcher.find() || matcher1.find()) {
List<String> clauseContent = new ArrayList<>();
SarFileSplitItemsEO message = getSarFileSplitItemsEO(sarFileSplitInfoEO.getId(), "1", "范围", clauseContent);
SarFileSplitMenuEO documentTreeEO1 = new SarFileSplitMenuEO();
@@ -260,7 +272,28 @@ public class FileSpiltService {
else if (messageNumber >= 1) {
ptest = Pattern.compile("^[\\d.]*$");
boolean hasGoIf = false;
String startDigit = getOneStartDigit(paragraphString, enumByValue);
String nextParagraphString = getNextParagraphString(g, elements, enumByValue);
//替换序号
boolean replaceNumberFlag = false;
String replaceNumber = "";
if (!emptyTitleList.isEmpty() && StringUtils.isBlank(paragraphString) && StringUtils.isNotBlank(nextParagraphString)){
paragraphString = emptyTitleList.get(0);
emptyTitleList.remove(0);
}else if (StringUtils.isNotBlank(paragraphString) && titleNumberMap.containsKey(paragraphString)){
replaceNumberFlag = true;
replaceNumber = titleNumberMap.get(paragraphString);
paragraphString = replaceNumber + paragraphString;
}
String startDigit;
if (replaceNumberFlag){
startDigit = replaceNumber;
}else {
startDigit = getOneStartDigit(paragraphString, enumByValue);
}
if (startDigit.equals("15.8")){
ptest = Pattern.compile("^[\\d.]*$");
}
for (int i = 0; i < nowStart.size(); i++) {
// if (paragraphString.startsWith(nowStart.get(i))) {
if (startDigit.equals(nowStart.get(i))) {
@@ -302,9 +335,13 @@ public class FileSpiltService {
// 判断是否是附录
ptest = Pattern.compile("^附录\\s{0,3}[A-Z]{1}");
matcher = ptest.matcher(paragraphString);
String title = "";
if (StringUtils.isNotBlank(paragraphString) && titleNumberMap.containsKey(paragraphString)){
title = titleNumberMap.get(paragraphString);
}
// 获取当前最后一项num
String appendixNum = messageList.get(messageList.size() - 1).getItemsNum().split("\\.")[0];
if (matcher.find() && !messageList.get(messageList.size() - 1).getItemsNum().equals("附录")) {
if (matcher.find()&& !messageList.get(messageList.size() - 1).getItemsNum().equals("附录")) {
if (Integer.valueOf(appendixNum) + 1 <= sarFileSplitInfoEO.getStopNumber()) {
String itemName = "附录";
List<String> clauseContent = new ArrayList<>();
@@ -361,9 +398,32 @@ public class FileSpiltService {
}
// public static void main(String[] args) {
// fileEU(new SarFileSplitInfoEO());
// }
private String getNextParagraphString(int g, List<IBodyElement> elements, SplitFileTypeTypeEnum enumByValue) {
if (elements.size()-1 == g){
return "";
}
IBodyElement element = elements.get(g+1);
// 段落
if (element instanceof XWPFParagraph) {
XWPFParagraph p = (XWPFParagraph) element;
// 处理段落生成编号不识别问题
String paragraphString = p.getText();
if (StringUtils.isNotBlank(paragraphString)) {
paragraphString = paragraphString.replace((char) 12288, ' ');
paragraphString = paragraphString.trim();
}
if (StringUtils.isEmpty(paragraphString)) {
return "";
}
paragraphString = paragraphString.replaceAll("\r\n", "");
paragraphString = paragraphString.replaceAll("\n", "");
paragraphString = paragraphString.trim();
return paragraphString;
}
return "";
}
@@ -373,7 +433,7 @@ public class FileSpiltService {
* @param nowStart
* @return
*/
public static List<String> getNewNowStart(String nowStart) {
public List<String> getNewNowStart(String nowStart) {
// List<String> nowStartList = new ArrayList<>();
List<String> nowResultStartList = new ArrayList<>();
String[] numberStr = nowStart.split("\\.");
@@ -857,11 +917,7 @@ public class FileSpiltService {
byte[] bytev = pictureData.getData();
String filepathandname = filePathCos + "/" + imageName;
try {
// String fileUrl = splitUrl + "/" + filepathandname;
// BASE64Encoder base64Encoder = new BASE64Encoder();
// String encode = base64Encoder.encode(filepathandname.getBytes(StandardCharsets.UTF_8));
String path = splitUrl+"/jero-boot/sys/split/file/getImage?fileUrl="+filepathandname;
String path = splitUrl+imageName;
MinioUtil.uploadByte(bytev, filepathandname);
String imgCon = path;
String imgConVal = "<img class=\"wordImg\" style=\"width:100%;height:100%\" src=\"" + path + "\">";
@@ -938,6 +994,9 @@ public class FileSpiltService {
}
public String getOneStartDigit(String arr, SplitFileTypeTypeEnum enumByValue) {
//\p{Punct} 标点符号:!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
//\p{Space} 空白字符:[ \t\n\x0B\f\r]
//\p{Digit} 十进制数字:[0-9]
String regex="[^\\p{Punct}\\p{Space}\\p{Digit}]"; // TODO
switch (enumByValue) {
case GB:
@@ -1267,7 +1267,7 @@ public class FileSplitItemsEOServiceImpl extends ServiceImpl<FileSplitItemsEOMap
// BASE64Encoder base64Encoder = new BASE64Encoder();
// String encode = base64Encoder.encode(oSSFile.getUrl().getBytes(StandardCharsets.UTF_8));
String path = splitUrl + "/jero-boot/sys/split/file/getImage?fileUrl=" + oSSFile.getUrl();
String path = splitUrl + oSSFile.getUrl();
// String path = imgpath + oSSFile.getUrl().substring(oSSFile.getUrl().lastIndexOf(File.separator)+1);
String imgCon = "<img class=\"wordImg\" src=\"" + path + "\">";
valEO.setType("IMG");
@@ -1317,7 +1317,7 @@ public class FileSplitItemsEOServiceImpl extends ServiceImpl<FileSplitItemsEOMap
// BASE64Encoder base64Encoder = new BASE64Encoder();
// String encode = base64Encoder.encode(oSSFile.getUrl().getBytes(StandardCharsets.UTF_8));
String path = splitUrl + "/jero-boot/sys/split/file/getImage?fileUrl=" + oSSFile.getUrl();
String path = splitUrl + oSSFile.getUrl();
// String path = imgpath + oSSFile.getUrl().substring(oSSFile.getUrl().lastIndexOf(File.separator)+1);
String imgCon = "<img class=\"wordImg\" src=\"" + path + "\">";
valEO.setType("IMG");
@@ -1364,7 +1364,7 @@ public class FileSplitItemsEOServiceImpl extends ServiceImpl<FileSplitItemsEOMap
// BASE64Encoder base64Encoder = new BASE64Encoder();
// String encode = base64Encoder.encode(oSSFile.getUrl().getBytes(StandardCharsets.UTF_8));
String path = splitUrl + "/jero-boot/sys/split/file/getImage?fileUrl=" + oSSFile.getUrl();
String path = splitUrl + oSSFile.getUrl();
// String path = imgpath + oSSFile.getUrl().substring(oSSFile.getUrl().lastIndexOf(File.separator)+1);
String imgCon = "<img class=\"wordImg\" src=\"" + path + "\">";
valEO.setType("IMG");
@@ -1,52 +1,71 @@
package com.jero.modules.split.util;
import com.aspose.words.*;
import com.jero.modules.split.vo.TitleNumberVO;
import org.apache.commons.lang3.StringUtils;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author liJiaRao
* @date 2023-09-20 14:42
*/
public class AsposeUtil {
public static void main(String[] args) throws Exception {
// Document doc = new Document("C:\\wordtest\\GB7258标准修订test.docx");
Document doc = new Document("E:\\Data\\Downloads\\wordtest\\GB7258标准修订test - 表格测试.docx");
//Document doc = new Document("E:\\Data\\Downloads\\wordtest\\GB7258标准修订test.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
public static TitleNumberVO getNumber(InputStream is) {
TitleNumberVO titleNumberVO = new TitleNumberVO();
Map<String, String> titleNumberMap = new HashMap<>();
List<String> emptyTitleList = new ArrayList<>();
try {
Document doc = new Document(is);
builder.insertField(FieldType.FIELD_NUM_PAGES, true);
DocumentBuilder builder = new DocumentBuilder(doc);
NodeCollection childNodes = doc.getChildNodes(NodeType.ANY, true); //获取段落
builder.insertField(FieldType.FIELD_NUM_PAGES, true);
//获取段落
NodeCollection childNodes = doc.getChildNodes(NodeType.ANY, true);
for (int i = 0; i < childNodes.getCount(); i++) {
Node next = childNodes.get(i);
for (int i = 0; i < childNodes.getCount(); i++) {
Node next = childNodes.get(i);
if (next instanceof Paragraph) {
Paragraph node = (Paragraph) next;
String text = node.getText();
text = text.replaceAll("\t", "").replaceAll("\b", "")
.replaceAll(" ", "").replaceAll("\"", "").replaceAll("'", "");
String labelString = "";
if (node.getListFormat().isListItem()) {
ListLabel listLabel = node.getListLabel();
labelString = listLabel.getLabelString().trim();
labelString = labelString.replaceAll("\t", "").replaceAll("\b", "")
.replaceAll(" ", "").replaceAll("\"", "").replaceAll("'", "").replaceAll(" ","");
if (next instanceof Paragraph) {
Paragraph node = (Paragraph) next;
String text = node.getText();
if (text.contains("规范性附录")){
text = node.getText();
}
text = text.replaceAll("\t", "")
.replaceAll("\u000B", "")
.replaceAll("\b", "")
.replaceAll("\"", "")
.replaceAll("'", "")
.replaceAll("\r\n","")
.replaceAll("\n","")
.trim();
String labelString = "";
if (node.getListFormat().isListItem()) {
ListLabel listLabel = node.getListLabel();
labelString = listLabel.getLabelString().trim();
labelString = labelString.replaceAll("\t", "").replaceAll("\b", "")
.replaceAll(" ", "").replaceAll("\"", "").replaceAll("'", "").replaceAll(" ", "");
//System.out.println(labelString.substring(0,labelString.length()-1));
System.out.println("Text:"+labelString + text);
System.out.println("Text:" + labelString + text);
if (StringUtils.isNotBlank(text)) {
titleNumberMap.put(text, labelString);
} else {
emptyTitleList.add(labelString);
}
}
}
}
//if (next instanceof Table) {
// System.out.println("Table:"+next);
//}
//if (next instanceof Image) {
// System.out.println("ImageData:"+next);
//}
} catch (Exception e) {
e.printStackTrace();
}
titleNumberVO.setTitleNumberMap(titleNumberMap);
titleNumberVO.setEmptyTitleList(emptyTitleList);
return titleNumberVO;
}
}
@@ -0,0 +1,16 @@
package com.jero.modules.split.vo;
import lombok.Data;
import java.util.List;
import java.util.Map;
/**
* @author liJiaRao
* @date 2023-09-20 17:17
*/
@Data
public class TitleNumberVO {
private Map<String, String> titleNumberMap;
private List<String> emptyTitleList;
}