diff --git a/src/main/java/com/example/ts_obj/controller/TypesetController.java b/src/main/java/com/example/ts_obj/controller/TypesetController.java index fc686d4..ed8671c 100644 --- a/src/main/java/com/example/ts_obj/controller/TypesetController.java +++ b/src/main/java/com/example/ts_obj/controller/TypesetController.java @@ -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("","").replace("","")); map.put("abstract", this.crowStr(ti.getAbstractText())); map.put("keywords", ti.getKeywords().replace("", "").replace("", "")); - map.put("img", this.BASE_DIR + "p1.png"); +// map.put("img", this.BASE_DIR + "p1.png"); + + ArrayList 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 "+arrayList); + } + } + bw.flush(); + bw.close(); + fw.close(); + + // List list = new ArrayList<>(); LinkedHashMap params = new LinkedHashMap<>(); - params.put("name", "姓名"); - params.put("age", "年龄"); - params.put("sex", "性别"); - list.add(params); - for (int i = 0; i < 4; i++) { - HashMap 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 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 refList = (List) m.get("refers"); + ZipSecureFile.setMinInflateRatio(-1.0d); + User user = userservice.getUser(t.getUserId()); + HashMap 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("", "").replace("", "").replace("", "").replace("", "")); + map.put("author", typesetInfo.getAuthor().replace("", "").replace("", "").replace("", "").replace("", "")); + map.put("authoraddress", typesetInfo.getAuthorAddress().replace("", "").replace("", "").replace("", "").replace("", "")); + map.put("corresponding", typesetInfo.getAuthorCorresponding().replace("", "").replace("", "").replace("", "").replace("", "")); + map.put("coremail", typesetInfo.getAuthorCorrespondingEmail().replace("", "").replace("", "").replace("", "").replace("", "")); + map.put("website", typesetInfo.getWebsite()); + if (typesetInfo.getAuthorContribution().equals("")) { + map.put("contributions", false); + } else { + HashMap 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("", "").replace("", "").replace("", "").replace("", "")); + 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("","").replace("","")); + map.put("abstract", this.crowStr(typesetInfo.getAbstractText())); + map.put("keywords", typesetInfo.getKeywords().replace("", "").replace("", "")); +// map.put("img", this.BASE_DIR + "p1.png"); + + + ArrayList 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 "+arrayList); + } + } + bw.flush(); + bw.close(); + fw.close(); + + // + List list = new ArrayList<>(); + LinkedHashMap 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 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 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 refsl = new ArrayList(); + for (TypesetInfoReference tif : refList) { + String tttt = tif.getRefer_title().replace("", "").replace("", "").replace("", "").replace("", "").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("", "").replace("", "").replace("", "").replace("", ""); + 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 picMap = new HashMap<>(); + String randomFix = String.valueOf(System.currentTimeMillis()) + "\\"; + //获取图片所在位置保存 for(int i = 0;i < list.size();i++){ List 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();