This commit is contained in:
wangjinlei
2025-10-17 11:16:31 +08:00
parent 840d6fda4c
commit df57b86db2
2 changed files with 320 additions and 13 deletions

View File

@@ -283,6 +283,27 @@ public class TypesetController {
return new ReturnValue(ReturnCodeAndMsgEnum.Success, al);
}
@PostMapping("/readDocxNormal")
public ReturnValue readDocxNormal(@RequestParam String fileRoute){
ArrayList<String> al = new ArrayList<>();
try{
InputStream inputStream = FileDownloadUtil.getInputStream(fileRoute);
XWPFDocument xdoc = new XWPFDocument(inputStream);
List<IBodyElement> bodyElements = xdoc.getBodyElements();
for (int i=0;i < bodyElements.size(); i++) {
IBodyElement element = bodyElements.get(i);
if (element instanceof XWPFParagraph) {
XWPFParagraph paragraph = (XWPFParagraph) element;
String text = paragraph.getText();
al.add(text);
}
}
}catch (Exception e){
e.printStackTrace();
}
return new ReturnValue(ReturnCodeAndMsgEnum.Success,al);
}
@PostMapping("/readDocx")
public ReturnValue readDocx(String fileRoute,Integer article_id){
ArrayList al = new ArrayList();
@@ -678,19 +699,23 @@ public class TypesetController {
map.put("type", typesetInfo.getInfo_type().toUpperCase());
map.put("journaltitle", typesetInfo.getJournal());
map.put("stage", typesetInfo.getStage());
Map<Integer, Map<String, String>> imageDatas = typesetInfo.getImages();
Map<Integer, Map<String, String>> tableDatas = typesetInfo.getTables();
Style doi_s = new Style();
doi_s.setColor("FF0000");
ParagraphRenderData doi_p = new ParagraphRenderData();
doi_p.addText(new TextRenderData("input doi", doi_s));
// doi_p.addText(new TextRenderData("input doi", doi_s));
doi_p.addText(new TextRenderData(typesetInfo.getDoi()));
map.put("doi", doi_p);
map.put("topic", typesetInfo.getTopic());
ParagraphRenderData peer_p = new ParagraphRenderData();
Style Italic_sty = new Style();
Italic_sty.setItalic(true);
peer_p.addText(new TextRenderData(typesetInfo.getJournal(), Italic_sty));
peer_p.addText(" thanks all ");
peer_p.addText(new TextRenderData("XXXXXXXXXXXXXXXX", doi_s));
peer_p.addText("anonymous reviewers for their contribution to the peer review of this paper.");
peer_p.addText(" thanks ");
// peer_p.addText(new TextRenderData("XXXXXXXXXXXXXXXX", doi_s));
peer_p.addText(typesetInfo.getAuthor_str());
peer_p.addText(" for their contribution to the peer review of this paper.");
map.put("peer", peer_p);
map.put("articletitle", typesetInfo.getInfo_title().replace("<b>", "").replace("</b>", "").replace("<i>", "").replace("</i>", ""));
String author = typesetInfo.getAuthor();
@@ -707,6 +732,7 @@ public class TypesetController {
String bu = cache_author[1];
Style ca_sty = new Style();
ca_sty.setVertAlign("superscript");
ca_sty.setColor("0070C0");
author_p.addText(new TextRenderData(bu.replace("<i>", "").replace("</i>", ""), ca_sty));
}
if (i < s_author.length - 1) {
@@ -714,7 +740,20 @@ public class TypesetController {
}
}
map.put("author", author_p);
map.put("authoraddress", typesetInfo.getAuthorAddress().replace("<b>", "").replace("</b>", "").replace("<i>", "").replace("</i>", ""));
ArrayList authorAddressList = new ArrayList();
for(Map<String,String> m:typesetInfo.getAuthorAddressList() ){
HashMap hashMap = new HashMap();
Style style = new Style();
style.setColor("0070C0");
TextRenderData textRenderData = new TextRenderData(m.get("k"), style);
hashMap.put("k",textRenderData);
hashMap.put("c",m.get("content"));
authorAddressList.add(hashMap);
}
map.put("authorAddressList",authorAddressList);
// 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());
@@ -742,8 +781,8 @@ public class TypesetController {
map.put("online_date", od_p);
map.put("ye", typesetInfo.getStage().substring(0, 4));
ParagraphRenderData abstract_p = new ParagraphRenderData();
abstract_p.addText(new TextRenderData("input abstract", doi_s));
map.put("abstract", abstract_p);
abstract_p.addText(new TextRenderData(typesetInfo.getAbstractText()));
map.put("abstract",abstract_p);
map.put("keywords", typesetInfo.getKeywords().replace("<b>", "").replace("</b>", ""));
//主体部分
@@ -753,8 +792,6 @@ public class TypesetController {
// ArrayList otherArr = new ArrayList();
Map<String,ArrayList<Map<String,MyParagraphRenderData>>> otherAll = new HashMap();
int otherNum=1;
Map<Integer, Map<String, String>> imageDatas = typesetInfo.getImages();
Map<Integer, Map<String, String>> tableDatas = typesetInfo.getTables();
for (String s : l) {
ParagraphStyle paragraphStyle = new ParagraphStyle();
paragraphStyle.setAlign(ParagraphAlignment.CENTER);
@@ -779,9 +816,11 @@ public class TypesetController {
}
Map<String, String> stringStringMap = imageDatas.get(image_id);
String s1 = stringStringMap.get("note");
MyParagraphRenderData myParagraphRenderData1 = crowStrNew(s1, tableDatas, imageDatas);
String s2 = stringStringMap.get("title");
MyParagraphRenderData myParagraphRenderData1 = crowStrNew(s2+s1, tableDatas, imageDatas);
Map cache1 = new HashMap();
myParagraphRenderData1.setParagraphStyle(paragraphStyle);
// myParagraphRenderData1.setParagraphStyle(paragraphStyle);
if(checkImg(s)){
cache1.put("data",myParagraphRenderData1);
otherAllList.add(cache1);
@@ -971,6 +1010,226 @@ public class TypesetController {
return new ReturnValue(ReturnCodeAndMsgEnum.Success, frag);
}
@PostMapping("/createDocxWithOut")
public ReturnValue createDocxWithOut(@RequestBody TypesetInfo typesetInfo) throws Exception{
Map frag = new HashMap();
ZipSecureFile.setMinInflateRatio(-1.0d);
HashMap<String, Object> map = new HashMap<>();
//处理基础信息部分
map.put("type", typesetInfo.getInfo_type().toUpperCase());
map.put("journaltitle", typesetInfo.getJournal());
map.put("stage", typesetInfo.getStage());
Style doi_s = new Style();
doi_s.setColor("FF0000");
ParagraphRenderData doi_p = new ParagraphRenderData();
doi_p.addText(new TextRenderData(typesetInfo.getDoi(), doi_s));
map.put("doi", doi_p);
map.put("topic", typesetInfo.getTopic());
ParagraphRenderData peer_p = new ParagraphRenderData();
Style Italic_sty = new Style();
Italic_sty.setItalic(true);
peer_p.addText(new TextRenderData(typesetInfo.getJournal(), Italic_sty));
peer_p.addText(" thanks all ");
peer_p.addText(new TextRenderData("XXXXXXXXXXXXXXXX", doi_s));
peer_p.addText("anonymous reviewers for their contribution to the peer review of this paper.");
map.put("peer", peer_p);
map.put("articletitle", typesetInfo.getInfo_title().replace("<b>", "").replace("</b>", "").replace("<i>", "").replace("</i>", ""));
String author = typesetInfo.getAuthor();
String[] s_author = author.split("</q>");
ParagraphRenderData author_p = new ParagraphRenderData();
for (int i = 0; i < s_author.length; i++) {
if (s_author[i].equals("")) {
continue;
}
String[] cache_author = s_author[i].split("<q>");
String au = cache_author[0];
author_p.addText(au);
if (cache_author.length > 1) {
String bu = cache_author[1];
Style ca_sty = new Style();
ca_sty.setVertAlign("superscript");
ca_sty.setColor("0070C0");
author_p.addText(new TextRenderData(bu.replace("<i>", "").replace("</i>", ""), ca_sty));
}
if (i < s_author.length - 1) {
author_p.addText(", ");
}
}
map.put("author", author_p);
ArrayList authorAddressList = new ArrayList();
for(Map<String,String> m:typesetInfo.getAuthorAddressList() ){
HashMap hashMap = new HashMap();
Style style = new Style();
style.setColor("0070C0");
TextRenderData textRenderData = new TextRenderData(m.get("k"), style);
hashMap.put("k",textRenderData);
hashMap.put("c",m.get("content"));
authorAddressList.add(hashMap);
}
map.put("authorAddressList",authorAddressList);
// 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: 10.53388/" + typesetInfo.getDoi() + ".").replace("<b>", "").replace("</b>", "").replace("<i>", "").replace("</i>", ""));
map.put("exeditor", typesetInfo.getUserAccount());
map.put("received_date", typesetInfo.getReceived_date());
map.put("accept_date", typesetInfo.getAccepted_date());
ParagraphRenderData od_p = new ParagraphRenderData();
od_p.addText(new TextRenderData("input online_date", doi_s));
map.put("online_date", od_p);
map.put("ye", typesetInfo.getStage().substring(0, 4));
ParagraphRenderData abstract_p = new ParagraphRenderData();
abstract_p.addText(new TextRenderData("input abstract", doi_s));
map.put("abstract", typesetInfo.getAbstractText());
map.put("keywords", typesetInfo.getKeywords().replace("<b>", "").replace("</b>", ""));
//主体部分
String main = typesetInfo.getMainText();
List<String> l = JSON.parseArray(main, String.class);
ArrayList mainArr = new ArrayList();
// ArrayList otherArr = new ArrayList();
Map<String,ArrayList<Map<String,MyParagraphRenderData>>> otherAll = new HashMap();
int otherNum=1;
// Map<Integer, Map<String, String>> imageDatas = typesetInfo.getImages();
// Map<Integer, Map<String, String>> tableDatas = typesetInfo.getTables();
for (String s : l) {
// ParagraphStyle paragraphStyle = new ParagraphStyle();
// paragraphStyle.setAlign(ParagraphAlignment.CENTER);
// paragraphStyle.setSpacingAfterLines(Double.valueOf(1));
Map cache = new HashMap();
MyParagraphRenderData capd = crowStrNew(s,typesetInfo.getTables(),typesetInfo.getImages());
cache.put("content", capd);
mainArr.add(cache);
}
map.put("main", mainArr);
//参考文献部分
JSONArray ja = (JSONArray) JSON.parse(typesetInfo.getRefers());
List<ParagraphRenderData> refsl = new ArrayList<ParagraphRenderData>();
for (Object tif : ja) {
JSONObject a = (JSONObject) tif;
if (a.getString("refer_type").equals("journal")) {
ParagraphRenderData chc_pdd = new ParagraphRenderData();
Style chch = new Style();
chch.setFontFamily("Charis SIL");
chch.setFontSize(7.5);
Style cheche = new Style();
cheche.setItalic(true);
cheche.setFontFamily("Charis SIL");
cheche.setFontSize(7.5);
if (a.getString("is_ja").equals("0")) {
cheche.setColor("f98f36");
}
Style lstitle = new Style();
lstitle.setFontFamily("Charis SIL");
lstitle.setFontSize(7.5);
lstitle.setColor("0082AA");
chc_pdd.addText(new TextRenderData(a.getString("author") + " ", chch));
chc_pdd.addText(new TextRenderData(a.getString("title") + ". ", chch));
chc_pdd.addText(new TextRenderData(a.getString("joura") + ". ", cheche));
chc_pdd.addText(new TextRenderData(a.getString("dateno") + ". Available at:", chch));
chc_pdd.addText("\n");
chc_pdd.addText(new TextRenderData(a.getString("doilink"), lstitle));
refsl.add(chc_pdd);
}else if(a.getString("refer_type").equals("book")){
ParagraphRenderData chc_pd = new ParagraphRenderData();
Style chch = new Style();
chch.setFontFamily("Charis SIL");
chch.setFontSize(7.5);
chch.setColor("f98f36");
Style cheche = new Style();
cheche.setItalic(true);
cheche.setFontFamily("Charis SIL");
cheche.setFontSize(7.5);
chc_pd.addText(new TextRenderData(a.getString("author") + " ", chch));
chc_pd.addText(new TextRenderData(a.getString("title") + ". ", chch));
chc_pd.addText(new TextRenderData(a.getString("dateno") + ". ", chch));
chc_pd.addText(new TextRenderData(a.getString("isbn"), chch));
refsl.add(chc_pd);
}else {
ParagraphRenderData chc_pd = new ParagraphRenderData();
String tgh_title = a.getString("refer_frag").replace("<b>", "").replace("</b>", "").replace("<i>", "").replace("</i>", "");
String[] split = tgh_title.split("\n");
Style chch = new Style();
chch.setFontFamily("Charis SIL");
chch.setFontSize(7.5);
chch.setColor("f98f36");
chc_pd.addText(new TextRenderData(split[0], chch));
if (split.length > 1) {
chc_pd.addText("\n");
Style lstitle = new Style();
lstitle.setFontFamily("Charis SIL");
lstitle.setFontSize(7.5);
lstitle.setColor("0082AA");
chc_pd.addText(new TextRenderData(split[1], lstitle));
}
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";
myTemplate = "template1_1.docx";
}
Configure config = Configure.builder().bind("content", new MyContentPolicy()).bind("data",new MyContentPolicy())
.bind("abstract", new ParagraphRenderPolicy()).bind("author", new ParagraphRenderPolicy())
.bind("doi", new ParagraphRenderPolicy()).bind("online_date", new ParagraphRenderPolicy())
.bind("peer", new ParagraphRenderPolicy())
.build();
XWPFTemplate template = XWPFTemplate.compile(this.BASE_DIR + myTemplate, config).render(map);
// XWPFDocument document = template.getXWPFDocument();
// List<XWPFTable> tables = document.getTables();
// for (int i = 1; i < tables.size(); i++) { // 从索引 1 开始,跳过第一个表格
// XWPFTable table = tables.get(i);
// setThreeLineTableStyle(table); // 应用三线表样式
// }
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";
template.writeAndClose(new FileOutputStream(dirpath + "/" + goFileName));
//二次写入
frag.put("file", goFileName);
} catch (IOException e) {
e.printStackTrace();
}
return new ReturnValue();
}
@PostMapping("/crossImage")
@@ -1425,6 +1684,13 @@ public class TypesetController {
cellRenderData.setParagraphs(en.getValue());
rowRenderData.addCell(cellRenderData);
}
if(entry.getKey()%2==1){
RowStyle rowStyle = new RowStyle();
CellStyle cellStyle = new CellStyle();
cellStyle.setBackgroundColor("FAE7E8");
rowStyle.setDefaultCellStyle(cellStyle);
rowRenderData.setRowStyle(rowStyle);
}
tableRenderData.addRow(rowRenderData);
}
@@ -1459,6 +1725,13 @@ public class TypesetController {
}else{
textRenderCreate(s,p);
}
if(s.contains("<tr>")) {
ParagraphStyle paragraphStyle = new ParagraphStyle();
paragraphStyle.setAlign(ParagraphAlignment.CENTER);
paragraphStyle.setBackgroundColor("DEEBF7");
paragraphStyle.setSpacing(1.1);
p.setParagraphStyle(paragraphStyle);
}
return p;
}
@@ -1636,7 +1909,7 @@ public class TypesetController {
return copy;
}
private String determineNextTag(String s) {
Pattern pattern = Pattern.compile("<(sup|sub|blue|b|i)>"); // 只匹配开放标签
Pattern pattern = Pattern.compile("<(sup|sub|blue|b|i|tr|t|r)>"); // 只匹配开放标签
Matcher matcher = pattern.matcher(s);
if (matcher.find()) {
return matcher.group(1); // 返回第一个匹配的标签
@@ -1651,7 +1924,6 @@ public class TypesetController {
break;
case "blue":
s.setColor("0082AA");
break;
case "sub":
s.setVertAlign("subscript");
@@ -1659,6 +1931,17 @@ public class TypesetController {
case "b":
s.setBold(true);
break;
case "tr":
s.setColor("D25A5A");
s.setFontSize(9.0);
s.setBold(true);
break;
case "t":
s.setColor("D25A5A");
break;
case "r":
s.setColor("D25A5A");
break;
case "i":
s.setItalic(true);
break;

View File

@@ -71,6 +71,10 @@ public class TypesetInfo implements Serializable {
private String userAccount;
private String author_str;
private List<Map<String,String>> authorAddressList;
private Map<Integer,Map<String,String>> images=new LinkedHashMap<>();;
@@ -210,6 +214,14 @@ public class TypesetInfo implements Serializable {
return images;
}
public String getAuthor_str() {
return author_str;
}
public void setAuthor_str(String author_str) {
this.author_str = author_str;
}
@JsonSetter
public void setImages(Map<Integer, Map<String, String>> images) {
if (images instanceof List) {
@@ -236,6 +248,15 @@ public class TypesetInfo implements Serializable {
}
}
public List<Map<String, String>> getAuthorAddressList() {
return authorAddressList;
}
public void setAuthorAddressList(List<Map<String, String>> authorAddressList) {
this.authorAddressList = authorAddressList;
}
public void setStage(String stage) {
this.stage = stage;
}
@@ -372,6 +393,9 @@ public class TypesetInfo implements Serializable {
", refers='" + refers + '\'' +
", info_state=" + info_state +
", userAccount='" + userAccount + '\'' +
", authorAddressList=" + authorAddressList +
", images=" + images +
", tables=" + tables +
'}';
}
}