getDoc网上专用接口

This commit is contained in:
Sakura9701
2022-06-13 16:31:20 +08:00
parent b32f5dbdaa
commit 0453abe2a5

View File

@@ -41,9 +41,11 @@ import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
@@ -475,7 +477,7 @@ public class TypesetController {
@ApiOperation(value = "获取生成的docx")
@PostMapping("/getDocx")
public ReturnValue getDocx(Long typesetId) throws IOException {
public ReturnValue getDocx(Long typesetId,@RequestParam("filename") String filename) throws IOException {
Map m = typesetservice.getTypesetDetail(typesetId);
Map frag = new HashMap();
Typeset t = (Typeset) m.get("info");
@@ -534,20 +536,98 @@ public class TypesetController {
// map.put("abstract",ti.getAbstractText().replace("<b>","").replace("</b>",""));
map.put("abstract", this.crowStr(ti.getAbstractText()));
map.put("keywords", ti.getKeywords().replace("<b>", "").replace("</b>", ""));
map.put("img", this.BASE_DIR + "p1.png");
// map.put("img", this.BASE_DIR + "p1.png");
ArrayList<Object> arrayList = new ArrayList<>();
//读取原文件表格数据
Document doc = new Document();
doc.loadFromFile(this.BASE_DIR+filename);
SectionCollection sections = doc.getSections();
//保存文本
String output = "C:\\Users\\Administrator\\Desktop\\typesetcode\\a.txt";
File file = new File(output);
if (!file.exists()) {
file.delete();
}
file.createNewFile();
FileWriter fw = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(fw);
for (int i = 0; i <sections.getCount() ; i++) {
TableCollection tables = sections.get(i).getTables();
for (int j = 0; j <tables.getCount() ; j++) {
Table table = tables.get(j);
//创建String List
table.applyStyle(DefaultTableStyle.Table_Classic_1);
List images = new ArrayList();
//遍历表格中的行
for (int l = 0; l < table.getRows().getCount(); l++)
{
ArrayList prList = new ArrayList();
TableRow row = table.getRows().get(l);
//遍历每行中的单元格
for (int k = 0; k < row.getCells().getCount(); k++)
{
TableCell cell = row.getCells().get(k);
//遍历单元格中的段落
for (int n = 0; n < cell.getParagraphs().getCount(); n++)
{
Paragraph paragraph = cell.getParagraphs().get(n);
prList.add(paragraph.getText());
// bw.write(paragraph.getText() + "\t");//获取文本内容
System.out.println("表格数据"+paragraph.getText());
//遍历段落中的所有子对象
for (int x = 0; x < paragraph.getChildObjects().getCount(); x++)
{
Object object = paragraph.getChildObjects().get(x);
//判定对象是否为图片
if (object instanceof DocPicture)
{
//获取图片
DocPicture picture = (DocPicture) object;
images.add(picture.getImage());
}
}
}
}
arrayList.add(prList);
// bw.write("\r\n");//写入内容到txt文件
}
System.out.println("List=====================>"+arrayList);
}
}
bw.flush();
bw.close();
fw.close();
//
List<Map> list = new ArrayList<>();
LinkedHashMap<Object, Object> params = new LinkedHashMap<>();
params.put("name", "姓名");
params.put("age", "年龄");
params.put("sex", "性别");
list.add(params);
for (int i = 0; i < 4; i++) {
HashMap<Object, Object> hashMap = new LinkedHashMap<>();
hashMap.put("name","小李"+ i);
hashMap.put("age", 23 + i);
hashMap.put("sex", "");
list.add(hashMap);
for (int i = 0; i < arrayList.size(); i++){
List o = (List) arrayList.get(i);
if (i == 0) {
for (int j = 0; j < o.size(); j++) {
params.put(j,o.get(j));
}
list.add(params);
}else {
LinkedHashMap<Object, Object> hashMap = new LinkedHashMap<>();
for (int j = 0; j < o.size(); j++) {
String o1 = (String) o.get(j);
hashMap.put(j, o1);
}
list.add(hashMap);
}
}
System.out.println("list===============>"+list);
map.put("table", list);
Configure config = Configure.builder().bind("content", new ParagraphRenderPolicy()).
@@ -645,6 +725,229 @@ public class TypesetController {
return new ReturnValue(ReturnCodeAndMsgEnum.Success, frag);
}
@ApiOperation(value = "网上专用获取生成的docx")
@PostMapping("/webGetDocx")
public ReturnValue webGetDocx(@RequestBody TypesetInfo typesetInfo,@RequestParam("filename") String filename) throws IOException {
Long typesetId = typesetInfo.getTypesetId();
Map m = typesetservice.getTypesetDetail(typesetId);
Map frag = new HashMap();
Typeset t = (Typeset) m.get("info");
// TypesetInfo typesetInfo = t.getU_typeset_info();
List<TypesetInfoReference> refList = (List<TypesetInfoReference>) m.get("refers");
ZipSecureFile.setMinInflateRatio(-1.0d);
User user = userservice.getUser(t.getUserId());
HashMap<String, Object> map = new HashMap<>();
map.put("type", typesetInfo.getInfo_type().toUpperCase());
map.put("journaltitle", typesetInfo.getJournal());
map.put("stage", typesetInfo.getStage());
map.put("doi", typesetInfo.getDoi());
map.put("topic", typesetInfo.getTopic());
map.put("articletitle", typesetInfo.getInfo_title().replace("<b>", "").replace("</b>", "").replace("<i>", "").replace("</i>", ""));
map.put("author", typesetInfo.getAuthor().replace("<b>", "").replace("</b>", "").replace("<i>", "").replace("</i>", ""));
map.put("authoraddress", typesetInfo.getAuthorAddress().replace("<b>", "").replace("</b>", "").replace("<i>", "").replace("</i>", ""));
map.put("corresponding", typesetInfo.getAuthorCorresponding().replace("<b>", "").replace("</b>", "").replace("<i>", "").replace("</i>", ""));
map.put("coremail", typesetInfo.getAuthorCorrespondingEmail().replace("<b>", "").replace("</b>", "").replace("<i>", "").replace("</i>", ""));
map.put("website", typesetInfo.getWebsite());
if (typesetInfo.getAuthorContribution().equals("")) {
map.put("contributions", false);
} else {
HashMap<String, Object> map1 = new HashMap<>();
map1.put("author_contribution", typesetInfo.getAuthorContribution());
map.put("contributions", map1);
}
if (typesetInfo.getAcknowledgment().equals("")) {
map.put("acknowledgment", false);
} else {
HashMap map2 = new HashMap();
map2.put("acknowledgment_content", typesetInfo.getAcknowledgment());
map.put("acknowledgment", map2);
}
map.put("abbreviation", typesetInfo.getAbbreviation());
map.put("citation", (typesetInfo.getLittle_author() + ". " + typesetInfo.getInfo_title() + ". " + typesetInfo.getJabbr() + ". " + typesetInfo.getStage() + ". doi: " + typesetInfo.getDoi() + ".").replace("<b>", "").replace("</b>", "").replace("<i>", "").replace("</i>", ""));
map.put("exeditor", user.getAccount());
SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy", Locale.ENGLISH);
if (typesetInfo.getReceived_date().toString().equals("")) {
map.put("received_date", typesetInfo.getReceived_date());
} else {
map.put("received_date", sdf.format(new Date(Long.valueOf(typesetInfo.getReceived_date()))));
}
// map.put("received_date",sdf.format(new Date(Long.valueOf(typesetInfo.getReceived_date()))));
if (typesetInfo.getAccepted_date().toString().equals("")) {
map.put("accept_date", typesetInfo.getAccepted_date());
} else {
map.put("accept_date", sdf.format(new Date(Long.valueOf(typesetInfo.getAccepted_date()))));
}
// map.put("accept_date",sdf.format(new Date(Long.valueOf(typesetInfo.getAccepted_date()))));
if (typesetInfo.getOnline_date().toString().equals("")) {
map.put("online_date", typesetInfo.getOnline_date());
} else {
map.put("online_date", sdf.format(new Date(Long.valueOf(typesetInfo.getOnline_date()))));
}
map.put("ye", typesetInfo.getStage().substring(0, 4));
// map.put("abstract",typesetInfo.getAbstractText().replace("<b>","").replace("</b>",""));
map.put("abstract", this.crowStr(typesetInfo.getAbstractText()));
map.put("keywords", typesetInfo.getKeywords().replace("<b>", "").replace("</b>", ""));
// map.put("img", this.BASE_DIR + "p1.png");
ArrayList<Object> arrayList = new ArrayList<>();
//读取原文件表格数据
Document doc = new Document();
doc.loadFromFile(this.BASE_DIR+filename);
SectionCollection sections = doc.getSections();
//保存文本
String output = "C:\\Users\\Administrator\\Desktop\\typesetcode\\a.txt";
File file = new File(output);
if (!file.exists()) {
file.delete();
}
file.createNewFile();
FileWriter fw = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(fw);
for (int i = 0; i <sections.getCount() ; i++) {
TableCollection tables = sections.get(i).getTables();
for (int j = 0; j <tables.getCount() ; j++) {
Table table = tables.get(j);
//创建String List
table.applyStyle(DefaultTableStyle.Table_Classic_1);
List images = new ArrayList();
//遍历表格中的行
for (int l = 0; l < table.getRows().getCount(); l++)
{
ArrayList prList = new ArrayList();
TableRow row = table.getRows().get(l);
//遍历每行中的单元格
for (int k = 0; k < row.getCells().getCount(); k++)
{
TableCell cell = row.getCells().get(k);
//遍历单元格中的段落
for (int n = 0; n < cell.getParagraphs().getCount(); n++)
{
Paragraph paragraph = cell.getParagraphs().get(n);
prList.add(paragraph.getText());
// bw.write(paragraph.getText() + "\t");//获取文本内容
System.out.println("表格数据"+paragraph.getText());
//遍历段落中的所有子对象
for (int x = 0; x < paragraph.getChildObjects().getCount(); x++)
{
Object object = paragraph.getChildObjects().get(x);
//判定对象是否为图片
if (object instanceof DocPicture)
{
//获取图片
DocPicture picture = (DocPicture) object;
images.add(picture.getImage());
}
}
}
}
arrayList.add(prList);
// bw.write("\r\n");//写入内容到txt文件
}
System.out.println("List=====================>"+arrayList);
}
}
bw.flush();
bw.close();
fw.close();
//
List<Map> list = new ArrayList<>();
LinkedHashMap<Object, Object> params = new LinkedHashMap<>();
for (int i = 0; i < arrayList.size(); i++){
List o = (List) arrayList.get(i);
if (i == 0) {
for (int j = 0; j < o.size(); j++) {
params.put(j,o.get(j));
}
list.add(params);
}else {
LinkedHashMap<Object, Object> hashMap = new LinkedHashMap<>();
for (int j = 0; j < o.size(); j++) {
String o1 = (String) o.get(j);
hashMap.put(j, o1);
}
list.add(hashMap);
}
}
System.out.println("list===============>"+list);
map.put("table", list);
Configure config = Configure.builder().bind("content", new ParagraphRenderPolicy()).
bind("abstract", new ParagraphRenderPolicy()).bind("table", new CustomTableRenderPolicy()).
build();
String main = typesetInfo.getMainText();
List<String> l = JSON.parseArray(main, String.class);
ArrayList mainArr = new ArrayList();
for (String s : l) {
Map cache = new HashMap();
ParagraphRenderData capd = this.crowStr(s);
cache.put("content", capd);
mainArr.add(cache);
}
map.put("main", mainArr);
//测试
List<ParagraphRenderData> refsl = new ArrayList<ParagraphRenderData>();
for (TypesetInfoReference tif : refList) {
String tttt = tif.getRefer_title().replace("<b>", "").replace("</b>", "").replace("<i>", "").replace("</i>", "").trim().toUpperCase();
if (tttt.equals("REFERENCE") || tttt.equals("REFERENCES")) {
continue;
}
ParagraphRenderData chc_pd = new ParagraphRenderData();
Style chch = new Style();
chch.setFontFamily("Charis SIL");
chch.setFontSize(7.5);
String tgh_title = tif.getRefer_title().replace("<b>", "").replace("</b>", "").replace("<i>", "").replace("</i>", "");
chc_pd.addText(new TextRenderData(tgh_title, chch));
refsl.add(chc_pd);
}
map.put("refs", new NumberingRenderData(NumberingFormat.DECIMAL, refsl));
String myTemplate = "";
//确定使用的模板
if (typesetInfo.getInfo_type().toUpperCase().equals("COMMENT") || typesetInfo.getInfo_type().toUpperCase().equals("NEWS")) {
myTemplate = "template2.docx";
} else {
myTemplate = "template1.docx";
}
XWPFTemplate template = XWPFTemplate.compile(this.BASE_DIR + myTemplate, config).render(map);
try {
Date date = new Date();
String dirpath = this.BASE_DIR + new SimpleDateFormat("yyyyMMdd").format(date);
//如果不存在,创建文件夹
File f = new File(dirpath);
if (!f.exists()) {
f.mkdirs();
}
String goFileName = System.currentTimeMillis() + ".docx";
frag.put("file", new SimpleDateFormat("yyyyMMdd").format(date) + "/" + goFileName);
template.writeAndClose(new FileOutputStream(dirpath + "/" + goFileName));
} catch (IOException e) {
e.printStackTrace();
}
return new ReturnValue(ReturnCodeAndMsgEnum.Success, frag);
}
private ParagraphRenderData crowStr(String s) {
ParagraphRenderData p = new ParagraphRenderData();
@@ -742,7 +1045,7 @@ public class TypesetController {
}
@ApiOperation(value = "生成图片")
@RequestMapping("/addPic")
@PostMapping("/addPic")
public ReturnValue addPic(String OldFileName,String newFileName){
//1.操作初始文档获取图片 和 图片所在的位置
try {
@@ -758,6 +1061,8 @@ public class TypesetController {
//准备一个Map key为图片名称值为位置
HashMap<String, String> picMap = new HashMap<>();
String randomFix = String.valueOf(System.currentTimeMillis()) + "\\";
//获取图片所在位置保存
for(int i = 0;i < list.size();i++){
List<String> imageBundleList = XWPFUtils.readImageInParagraph(list.get(i));
@@ -787,7 +1092,7 @@ public class TypesetController {
//遍历照片
// for (XWPFPictureData picture: allPictures) {
byte[] data = pictureData.getData();
File filePicture = new File(IMG_DIR + imageName.replace("image",""));
File filePicture = new File(IMG_DIR + randomFix + imageName.replace("image",""));
if (!filePicture.exists()){
if (filePicture.getParentFile().exists()) {
filePicture.getParentFile().mkdirs();
@@ -829,7 +1134,13 @@ public class TypesetController {
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 imgPath = IMG_DIR + randomFix +pic.replace("image", "");
File picture=new File(imgPath);
BufferedImage sourceImg= ImageIO.read(new FileInputStream(picture));
int width = sourceImg.getWidth();
sourceImg.getHeight();
//判断图片中是否包含 文字
// String valCode = new OCRUtil().recognizeText(new File(imgPath));
// System.out.println("valCode==============>"+valCode);
@@ -847,10 +1158,10 @@ public class TypesetController {
// docPicture.setTextWrappingType(TextWrappingType.Both);
// }else {
//2.不包含 判断图片大小决定 图片为单栏还是双栏
if (pic.replace("image","").equals("1.png")){
if (width <= 220f){
//图片为小图 双栏
DocPicture docPicture = paragraph.appendPicture(imgPath);
docPicture.setWidth(200f);
docPicture.setWidth(220f);
docPicture.setHeight(180f);
docPicture.setTextWrappingStyle(TextWrappingStyle.Top_And_Bottom);
docPicture.setTextWrappingType(TextWrappingType.Both);
@@ -891,7 +1202,7 @@ public class TypesetController {
//
//设置表格的顶部边框
tables.get(j).getTableFormat().getBorders().getTop().setBorderType(BorderStyle.Hairline);
tables.get(j).getTableFormat().getBorders().getTop().setLineWidth(2.25F);
tables.get(j).getTableFormat().getBorders().getTop().setLineWidth(0.2F);
tables.get(j).getTableFormat().getBorders().getTop().setColor(Color.black);
//
// //设置表格的左边框
@@ -902,7 +1213,7 @@ public class TypesetController {
//设置表格的底部边框
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().getBottom().setLineWidth(0.2F);
//设置表格的水平和垂直边框
// tables.get(j).getTableFormat().getBorders().getVertical().setBorderType(BorderStyle.Hairline);
// tables.get(j).getTableFormat().getBorders().getHorizontal().setBorderType(BorderStyle.Hairline);
@@ -921,7 +1232,7 @@ public class TypesetController {
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().setLineWidth(0.2F);
// rowCollection.get(k).getCellFormat().getBorders().getBottom().setColor(Color.red);
}
// tables.get(j).getRows().get(0).getCells().get(0).getCellFormat().getBorders().getBottom().setBorderType(BorderStyle.Hairline);
@@ -932,8 +1243,7 @@ public class TypesetController {
}
String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
// for (int i=0 ; i < paragraphs.size() ; i++){
// System.out.println("内容为"+paragraphs.get(i).getRuns());
@@ -958,7 +1268,7 @@ public class TypesetController {
// }
// FileOutputStream os = new FileOutputStream(IMG_DIR+"New.doc");
// newXdoc.write(os);
doc.saveToFile(IMG_DIR+"New.doc");
doc.saveToFile(IMG_DIR + randomFix + uuid+".doc");
System.out.println("输出完毕");
// os.close();
fis.close();