doc网上专用接口
This commit is contained in:
71
src/main/java/com/example/ts_obj/Util/FileDownloadUtil.java
Normal file
71
src/main/java/com/example/ts_obj/Util/FileDownloadUtil.java
Normal file
@@ -0,0 +1,71 @@
|
||||
package com.example.ts_obj.Util;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
public class FileDownloadUtil {
|
||||
|
||||
/**
|
||||
* @return
|
||||
* @description: 从服务器获得一个输入流
|
||||
*/
|
||||
public static InputStream getInputStream(String urlPath) {
|
||||
InputStream inputStream = null;
|
||||
HttpURLConnection httpURLConnection = null;
|
||||
try {
|
||||
URL url = new URL(urlPath);
|
||||
httpURLConnection = (HttpURLConnection) url.openConnection();
|
||||
// 设置网络连接超时时间
|
||||
httpURLConnection.setConnectTimeout(3000);
|
||||
// 设置应用程序要从网络连接读取数据
|
||||
httpURLConnection.setDoInput(true);
|
||||
httpURLConnection.setRequestMethod("GET");
|
||||
int responseCode = httpURLConnection.getResponseCode();
|
||||
System.out.println("responseCode is:" + responseCode);
|
||||
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||
// 从服务器返回一个输入流
|
||||
inputStream = httpURLConnection.getInputStream();
|
||||
} else {
|
||||
inputStream = httpURLConnection.getErrorStream();
|
||||
}
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return inputStream;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param resp
|
||||
* @param inputStream
|
||||
* @description: 将输入流输出到页面
|
||||
*/
|
||||
public static void writeFile(HttpServletResponse resp, InputStream inputStream) {
|
||||
OutputStream out = null;
|
||||
try {
|
||||
out = resp.getOutputStream();
|
||||
int len = 0;
|
||||
byte[] b = new byte[1024];
|
||||
while ((len = inputStream.read(b)) != -1) {
|
||||
out.write(b, 0, len);
|
||||
}
|
||||
out.flush();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (out != null) {
|
||||
out.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
76
src/main/java/com/example/ts_obj/Util/ImageIOHelper.java
Normal file
76
src/main/java/com/example/ts_obj/Util/ImageIOHelper.java
Normal file
@@ -0,0 +1,76 @@
|
||||
package com.example.ts_obj.Util;
|
||||
|
||||
|
||||
import com.github.jaiimageio.plugins.tiff.TIFFImageWriteParam;
|
||||
|
||||
import javax.imageio.*;
|
||||
import javax.imageio.metadata.IIOMetadata;
|
||||
import javax.imageio.stream.FileImageInputStream;
|
||||
import javax.imageio.stream.ImageInputStream;
|
||||
import javax.imageio.stream.ImageOutputStream;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* 识别图片中的文字
|
||||
*
|
||||
* @author zlj
|
||||
*
|
||||
*/
|
||||
public class ImageIOHelper {
|
||||
/**
|
||||
* 创建临时图片文件
|
||||
*
|
||||
* @param imageFile
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public File createImage(File imageFile) throws IOException {
|
||||
Iterator<ImageReader> readers = ImageIO.getImageReaders(new FileImageInputStream(imageFile));
|
||||
ImageReader reader = readers.next();
|
||||
ImageInputStream iis = ImageIO.createImageInputStream(imageFile);
|
||||
reader.setInput(iis);
|
||||
|
||||
IIOMetadata streamMetadata = reader.getStreamMetadata();
|
||||
TIFFImageWriteParam tiffWriteParam = new TIFFImageWriteParam(Locale.CHINESE);
|
||||
tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_DISABLED);
|
||||
|
||||
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("tiff");
|
||||
ImageWriter writer = writers.next();
|
||||
BufferedImage bi = reader.read(0);
|
||||
|
||||
IIOImage image = new IIOImage(bi, null, reader.getImageMetadata(0));
|
||||
File tempFile = tempImageFile(imageFile);
|
||||
ImageOutputStream ios = ImageIO.createImageOutputStream(tempFile);
|
||||
writer.setOutput(ios);
|
||||
writer.write(streamMetadata, image, tiffWriteParam);
|
||||
|
||||
ios.close();
|
||||
iis.close();
|
||||
writer.dispose();
|
||||
reader.dispose();
|
||||
return tempFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加后缀 tempfile
|
||||
*
|
||||
* @param imageFile
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
private File tempImageFile(File imageFile) throws IOException {
|
||||
String path = imageFile.getPath();
|
||||
StringBuffer strB = new StringBuffer(path);
|
||||
strB.insert(path.lastIndexOf('.'), "_text_recognize_temp");
|
||||
String s = strB.toString().replaceFirst("(?<=//.)(//w+)$", "tif");
|
||||
Runtime.getRuntime().exec("attrib " + "\"" + s + "\"" + " +H"); // 设置文件隐藏
|
||||
return new File(strB.toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,14 +1,20 @@
|
||||
package com.example.ts_obj.controller;
|
||||
|
||||
import com.example.ts_obj.Util.*;
|
||||
import com.spire.doc.*;
|
||||
import com.spire.doc.Document;
|
||||
import com.spire.doc.collections.CellCollection;
|
||||
import com.spire.doc.collections.RowCollection;
|
||||
import com.spire.doc.collections.SectionCollection;
|
||||
import com.spire.doc.collections.TableCollection;
|
||||
import com.spire.doc.documents.*;
|
||||
import com.spire.doc.fields.DocPicture;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.deepoove.poi.XWPFTemplate;
|
||||
import com.deepoove.poi.config.Configure;
|
||||
import com.deepoove.poi.data.*;
|
||||
import com.deepoove.poi.data.style.Style;
|
||||
import com.deepoove.poi.policy.ParagraphRenderPolicy;
|
||||
import com.example.ts_obj.Util.CustomTableRenderPolicy;
|
||||
import com.example.ts_obj.Util.Util;
|
||||
import com.example.ts_obj.Util.XWPFUtils;
|
||||
import com.example.ts_obj.bean.ReturnCodeAndMsgEnum;
|
||||
import com.example.ts_obj.bean.ReturnValue;
|
||||
import com.example.ts_obj.entity.Typeset;
|
||||
@@ -19,12 +25,14 @@ import com.example.ts_obj.service.TypesetService;
|
||||
import com.example.ts_obj.service.UserService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import jdk.nashorn.internal.runtime.regexp.joni.Regex;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.poi.openxml4j.util.ZipSecureFile;
|
||||
import org.apache.poi.util.Units;
|
||||
import org.apache.poi.xwpf.usermodel.*;
|
||||
import org.apache.xmlbeans.XmlCursor;
|
||||
import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline;
|
||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -33,9 +41,15 @@ import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.awt.*;
|
||||
import java.io.*;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/typeset")
|
||||
@@ -44,8 +58,10 @@ public class TypesetController {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(UserController.class);
|
||||
|
||||
private String BASE_DIR="E:/upload/";
|
||||
private static String NEW_BASE_DIR="E:/upload/20220520/";
|
||||
private String BASE_DIR="C:\\Users\\Administrator\\Desktop\\typesetcode\\upload\\";
|
||||
private static String NEW_BASE_DIR="C:\\Users\\Administrator\\Desktop\\typesetcode\\upload\\20220520\\";
|
||||
|
||||
private static String IMG_DIR="C:\\Users\\Administrator\\Desktop\\typesetcode\\pictures\\";
|
||||
// private String BASE_DIR = "/home/wwwroot/ts.tmrjournals.com/upload/";
|
||||
@Autowired
|
||||
private TypesetService typesetservice;
|
||||
@@ -386,6 +402,49 @@ public class TypesetController {
|
||||
return new ReturnValue(ReturnCodeAndMsgEnum.Success, al);
|
||||
}
|
||||
|
||||
//网上专用文件读取接口
|
||||
@ApiOperation(value = "网上专用文件读取接口")
|
||||
@PostMapping("webReaddoc")
|
||||
public ReturnValue DocReader(String fileRoute, HttpServletRequest request, HttpServletResponse response) {
|
||||
|
||||
ArrayList al = new ArrayList();
|
||||
try {
|
||||
InputStream inputStream = FileDownloadUtil.getInputStream(fileRoute);
|
||||
// FileDownloadUtil.writeFile(response, inputStream);
|
||||
XWPFDocument xdoc = new XWPFDocument(inputStream);
|
||||
List<XWPFParagraph> list = xdoc.getParagraphs();
|
||||
|
||||
String cache = "";
|
||||
String cach = "";
|
||||
for (XWPFParagraph para:list) {
|
||||
List<XWPFRun> runs = para.getRuns();
|
||||
for (XWPFRun r : runs) {
|
||||
System.out.println("run====>"+r);
|
||||
if (r.getText(0) == null) {
|
||||
continue;
|
||||
}
|
||||
if (r.isItalic() && r.isBold()) {
|
||||
cach = "<b><i>" + r.getText(0) + "</i></b>";
|
||||
} else if (r.isItalic()) {
|
||||
cach = "<i>" + r.getText(0) + "</i>";
|
||||
} else if (r.isBold()) {
|
||||
cach = "<b>" + r.getText(0) + "</b>";
|
||||
} else {
|
||||
cach = r.getText(0);
|
||||
}
|
||||
cache += cach;
|
||||
}
|
||||
}
|
||||
al.add(cache);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
|
||||
return new ReturnValue(ReturnCodeAndMsgEnum.Success, al);
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/mytest")
|
||||
public ReturnValue mytest(Long typesetId) {
|
||||
@@ -728,7 +787,7 @@ public class TypesetController {
|
||||
//遍历照片
|
||||
// for (XWPFPictureData picture: allPictures) {
|
||||
byte[] data = pictureData.getData();
|
||||
File filePicture = new File("C:\\Users\\yl\\Desktop\\pictures\\" + imageName.replace("image",""));
|
||||
File filePicture = new File(IMG_DIR + imageName.replace("image",""));
|
||||
if (!filePicture.exists()){
|
||||
if (filePicture.getParentFile().exists()) {
|
||||
filePicture.getParentFile().mkdirs();
|
||||
@@ -751,31 +810,157 @@ public class TypesetController {
|
||||
XWPFDocument newXdoc = new XWPFDocument(newFis);
|
||||
List<XWPFParagraph> paragraphs = newXdoc.getParagraphs();
|
||||
|
||||
for (int i=0 ; i < paragraphs.size() ; i++){
|
||||
System.out.println("内容为"+paragraphs.get(i).getRuns());
|
||||
List<XWPFRun> runs = paragraphs.get(i).getRuns();
|
||||
String replace = String.valueOf(runs).replace(",", "").replace(" ","");
|
||||
for (String pic: picNameList){
|
||||
if (replace.contains(picMap.get(pic).replace(",","").replace(" ",""))){
|
||||
System.out.println("包含执行了");
|
||||
//获取光标
|
||||
XmlCursor xmlCursor = paragraphs.get(i+1).getCTP().newCursor();
|
||||
XWPFParagraph xwpfParagraph = newXdoc.insertNewParagraph(xmlCursor);
|
||||
|
||||
XWPFRun run = xwpfParagraph.createRun();
|
||||
run.addBreak(BreakClear.ALL);
|
||||
// run.setText("{{@image}}");
|
||||
FileInputStream inputStream = new FileInputStream("C:\\Users\\yl\\Desktop\\pictures\\"+pic.replace("image",""));
|
||||
run.addPicture(inputStream,XWPFDocument.PICTURE_TYPE_JPEG,pic, Units.toEMU(500),Units.toEMU(180));
|
||||
//
|
||||
//其他方式
|
||||
Document doc = new Document();
|
||||
doc.loadFromFile(this.NEW_BASE_DIR + newFileName);
|
||||
Section sec = doc.getSections().get(0);
|
||||
System.out.println("sec=================>"+sec);
|
||||
String text = doc.getText();
|
||||
System.out.println("text=================>"+text);
|
||||
Paragraph para = sec.getParagraphs().get(0);
|
||||
System.out.println("para=================>"+para);
|
||||
for(int i = 0; i < doc.getSections().getCount(); i++) {
|
||||
Section section = doc.getSections().get(i);
|
||||
for (int j = 0; j < section.getParagraphs().getCount(); j++) {
|
||||
Paragraph paragraph = section.getParagraphs().get(j);
|
||||
System.out.println(j+"新方式内容"+paragraph.getText());
|
||||
for (String pic: picNameList) {
|
||||
String replace = String.valueOf(paragraph.getText()).replace(",", "").replace(" ", "");
|
||||
if (replace.contains(picMap.get(pic).replace(",", "").replace(" ", ""))) {
|
||||
System.out.println("包含");
|
||||
String imgPath = IMG_DIR + pic.replace("image", "");
|
||||
//判断图片中是否包含 文字
|
||||
// String valCode = new OCRUtil().recognizeText(new File(imgPath));
|
||||
// System.out.println("valCode==============>"+valCode);
|
||||
// //正则表达式
|
||||
// Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
|
||||
// Matcher m = p.matcher(valCode);
|
||||
// System.out.println(m.find());
|
||||
// if (m.find()){
|
||||
// System.out.println("查找到了文字");
|
||||
// //1.包含中文 单栏
|
||||
// DocPicture docPicture = paragraph.appendPicture(imgPath);
|
||||
// docPicture.setWidth(500f);
|
||||
// docPicture.setHeight(180f);
|
||||
// docPicture.setTextWrappingStyle(TextWrappingStyle.Top_And_Bottom);
|
||||
// docPicture.setTextWrappingType(TextWrappingType.Both);
|
||||
// }else {
|
||||
//2.不包含 判断图片大小决定 图片为单栏还是双栏
|
||||
if (pic.replace("image","").equals("1.png")){
|
||||
//图片为小图 双栏
|
||||
DocPicture docPicture = paragraph.appendPicture(imgPath);
|
||||
docPicture.setWidth(200f);
|
||||
docPicture.setHeight(180f);
|
||||
docPicture.setTextWrappingStyle(TextWrappingStyle.Top_And_Bottom);
|
||||
docPicture.setTextWrappingType(TextWrappingType.Both);
|
||||
}else {
|
||||
//图片为大图 单栏
|
||||
DocPicture docPicture = paragraph.appendPicture(imgPath);
|
||||
docPicture.setWidth(500f);
|
||||
docPicture.setHeight(180f);
|
||||
docPicture.setTextWrappingStyle(TextWrappingStyle.Top_And_Bottom);
|
||||
docPicture.setTextWrappingType(TextWrappingType.Both);
|
||||
}
|
||||
// }
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
FileOutputStream os = new FileOutputStream("C:\\Users\\yl\\Desktop\\pictures\\New.doc");
|
||||
newXdoc.write(os);
|
||||
SectionCollection secs = doc.getSections();
|
||||
//处理表格
|
||||
for (int i = 0; i < secs.getCount(); i++) {
|
||||
//获取第一个section
|
||||
// Section section = doc.getSections().get(0);
|
||||
|
||||
TableCollection tables = secs.get(i).getTables();
|
||||
System.out.println(tables);
|
||||
//获取section中第一个表格
|
||||
// Table table = sec.getTables().get(0);
|
||||
int count = tables.getCount();
|
||||
System.out.println("===================>"+count);
|
||||
for (int j = 0; j < tables.getCount(); j++) {
|
||||
//给表格应用样式
|
||||
tables.get(j).applyStyle(DefaultTableStyle.Table_Classic_1);
|
||||
// //设置表格的右边框
|
||||
// tables.get(j).getTableFormat().getBorders().getRight().setBorderType(BorderStyle.Hairline);
|
||||
// tables.get(j).getTableFormat().getBorders().getRight().setLineWidth(0F);
|
||||
// tables.get(j).getTableFormat().getBorders().getRight().setColor(Color.white);
|
||||
//
|
||||
//设置表格的顶部边框
|
||||
tables.get(j).getTableFormat().getBorders().getTop().setBorderType(BorderStyle.Hairline);
|
||||
tables.get(j).getTableFormat().getBorders().getTop().setLineWidth(2.25F);
|
||||
tables.get(j).getTableFormat().getBorders().getTop().setColor(Color.black);
|
||||
//
|
||||
// //设置表格的左边框
|
||||
// tables.get(j).getTableFormat().getBorders().getLeft().setBorderType(BorderStyle.Hairline);
|
||||
// tables.get(j).getTableFormat().getBorders().getLeft().setLineWidth(0F);
|
||||
// tables.get(j).getTableFormat().getBorders().getLeft().setColor(Color.white);
|
||||
//
|
||||
//设置表格的底部边框
|
||||
tables.get(j).getTableFormat().getBorders().getBottom().setBorderType(BorderStyle.Hairline);
|
||||
// tables.get(j).getTableFormat().getBorders().getLeft().setColor(Color.black);
|
||||
tables.get(j).getTableFormat().getBorders().getBottom().setLineWidth(2.25F);
|
||||
//设置表格的水平和垂直边框
|
||||
// tables.get(j).getTableFormat().getBorders().getVertical().setBorderType(BorderStyle.Hairline);
|
||||
// tables.get(j).getTableFormat().getBorders().getHorizontal().setBorderType(BorderStyle.Hairline);
|
||||
// tables.get(j).getTableFormat().getBorders().getVertical().setLineWidth(0F);
|
||||
// tables.get(j).getTableFormat().getBorders().getHorizontal().setLineWidth(0F);
|
||||
// tables.get(j).getTableFormat().getBorders().getVertical().setColor(Color.white);
|
||||
|
||||
// tables.get(j).getRows().get(0).setHeight(99F);
|
||||
// tables.get(j).getRows().get(0).getRowFormat().getBorders().setBorderType(BorderStyle.Hairline);
|
||||
// RowCollection rows = tables.get(j).getRows();
|
||||
// for (int l = 0; l < rows.getCount(); l++) {
|
||||
// rows.get(l).setHeight(15F);
|
||||
// }
|
||||
|
||||
|
||||
CellCollection rowCollection = tables.get(j).getRows().get(0).getCells();
|
||||
for (int k = 0; k < rowCollection.getCount(); k++) {
|
||||
rowCollection.get(k).getCellFormat().getBorders().getBottom().setBorderType(BorderStyle.Hairline);
|
||||
rowCollection.get(k).getCellFormat().getBorders().getBottom().setLineWidth(15F);
|
||||
// rowCollection.get(k).getCellFormat().getBorders().getBottom().setColor(Color.red);
|
||||
}
|
||||
// tables.get(j).getRows().get(0).getCells().get(0).getCellFormat().getBorders().getBottom().setBorderType(BorderStyle.Hairline);
|
||||
tables.get(j).autoFit(AutoFitBehaviorType.Auto_Fit_To_Window);
|
||||
|
||||
// tables.get(j).getTableFormat().setHorizontalAlignment(RowAlignment.Right);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// for (int i=0 ; i < paragraphs.size() ; i++){
|
||||
// System.out.println("内容为"+paragraphs.get(i).getRuns());
|
||||
// List<XWPFRun> runs = paragraphs.get(i).getRuns();
|
||||
// String replace = String.valueOf(runs).replace(",", "").replace(" ","");
|
||||
// for (String pic: picNameList){
|
||||
// if (replace.contains(picMap.get(pic).replace(",","").replace(" ",""))){
|
||||
// System.out.println("包含执行了");
|
||||
// //获取光标
|
||||
// XmlCursor xmlCursor = paragraphs.get(i+1).getCTP().newCursor();
|
||||
// XWPFParagraph xwpfParagraph = newXdoc.insertNewParagraph(xmlCursor);
|
||||
//
|
||||
// XWPFRun run = xwpfParagraph.createRun();
|
||||
//// run.addBreak(BreakClear.ALL);
|
||||
//// run.setText("{{@image}}");
|
||||
// FileInputStream inputStream = new FileInputStream("C:\\Users\\yl\\Desktop\\pictures\\"+pic.replace("image",""));
|
||||
// run.addPicture2(inputStream,XWPFDocument.PICTURE_TYPE_JPEG,pic, Units.toEMU(500),Units.toEMU(180));
|
||||
// //
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// }
|
||||
// FileOutputStream os = new FileOutputStream(IMG_DIR+"New.doc");
|
||||
// newXdoc.write(os);
|
||||
doc.saveToFile(IMG_DIR+"New.doc");
|
||||
System.out.println("输出完毕");
|
||||
os.close();
|
||||
// os.close();
|
||||
fis.close();
|
||||
newFis.close();
|
||||
}catch (Exception e){
|
||||
|
||||
Reference in New Issue
Block a user