getDoc网上专用接口修改
This commit is contained in:
@@ -2,7 +2,6 @@ package com.example.ts_obj.Util;
|
||||
|
||||
|
||||
import com.deepoove.poi.data.*;
|
||||
import com.deepoove.poi.data.style.*;
|
||||
import com.deepoove.poi.policy.AbstractRenderPolicy;
|
||||
import com.deepoove.poi.policy.TableRenderPolicy;
|
||||
import com.deepoove.poi.render.RenderContext;
|
||||
@@ -29,36 +28,45 @@ public class CustomTableRenderPolicy extends AbstractRenderPolicy<List<Map<Strin
|
||||
BodyContainer bodyContainer = BodyContainerFactory.getBodyContainer(run);
|
||||
|
||||
List<Map<String, Object>> list = context.getThing();
|
||||
HashMap<String, Object> info = (HashMap<String, Object>) list.get(0);
|
||||
// 定义行列
|
||||
int row = list.size(), col = info.size();
|
||||
// 插入表格
|
||||
XWPFTable table = bodyContainer.insertNewTable(run, row, col);
|
||||
if (list != null) {
|
||||
HashMap<String, Object> info = (HashMap<String, Object>) list.get(0);
|
||||
if (info != null) {
|
||||
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
Iterator<Map.Entry<String, Object>> it = list.get(i).entrySet().iterator();
|
||||
|
||||
List<String> cells = new ArrayList<>();
|
||||
while (it.hasNext()) {
|
||||
Map.Entry<String, Object> entry = it.next();
|
||||
cells.add(entry.getValue().toString());
|
||||
}
|
||||
String[] s = new String[cells.size()];
|
||||
cells.toArray(s);
|
||||
//创建一行数据
|
||||
RowRenderData data = Rows.create(s);
|
||||
XWPFTableRow tableRow = table.getRow(i);
|
||||
//表格边框
|
||||
table.setInsideHBorder(XWPFTable.XWPFBorderType.NONE, 1, 0, "");
|
||||
table.setInsideVBorder(XWPFTable.XWPFBorderType.NONE, 1, 0, "");
|
||||
table.setLeftBorder(XWPFTable.XWPFBorderType.NONE, 1, 0, "");
|
||||
table.setRightBorder(XWPFTable.XWPFBorderType.NONE, 1, 0, "");
|
||||
// 渲染一行
|
||||
TableRenderPolicy.Helper.renderRow(tableRow, data);
|
||||
}
|
||||
// 表格宽度
|
||||
TableTools.setWidth(table, UnitUtils.cm2Twips(14.63f) + "", null);
|
||||
// 边框和样式
|
||||
// 定义行列
|
||||
int row = list.size(), col = info.size();
|
||||
// 插入表格
|
||||
XWPFTable table = bodyContainer.insertNewTable(run, row, col);
|
||||
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
Iterator<Map.Entry<String, Object>> it = list.get(i).entrySet().iterator();
|
||||
|
||||
List<String> cells = new ArrayList<>();
|
||||
while (it.hasNext()) {
|
||||
Map.Entry<String, Object> entry = it.next();
|
||||
cells.add(entry.getValue().toString());
|
||||
}
|
||||
String[] s = new String[cells.size()];
|
||||
cells.toArray(s);
|
||||
//创建一行数据
|
||||
RowRenderData data = Rows.create(s);
|
||||
XWPFTableRow tableRow = table.getRow(i);
|
||||
//表格边框
|
||||
table.setInsideHBorder(XWPFTable.XWPFBorderType.NONE, 1, 0, "");
|
||||
table.setInsideVBorder(XWPFTable.XWPFBorderType.NONE, 1, 0, "");
|
||||
table.setLeftBorder(XWPFTable.XWPFBorderType.NONE, 1, 0, "");
|
||||
table.setRightBorder(XWPFTable.XWPFBorderType.NONE, 1, 0, "");
|
||||
// 渲染一行
|
||||
TableRenderPolicy.Helper.renderRow(tableRow, data);
|
||||
}
|
||||
// 表格宽度
|
||||
TableTools.setWidth(table, UnitUtils.cm2Twips(14.63f) + "", null);
|
||||
// 边框和样式
|
||||
// TableTools.borderTable(table, BorderStyle.builder().withType(XWPFTable.XWPFBorderType.NIL).build());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.example.ts_obj.controller;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.example.ts_obj.Util.*;
|
||||
import com.spire.doc.*;
|
||||
import com.spire.doc.Document;
|
||||
@@ -62,12 +61,11 @@ public class TypesetController {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(UserController.class);
|
||||
|
||||
private String BASE_DIR="E:\\";
|
||||
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/";
|
||||
private String Man_base_url = "/home/wwwroot/";
|
||||
@Autowired
|
||||
private TypesetService typesetservice;
|
||||
|
||||
@@ -205,16 +203,156 @@ public class TypesetController {
|
||||
@ApiOperation(value = "读取文件")
|
||||
@PostMapping("readdoc")
|
||||
public ReturnValue myDocReader(String filename) {
|
||||
|
||||
// String filename = "300641ce-cb9b-4a41-b5d9-de7028e46b171639634304222.docx";
|
||||
|
||||
ArrayList al = new ArrayList();
|
||||
try {
|
||||
File file = new File(this.BASE_DIR + filename);
|
||||
FileInputStream fis = new FileInputStream(file);
|
||||
XWPFDocument xdoc = new XWPFDocument(fis);
|
||||
List<XWPFParagraph> list = xdoc.getParagraphs();
|
||||
List<XWPFTable> tables = xdoc.getTables();
|
||||
for (XWPFTable table : tables) {
|
||||
//表格属性
|
||||
CTTblPr pr = table.getCTTbl().getTblPr();
|
||||
//获取表格对应的行
|
||||
List<XWPFTableRow> rows = table.getRows();
|
||||
for (XWPFTableRow row : rows) {
|
||||
//获取行对应的单元格
|
||||
List<XWPFTableCell> cells = row.getTableCells();
|
||||
for (XWPFTableCell cell : cells) {
|
||||
System.out.println(cell.getText());
|
||||
}
|
||||
}
|
||||
}
|
||||
// String lastParagraphText = "";
|
||||
// //读取文档在数据库的内容
|
||||
//
|
||||
//
|
||||
// //获取图片所在位置保存
|
||||
// for(int i = 0;i < list.size();i++){
|
||||
// List<String> imageBundleList = XWPFUtils.readImageInParagraph(list.get(i));
|
||||
// if(CollectionUtils.isNotEmpty(imageBundleList)){
|
||||
// for(String pictureId:imageBundleList){
|
||||
// XWPFPictureData pictureData = xdoc.getPictureDataByID(pictureId);
|
||||
// String imageName = pictureData.getFileName();
|
||||
// lastParagraphText = list.get(i - 1).getParagraphText();
|
||||
// if (StringUtils.isBlank(lastParagraphText)){
|
||||
// for (int j = 2 ; j<i ;j++){
|
||||
// if (StringUtils.isNotBlank(list.get(i - j).getParagraphText())){
|
||||
// lastParagraphText = list.get(i - j).getParagraphText();
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// System.out.println(pictureId +"\t|" + imageName + "\t|" + lastParagraphText);
|
||||
// //获取word文本中所有的图片
|
||||
//// List<XWPFPictureData> allPictures = xdoc.getAllPictures();
|
||||
// //遍历照片
|
||||
//// for (XWPFPictureData picture: allPictures) {
|
||||
// byte[] data = pictureData.getData();
|
||||
// File filePicture = new File("C:\\Users\\yl\\Desktop\\pictures\\" + imageName.replace("image",""));
|
||||
// if (!filePicture.exists()){
|
||||
// if (filePicture.getParentFile().exists()) {
|
||||
// filePicture.getParentFile().mkdirs();
|
||||
// }
|
||||
// filePicture.createNewFile();
|
||||
// }
|
||||
// FileOutputStream fileOutputStream = new FileOutputStream(filePicture);
|
||||
// fileOutputStream.write(data,0,data.length);
|
||||
// fileOutputStream.close();
|
||||
// System.out.println("执行结束了");
|
||||
//// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// List<XWPFTable> tables = xdoc.getTables();
|
||||
// for (XWPFTable table : tables) {
|
||||
// List<XWPFTableRow> rowList = table.getRows();
|
||||
// CTTblBorders borders = table.getCTTbl().getTblPr().addNewTblBorders();
|
||||
// CTBorder hBorder = borders.addNewInsideH();
|
||||
// hBorder.setVal(STBorder.Enum.forString("nil"));
|
||||
// hBorder.setSz(new BigInteger("1"));
|
||||
//
|
||||
// CTBorder vBorder = borders.addNewInsideV();
|
||||
// vBorder.setVal(STBorder.Enum.forString("nil"));
|
||||
// vBorder.setSz(new BigInteger("1"));
|
||||
//
|
||||
// CTBorder lBorder = borders.addNewLeft();
|
||||
// lBorder.setVal(STBorder.Enum.forString("nil"));
|
||||
// lBorder.setSz(new BigInteger("1"));
|
||||
//
|
||||
// CTBorder rBorder = borders.addNewRight();
|
||||
// rBorder.setVal(STBorder.Enum.forString("nil"));
|
||||
// rBorder.setSz(new BigInteger("1"));
|
||||
//
|
||||
// CTBorder tBorder = borders.addNewTop();
|
||||
// tBorder.setVal(STBorder.Enum.forString("thick"));
|
||||
// tBorder.setSz(new BigInteger("10"));
|
||||
//
|
||||
// CTBorder bBorder = borders.addNewBottom();
|
||||
// bBorder.setVal(STBorder.Enum.forString("thick"));
|
||||
// bBorder.setSz(new BigInteger("10"));
|
||||
//
|
||||
// CTTbl ttbl = table.getCTTbl();
|
||||
// CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl.getTblPr();
|
||||
// CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW();
|
||||
// CTJc cTJc = tblPr.addNewJc();
|
||||
// cTJc.setVal(STJc.Enum.forString("center"));
|
||||
// tblWidth.setW(new BigInteger("8000"));
|
||||
// tblWidth.setType(STTblWidth.DXA);
|
||||
|
||||
//设置表头样式与标题
|
||||
// List<XWPFTableCell> tableCells = rowList.get(0).getTableCells();
|
||||
// Iterator<Field> fieldIterator = Arrays.stream(TableDetail.class.getDeclaredFields()).iterator();
|
||||
// for (XWPFTableCell tableCell : tableCells) {
|
||||
// Field field = fieldIterator.next();
|
||||
// TableField annotation = field.getAnnotation(TableField.class);
|
||||
// String value = annotation.value();
|
||||
// CTTcBorders ctTcBorders = tableCell.getCTTc().addNewTcPr().addNewTcBorders();
|
||||
// CTBorder ctBorder = ctTcBorders.addNewBottom();
|
||||
// ctBorder.setVal(STBorder.Enum.forString("single"));
|
||||
// ctBorder.setSz(new BigInteger("1"));
|
||||
// setCellText(tableCell, value, "FFFFFF", 1600);
|
||||
// }
|
||||
// }
|
||||
// Boolean bold = false;
|
||||
for (int i=0 ; i < list.size() ; i++){
|
||||
// for (XWPFParagraph para : list) {
|
||||
System.out.println("内容为"+list.get(i).getRuns());
|
||||
// ArrayList cac_al = new ArrayList();//加粗
|
||||
// ArrayList cac_it = new ArrayList();//斜体
|
||||
// String cache = para.getText().trim();
|
||||
String cache = "";
|
||||
// if(cache.trim().toUpperCase().equals("ABSTRACT")){
|
||||
// bold = true;
|
||||
// }
|
||||
List<XWPFRun> runs = list.get(i).getRuns();
|
||||
String replace = String.valueOf(runs).replace(",", "").replace(" ","");
|
||||
if (replace.contains("Objective. A comprehensive and updated systematic review is needed to evaluate the effectiveness and safety of HuangQiXiXin decoction (HQXXD) for cough variant asthma (CVA). The aim of this systematic review protocol is to comprehensively assess the effectiveness and safety of HQXXD for CVA. Methods. An overall search for studies in main English and Chinese electronic databases from their inception to January 2021 will be performed. Randomized controlled trials (RCTs) regarding HQXXD for CVA will be included. We will use RevMan 5.3 software to perform statistical analysis and meta-analysis. Results. The findings of this systematic review will be disseminated through peer-reviewed publications. Conclusion. The systematic review will provide more evidence regarding the effectiveness and safety of HQXXD for CVA.".replace(",","").replace(" ",""))){
|
||||
System.out.println("确实包含");
|
||||
// for (int r=0 ; r < runs.size() ; r++){
|
||||
// runs.get(r);
|
||||
//
|
||||
// }
|
||||
//获取光标
|
||||
XmlCursor xmlCursor = list.get(i+1).getCTP().newCursor();
|
||||
XWPFParagraph xwpfParagraph = xdoc.insertNewParagraph(xmlCursor);
|
||||
XWPFRun run = xwpfParagraph.createRun();
|
||||
// run.setText("{{@image}}");
|
||||
FileInputStream inputStream = new FileInputStream("C:\\Users\\yl\\Desktop\\pictures\\1.png");
|
||||
run.addPicture(inputStream,XWPFDocument.PICTURE_TYPE_JPEG,"1.jgp", Units.toEMU(180),Units.toEMU(180));
|
||||
}
|
||||
|
||||
|
||||
|
||||
for (XWPFRun r : runs) {
|
||||
// if(!bold){
|
||||
// break;
|
||||
// }
|
||||
|
||||
String cach = "";
|
||||
|
||||
@@ -231,10 +369,36 @@ public class TypesetController {
|
||||
} else {
|
||||
cach = r.getText(0);
|
||||
}
|
||||
|
||||
// System.out.println(cach);
|
||||
|
||||
// if(r.isItalic()&&r.getText(0)!=null){
|
||||
// cach = "<i>"+r.getText(0)+"</i>";
|
||||
//// cac_it.add(r.getText(0));
|
||||
// }
|
||||
// if(r.isBold()&&r.getText(0)!=null){
|
||||
// cach = "<b>"+cach+"</b>";
|
||||
//// cac_al.add(r.getText(0));
|
||||
// }
|
||||
//
|
||||
cache += cach;
|
||||
}
|
||||
// for(Object cc:cac_al){
|
||||
// cache = cache.replace(cc.toString(),"<b>"+cc.toString()+"</b>");
|
||||
// }
|
||||
// for(Object ii:cac_it){
|
||||
// cache = cache.replace(ii.toString(),"<i>"+ii.toString()+"</i>");
|
||||
// }
|
||||
al.add(cache);
|
||||
}
|
||||
|
||||
FileOutputStream os = new FileOutputStream("C:\\Users\\yl\\Desktop\\pictures\\1.doc");
|
||||
xdoc.write(os);
|
||||
System.out.println("输出完毕");
|
||||
|
||||
//获取word文本中的所有表格
|
||||
// Iterator<XWPFTable> tablesIterator = xdoc.getTablesIterator();
|
||||
|
||||
} catch (Exception e) {
|
||||
e.getStackTrace();
|
||||
}
|
||||
@@ -618,7 +782,7 @@ public class TypesetController {
|
||||
// map.put("img", this.BASE_DIR + "p1.png");
|
||||
|
||||
|
||||
ArrayList<Object> arrayList = new ArrayList<>();
|
||||
|
||||
InputStream inputStream = FileDownloadUtil.getInputStream(typesetInfo.getFilename());
|
||||
|
||||
Document doc = new Document(inputStream);
|
||||
@@ -632,11 +796,12 @@ public class TypesetController {
|
||||
// file.createNewFile();
|
||||
// FileWriter fw = new FileWriter(file, true);
|
||||
// BufferedWriter bw = new BufferedWriter(fw);
|
||||
|
||||
ArrayList<List> tablesList = new ArrayList<>();
|
||||
for (int i = 0; i <sections.getCount() ; i++) {
|
||||
|
||||
TableCollection tables = sections.get(i).getTables();
|
||||
for (int j = 0; j <tables.getCount() ; j++) {
|
||||
|
||||
ArrayList<Object> arrayList = new ArrayList<>();
|
||||
|
||||
Table table = tables.get(j);
|
||||
//创建String List
|
||||
@@ -654,65 +819,118 @@ public class TypesetController {
|
||||
{
|
||||
TableCell cell = row.getCells().get(k);
|
||||
//遍历单元格中的段落
|
||||
String myText = "";
|
||||
for (int n = 0; n < cell.getParagraphs().getCount(); n++)
|
||||
{
|
||||
|
||||
Paragraph paragraph = cell.getParagraphs().get(n);
|
||||
prList.add(paragraph.getText());
|
||||
myText += paragraph.getText();
|
||||
// bw.write(paragraph.getText() + "\t");//获取文本内容
|
||||
System.out.println("表格数据"+paragraph.getText());
|
||||
System.out.println("prlist=========="+prList);
|
||||
|
||||
|
||||
//遍历段落中的所有子对象
|
||||
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());
|
||||
}
|
||||
}
|
||||
// //遍历段落中的所有子对象
|
||||
// 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());
|
||||
// }
|
||||
// }
|
||||
}
|
||||
prList.add(myText);
|
||||
}
|
||||
arrayList.add(prList);
|
||||
// bw.write("\r\n");//写入内容到txt文件
|
||||
}
|
||||
System.out.println("List=====================>"+arrayList);
|
||||
// System.out.println("List=====================>"+arrayList);
|
||||
tablesList.add(arrayList);
|
||||
}
|
||||
|
||||
}
|
||||
// System.out.println("tablesList============>"+tablesList);
|
||||
// bw.flush();
|
||||
// bw.close();
|
||||
// fw.close();
|
||||
int num = 0;
|
||||
int length = 0;
|
||||
if (tablesList.size() > 1){
|
||||
for (List ls:tablesList
|
||||
) {
|
||||
// System.out.println("ls================"+ls);
|
||||
List<Map> list = new ArrayList<>();
|
||||
LinkedHashMap<Object, Object> params = new LinkedHashMap<>();
|
||||
for (int i = 0; i < ls.size(); i++){
|
||||
|
||||
List o = (List) ls.get(i);
|
||||
List p = (List) ls.get(0);
|
||||
length = p.size();
|
||||
|
||||
if (length != o.size() ) {
|
||||
map.remove("table"+num);
|
||||
map.put("error","table"+num+"格式错误");
|
||||
break;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
//
|
||||
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);
|
||||
num++;
|
||||
map.put("table"+num, list);
|
||||
System.out.println("测试=====================》"+map.get("table"+num));
|
||||
}
|
||||
}else {
|
||||
//
|
||||
List<Map> list = new ArrayList<>();
|
||||
LinkedHashMap<Object, Object> params = new LinkedHashMap<>();
|
||||
for (int i = 0; i < tablesList.get(0).size(); i++){
|
||||
List o = (List) tablesList.get(0).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("list9===============>"+list);
|
||||
map.put("table1", list);
|
||||
}
|
||||
// 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()).
|
||||
bind("abstract", new ParagraphRenderPolicy()).bind("table1", new CustomTableRenderPolicy())
|
||||
.bind("table2", new CustomTableRenderPolicy()).bind("table3", new CustomTableRenderPolicy())
|
||||
.bind("table4", new CustomTableRenderPolicy()).bind("table5", new CustomTableRenderPolicy())
|
||||
.bind("table6", new CustomTableRenderPolicy()).bind("table7", new CustomTableRenderPolicy())
|
||||
.bind("table8", new CustomTableRenderPolicy()).bind("table9", new CustomTableRenderPolicy()).
|
||||
build();
|
||||
String main = typesetInfo.getMainText();
|
||||
List<String> l = JSON.parseArray(main, String.class);
|
||||
@@ -726,30 +944,44 @@ public class TypesetController {
|
||||
}
|
||||
map.put("main", mainArr);
|
||||
|
||||
|
||||
|
||||
|
||||
//测试
|
||||
|
||||
JSONArray ja = (JSONArray) JSON.parse(typesetInfo.getRefers());
|
||||
|
||||
List<ParagraphRenderData> refsl = new ArrayList<ParagraphRenderData>();
|
||||
for (Object tif : ja) {
|
||||
String tttt = String.valueOf(tif).replace("<b>", "").replace("</b>", "").replace("<i>", "").replace("</i>", "").trim().toUpperCase();
|
||||
// if (tttt.equals("REFERENCE") || tttt.equals("REFERENCES")) {
|
||||
// continue;
|
||||
// }
|
||||
ParagraphRenderData chc_pd = new ParagraphRenderData();
|
||||
|
||||
ArrayList referArr = new ArrayList();
|
||||
int keyn = 0;
|
||||
for (Object tif : ja){
|
||||
keyn++;
|
||||
String tttt = keyn+" "+String.valueOf(tif).replace("<b>", "").replace("</b>", "").replace("<i>", "").replace("</i>", "").trim().toUpperCase();
|
||||
Map cache = new HashMap();
|
||||
Style chch = new Style();
|
||||
chch.setFontFamily("Charis SIL");
|
||||
chch.setFontSize(7.5);
|
||||
String tgh_title = String.valueOf(tif).replace("<b>", "").replace("</b>", "").replace("<i>", "").replace("</i>", "");
|
||||
chc_pd.addText(new TextRenderData(tgh_title, chch));
|
||||
refsl.add(chc_pd);
|
||||
cache.put("ref",new TextRenderData(tttt, chch));
|
||||
referArr.add(cache);
|
||||
}
|
||||
|
||||
map.put("refs", new NumberingRenderData(NumberingFormat.DECIMAL, refsl));
|
||||
map.put("refers",referArr);
|
||||
|
||||
|
||||
// //测试
|
||||
//
|
||||
// JSONArray ja = (JSONArray) JSON.parse(typesetInfo.getRefers());
|
||||
//
|
||||
// List<ParagraphRenderData> refsl = new ArrayList<ParagraphRenderData>();
|
||||
// for (Object tif : ja) {
|
||||
// String tttt = String.valueOf(tif).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 = String.valueOf(tif).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 = "";
|
||||
//确定使用的模板
|
||||
@@ -769,8 +1001,10 @@ public class TypesetController {
|
||||
f.mkdirs();
|
||||
}
|
||||
String goFileName = System.currentTimeMillis() + ".docx";
|
||||
frag.put("file", new SimpleDateFormat("yyyyMMdd").format(date) + "/" + goFileName);
|
||||
// frag.put("file", new SimpleDateFormat("yyyyMMdd").format(date) + "/" + goFileName);
|
||||
template.writeAndClose(new FileOutputStream(dirpath + "/" + goFileName));
|
||||
String filePath = addPic1(typesetInfo.getFilename(), dirpath + "/" + goFileName);
|
||||
frag.put("file", filePath);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -1098,7 +1332,7 @@ public class TypesetController {
|
||||
// }
|
||||
// FileOutputStream os = new FileOutputStream(IMG_DIR+"New.doc");
|
||||
// newXdoc.write(os);
|
||||
doc.saveToFile(IMG_DIR + randomFix + uuid+".doc");
|
||||
doc.saveToFile(IMG_DIR + randomFix + uuid+".docx");
|
||||
System.out.println("输出完毕");
|
||||
// os.close();
|
||||
fis.close();
|
||||
@@ -1174,6 +1408,240 @@ public class TypesetController {
|
||||
}
|
||||
return new ReturnValue(ReturnCodeAndMsgEnum.Success, m);
|
||||
}
|
||||
public String addPic1(String OldFileName,String newFileName){
|
||||
//1.操作初始文档获取图片 和 图片所在的位置
|
||||
String filePath = "";
|
||||
try {
|
||||
// File file = new File(this.BASE_DIR + OldFileName);
|
||||
InputStream inputStream = FileDownloadUtil.getInputStream(OldFileName);
|
||||
// FileInputStream fis = new FileInputStream(inputStream);
|
||||
XWPFDocument xdoc = new XWPFDocument(inputStream);
|
||||
List<XWPFParagraph> list = xdoc.getParagraphs();
|
||||
String lastParagraphText = "";
|
||||
//TODO 读取文档在数据库的内容
|
||||
|
||||
//准备一个图片名称list
|
||||
ArrayList<String> picNameList = new ArrayList<>();
|
||||
//准备一个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));
|
||||
if(CollectionUtils.isNotEmpty(imageBundleList)){
|
||||
for(String pictureId:imageBundleList){
|
||||
XWPFPictureData pictureData = xdoc.getPictureDataByID(pictureId);
|
||||
String imageName = pictureData.getFileName();
|
||||
picNameList.add(imageName);
|
||||
// for (int j=1; j<i ; j++) {
|
||||
// if (StringUtils.isNotBlank(list.get(i - j).getParagraphText())) {
|
||||
// lastParagraphText = list.get(i - j).getParagraphText();
|
||||
// }
|
||||
// }
|
||||
lastParagraphText = list.get(i - 1).getParagraphText();
|
||||
if (StringUtils.isBlank(lastParagraphText)){
|
||||
for (int j = 2 ; j<i ;j++){
|
||||
if (StringUtils.isNotBlank(list.get(i - j).getParagraphText())){
|
||||
lastParagraphText = list.get(i - j).getParagraphText();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
picMap.put(imageName,lastParagraphText);
|
||||
System.out.println(pictureId +"\t|" + imageName + "\t|" + lastParagraphText);
|
||||
//获取word文本中所有的图片
|
||||
// List<XWPFPictureData> allPictures = xdoc.getAllPictures();
|
||||
//遍历照片
|
||||
// for (XWPFPictureData picture: allPictures) {
|
||||
byte[] data = pictureData.getData();
|
||||
File filePicture = new File(IMG_DIR + randomFix + imageName.replace("image",""));
|
||||
if (!filePicture.exists()){
|
||||
if (!filePicture.getParentFile().exists()) {
|
||||
filePicture.getParentFile().mkdirs();
|
||||
}
|
||||
filePicture.createNewFile();
|
||||
}
|
||||
FileOutputStream fileOutputStream = new FileOutputStream(filePicture);
|
||||
fileOutputStream.write(data,0,data.length);
|
||||
fileOutputStream.close();
|
||||
System.out.println("执行结束了");
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println("picNameList===========>"+picNameList);
|
||||
System.out.println("picMap===========>"+picMap);
|
||||
//2.获取按模板新生成的文档添加图片
|
||||
File newFile = new File(newFileName);
|
||||
FileInputStream newFis = new FileInputStream(newFile);
|
||||
XWPFDocument newXdoc = new XWPFDocument(newFis);
|
||||
List<XWPFParagraph> paragraphs = newXdoc.getParagraphs();
|
||||
|
||||
|
||||
//其他方式
|
||||
Document doc = new Document();
|
||||
doc.loadFromFile(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 + 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);
|
||||
// //正则表达式
|
||||
// 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 (width <= 22000f){
|
||||
//图片为小图 双栏
|
||||
DocPicture docPicture = paragraph.appendPicture(imgPath);
|
||||
docPicture.setWidth(220f);
|
||||
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);
|
||||
}
|
||||
// }
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
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(0.2F);
|
||||
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(0.2F);
|
||||
//设置表格的水平和垂直边框
|
||||
// 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(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);
|
||||
tables.get(j).autoFit(AutoFitBehaviorType.Auto_Fit_To_Window);
|
||||
|
||||
// tables.get(j).getTableFormat().setHorizontalAlignment(RowAlignment.Right);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
|
||||
|
||||
// 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 + randomFix + uuid+".docx");
|
||||
System.out.println("输出完毕");
|
||||
// os.close();
|
||||
inputStream.close();
|
||||
newFis.close();
|
||||
filePath = IMG_DIR + randomFix + uuid+".docx";
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return filePath;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user