This commit is contained in:
wangjinlei
2025-01-23 15:46:22 +08:00
parent 13d5bc4b23
commit b8306ffae5
3 changed files with 250 additions and 319 deletions

View File

@@ -50,9 +50,6 @@ public class MyContentPolicy extends AbstractRenderPolicy<MyParagraphRenderData>
Iterator var6 = contents.iterator();
while(var6.hasNext()) {
RenderData content = (RenderData)var6.next();
// String name = content.getClass().getSimpleName();
// System.out.println(name);
XWPFRun fragment = parentContext.insertNewRun(ParagraphUtils.getRunPos(run));
StyleUtils.styleRun(fragment, run);
if (content instanceof TextRenderData) {

View File

@@ -34,10 +34,7 @@ import org.apache.poi.xwpf.usermodel.*;
import org.freehep.graphicsio.emf.EMFHeader;
import org.freehep.graphicsio.emf.EMFInputStream;
import org.freehep.graphicsio.emf.EMFRenderer;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblBorders;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcBorders;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -54,6 +51,7 @@ import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Array;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
@@ -67,13 +65,12 @@ public class TypesetController {
private final static Logger logger = LoggerFactory.getLogger(UserController.class);
private String BASE_DIR = "/home/wwwroot/ts.tmrjournals.com/upload/";
private String IMG_BASE_DIR = "/home/wwwroot/api.tmrjournals.com/public/articleImage/";
private String IMG_DIR = "/home/wwwroot/ts.tmrjournals.com/upload/pictures/";
// private String BASE_DIR = "/home/wwwroot/ts.tmrjournals.com/upload/";
// private String IMG_BASE_DIR = "D:/phpstudy_pro/WWW/tougao/public/articleImage/";
// private String IMG_BASE_DIR = "/home/wwwroot/api.tmrjournals.com/public/articleImage/";
private String BASE_DIR = "D:/tsfile/";
private String IMG_BASE_DIR = "D:/phpstudy_pro/WWW/tougao/public/articleImage/";
@Autowired
@@ -340,10 +337,18 @@ public class TypesetController {
try (FileOutputStream fos = new FileOutputStream(f) ){
IOUtils.copy(new ByteArrayInputStream(imageData), fos);
}
flagImages.add("image-"+randomNumber + imgNum+"."+imageFileName);
String img_str = "<img src='https://submission.tmrjournals.com/public/articleImage/"+article_id+"/image-"+ randomNumber+ imgNum+"."+imageFileName+"'/>";
//存储图片数据到总数组
al.add(img_str);
if(imageFileName=="tif"){
ReturnValue returnValue = this.crossImage(article_id + "/" + "image-" + randomNumber + imgNum + "." + imageFileName);
String file = ((Map) returnValue.getData()).get("file").toString();
flagImages.add("image-" + randomNumber + imgNum + ".png");
String img_str = "<img src='https://submission.tmrjournals.com/public/articleImage/"+file+"'/>";
al.add(img_str);
}else{
flagImages.add("image-"+randomNumber + imgNum+"."+imageFileName);
String img_str = "<img src='https://submission.tmrjournals.com/public/articleImage/"+article_id+"/image-"+ randomNumber+ imgNum+"."+imageFileName+"'/>";
//存储图片数据到总数组
al.add(img_str);
}
imgNum++;
}
}
@@ -662,46 +667,6 @@ public class TypesetController {
return new ReturnValue(ReturnCodeAndMsgEnum.Success, frag);
}
@PostMapping("/myTest")
public ReturnValue myTest(@RequestBody TypesetInfo typesetInfo) throws IOException {
HashMap<String, String> frag = new HashMap<>();
HashMap<String,Object> map = new HashMap<>();
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.crowStrNew(s);
// cache.put("content", capd);
mainArr.add(cache);
}
map.put("main", mainArr);
// Configure config = Configure.builder().bind("content", new ParagraphRenderPolicy()).build();
Configure config = Configure.builder().bind("content", new MyContentPolicy()).build();
String myTemplate = "D:/tt1.docx";
XWPFTemplate template = XWPFTemplate.compile( myTemplate, config).render(map);
try {
Date date = new Date();
String dirpath = "D:/" + 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(ReturnCodeAndMsgEnum.Success,frag);
}
@PostMapping("/createDocx")
public ReturnValue createDocx(@RequestBody TypesetInfo typesetInfo) throws IOException {
Map frag = new HashMap();
@@ -779,24 +744,96 @@ public class TypesetController {
abstract_p.addText(new TextRenderData("input abstract", doi_s));
map.put("abstract", abstract_p);
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();
// 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) {
Map cache = new HashMap();
Map otherData = new HashMap();
MyParagraphRenderData capd = crowStrNew(s,typesetInfo.getTables(),typesetInfo.getImages());
if(checkImg(s)||s.contains("<table")){
otherData.put("data",capd);
otherArr.add(otherData);
}else {
ParagraphStyle paragraphStyle = new ParagraphStyle();
paragraphStyle.setAlign(ParagraphAlignment.CENTER);
paragraphStyle.setSpacingAfterLines(Double.valueOf(1));
if(isImgTag(s)){
MyParagraphRenderData myParagraphRenderData = crowStrNew(s, tableDatas, imageDatas);
Map cache = new HashMap();
ArrayList otherAllList = new ArrayList<>();
if(checkImg(s)){
cache.put("data",myParagraphRenderData);
otherAllList.add(cache);
}else {
cache.put("content",myParagraphRenderData);
mainArr.add(cache);
}
String regex = "imageId='(\\d+)'";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(s);
int image_id = 0;
if (matcher.find()) {
image_id= Integer.valueOf(matcher.group(1));
}
Map<String, String> stringStringMap = imageDatas.get(image_id);
String s1 = stringStringMap.get("note");
MyParagraphRenderData myParagraphRenderData1 = crowStrNew(s1, tableDatas, imageDatas);
Map cache1 = new HashMap();
myParagraphRenderData1.setParagraphStyle(paragraphStyle);
if(checkImg(s)){
cache1.put("data",myParagraphRenderData1);
otherAllList.add(cache1);
otherAll.put("otherall"+otherNum,otherAllList);
MyParagraphRenderData myParagraphRenderData2 = new MyParagraphRenderData();
myParagraphRenderData2.addText("{{+otherall"+otherNum+"}}");
HashMap hashMap = new HashMap();
hashMap.put("content",myParagraphRenderData2);
mainArr.add(hashMap);
otherNum++;
}else {
cache1.put("content",myParagraphRenderData1);
mainArr.add(cache1);
}
} else if (s.contains("<table")) {
ArrayList arrayList = new ArrayList();
String regex = "<table\\s+tableId='(\\d+)'\\s*/>";
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); // 不区分大小写
Matcher matcher = pattern.matcher(s);
String tid = "";
if(matcher.find()){
tid = matcher.group(1);
}
Map<String, String> stringStringMap = tableDatas.get(tid);
MyParagraphRenderData myParagraphRenderData = crowStrNew(stringStringMap.get("title"), tableDatas, imageDatas);
myParagraphRenderData.setParagraphStyle(paragraphStyle);
Map cache = new HashMap();
cache.put("data",myParagraphRenderData);
arrayList.add(cache);
MyParagraphRenderData myParagraphRenderData1 = crowStrNew(s, tableDatas, imageDatas);
Map cache1 = new HashMap();
cache1.put("data",myParagraphRenderData1);
arrayList.add(cache1);
MyParagraphRenderData myParagraphRenderData2 = crowStrNew(stringStringMap.get("note"), tableDatas, imageDatas);
myParagraphRenderData2.setParagraphStyle(paragraphStyle);
Map cache2 = new HashMap();
cache2.put("data",myParagraphRenderData2);
arrayList.add(cache2);
otherAll.put("otherall"+otherNum,arrayList);
MyParagraphRenderData myParagraphRenderData3 = new MyParagraphRenderData();
myParagraphRenderData3.addText("{{+otherall"+otherNum+"}}");
HashMap hashMap1 = new HashMap();
hashMap1.put("content",myParagraphRenderData3);
mainArr.add(hashMap1);
otherNum++;
}else{
Map cache = new HashMap();
MyParagraphRenderData capd = crowStrNew(s,typesetInfo.getTables(),typesetInfo.getImages());
cache.put("content", capd);
mainArr.add(cache);
}
}
map.put("main", mainArr);
map.put("other",otherArr);
//参考文献部分
JSONArray ja = (JSONArray) JSON.parse(typesetInfo.getRefers());
List<ParagraphRenderData> refsl = new ArrayList<ParagraphRenderData>();
@@ -877,18 +914,15 @@ public class TypesetController {
.bind("peer", new ParagraphRenderPolicy())
.build();
XWPFTemplate template = XWPFTemplate.compile(this.BASE_DIR + myTemplate, config).render(map);
// XWPFTemplate template = XWPFTemplate.compile("D:/tt1.docx", 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); // 应用三线表样式
}
// 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);
// String dirpath = "D:/" + new SimpleDateFormat("yyyyMMdd").format(date);
//如果不存在,创建文件夹
File f = new File(dirpath);
if (!f.exists()) {
@@ -896,7 +930,40 @@ public class TypesetController {
}
String goFileName = System.currentTimeMillis() + ".docx";
template.writeAndClose(new FileOutputStream(dirpath + "/" + goFileName));
frag.put("file", goFileName);
//二次写入
if(otherAll.size()>0){
HashMap<String, Object> othermap = new HashMap<>();
for (Map.Entry<String, ArrayList<Map<String,MyParagraphRenderData>>> entry : otherAll.entrySet()) {
String key = entry.getKey();
ArrayList<Map<String,MyParagraphRenderData>> value = entry.getValue();
HashMap<String, Object> map1 = new HashMap<>();
map1.put("other",value);
othermap.put(key,Includes.ofLocal(this.BASE_DIR+"table.docx").setRenderModel(map1).create());
}
Configure config1 = Configure.builder().bind("data",new MyContentPolicy()).build();
XWPFTemplate template1 = XWPFTemplate.compile(dirpath + "/" + goFileName, config1).render(othermap);
Date date1 = new Date();
String dirpath1 = this.BASE_DIR + new SimpleDateFormat("yyyyMMdd").format(date1);
XWPFDocument document1 = template1.getXWPFDocument();
List<XWPFTable> tables1 = document1.getTables();
for (int i = 1; i < tables1.size(); i++) { // 从索引 1 开始,跳过第一个表格
XWPFTable table22 = tables1.get(i);
setThreeLineTableStyle(table22); // 应用三线表样式
}
//如果不存在,创建文件夹
File f1 = new File(dirpath1);
if (!f1.exists()) {
f1.mkdirs();
}
String goFileName1 = System.currentTimeMillis() + ".docx";
template1.writeAndClose(new FileOutputStream(dirpath1 + "/" + goFileName1));
frag.put("file", goFileName1);
}else {
frag.put("file", goFileName);
}
} catch (IOException e) {
e.printStackTrace();
}
@@ -904,6 +971,7 @@ public class TypesetController {
}
@PostMapping("/crossImage")
public ReturnValue crossImage(String url) throws FileNotFoundException {
String fileUrl = IMG_BASE_DIR+url;
@@ -1270,17 +1338,26 @@ public class TypesetController {
Map<String, String> stringStringMap = images.get(image_id);
String imagePath = stringStringMap.get("image");
BufferedImage image = ImageIO.read(new File(IMG_BASE_DIR+imagePath));
PictureRenderData pictureRenderData = new PictureRenderData(image.getWidth(), image.getHeight(),IMG_BASE_DIR+imagePath);
PictureStyle pictureStyle = new PictureStyle();
pictureStyle.setAlign(PictureStyle.PictureAlign.CENTER);
pictureRenderData.setPictureStyle(pictureStyle);
int width = image.getWidth();
int height = image.getHeight();
if(width>650){
height = 650*height/width;
width = 650;
}
PictureRenderData pictureRenderData = new PictureRenderData(width, height,IMG_BASE_DIR+imagePath);
// PictureStyle pictureStyle = new PictureStyle();
// pictureStyle.setAlign(PictureStyle.PictureAlign.CENTER);
// pictureRenderData.setPictureStyle(pictureStyle);
p.addPicture(pictureRenderData);
ParagraphStyle paragraphStyle = new ParagraphStyle();
paragraphStyle.setAllowWordBreak(false);
paragraphStyle.setSpacingBeforeLines(Double.valueOf(1));
paragraphStyle.setSpacingAfterLines(Double.valueOf(1));
paragraphStyle.setAlign(ParagraphAlignment.CENTER);
paragraphStyle.setSpacing(Double.valueOf(10));
p.setParagraphStyle(paragraphStyle);
String note = stringStringMap.get("note");
textRenderCreate(note,p);
return p;
}
@@ -1293,42 +1370,45 @@ public class TypesetController {
if(matcher.find()){
tid = matcher.group(1);
}
int tableId = Integer.valueOf(tid);
Map<String, String> stringStringMap = map.get(tableId);
String title_table = stringStringMap.get("title");
String note_table = stringStringMap.get("note");
textRenderCreate(title_table,p);
// int tableId = Integer.valueOf(tid);
Map<String, String> stringStringMap = map.get(tid);
ArrayList<ArrayList<Map<String, String>>> tableData = JSON.parseObject(stringStringMap.get("table_data"), new TypeReference<ArrayList<ArrayList<Map<String, String>>>>() {});
TableRenderData tableRenderData = new TableRenderData();
for (ArrayList<Map<String, String>> row:tableData){
RowRenderData rowRenderData = new RowRenderData();
for (Map<String,String> rowBean:row){
CellRenderData cellRenderData = new CellRenderData();
ParagraphRenderData paragraphRenderData = new ParagraphRenderData();
ParagraphRenderData text = crowStrNew(rowBean.get("text"), map,images);
ParagraphRenderData text = crowStrNew(rowBean.get("text").trim(), map,images);
List<ParagraphRenderData> plist = new ArrayList<>();
plist.add(text);
plist.add(paragraphRenderData);
cellRenderData.setParagraphs(plist);
rowRenderData.addCell(cellRenderData);
}
RowStyle rowStyle = new RowStyle();
rowStyle.setHeight(12);
rowRenderData.setRowStyle(rowStyle);
tableRenderData.addRow(rowRenderData);
}
// TableStyle tableStyle = new TableStyle();
// tableStyle.setAlign(TableRowAlign.CENTER);
// tableRenderData.setTableStyle(tableStyle);
p.addTable(tableRenderData);
ParagraphStyle paragraphStyle = new ParagraphStyle();
paragraphStyle.setAlign(ParagraphAlignment.CENTER);
paragraphStyle.setSpacing(Double.valueOf(12));
textRenderCreate(note_table,p);
return p;
}
if(s.isEmpty()){
p.addText(s);
}else{
textRenderCreate(s,p);
}
textRenderCreate(s,p);
return p;
}
private void textRenderCreate(String s,MyParagraphRenderData p){
while (!s.isEmpty()){
String s1 = determineNextTag(s);
@@ -1337,7 +1417,7 @@ public class TypesetController {
s = "";
}else{
int beginTag = s.indexOf("<"+s1+">");
int endTag = s.indexOf("</"+s1+">");
int endTag = getLastTabIndex(s,s1);
if(beginTag>0){
p.addText(s.substring(0,beginTag));
}
@@ -1353,6 +1433,55 @@ public class TypesetController {
}
}
public Integer getLastTabIndex(String s,String tab){
int beginTag = s.indexOf("<"+tab+">");
int endTag_ca = s.indexOf("</"+tab+">");
int endTag = 0;
Pattern pattern = Pattern.compile("<" + tab + ">");
String ca_now_str = s.substring(beginTag,endTag_ca+ tab.length() + 3);
Matcher matcher = pattern.matcher(ca_now_str);
int ca_count = 0;
while (matcher.find()) {
ca_count++;
}
if(ca_count==1){
endTag = endTag_ca;
}else{
int really_num = 0;
while (really_num!=ca_count){
String substring = s.substring(beginTag, getNumIndex(s, tab, ca_count));
Matcher matcher1 = pattern.matcher(substring);
int cca_count = 0;
while (matcher1.find()){
cca_count++;
}
if(cca_count!=ca_count){
ca_count = cca_count;
}else{
really_num = cca_count;
}
}
endTag = getNumIndex(s,tab,really_num);
}
return endTag;
}
private Integer getNumIndex(String s,String tab,Integer num){
int count_cc_n = 0;
int index_aa = -1; // 初始化为-1表示开始时未找到
while ((index_aa = s.indexOf("</"+tab+">", index_aa + 1)) != -1) {
count_cc_n++;
if (count_cc_n == num) {
break;
}
}
return index_aa;
}
public boolean isImgTag(String str) {
// 定义正则表达式,用于匹配 <img> 标签
String imgTagPattern = "<\\s*img\\s+[^>]*\\s*\\/?>";
@@ -1377,8 +1506,9 @@ public class TypesetController {
textRenderData.add(textRenderData1);
content = "";
}else{
int beginTag = content.indexOf("<"+s+">");
int endTag = content.indexOf("</"+s+">");
int endTag = getLastTabIndex(content,s);
if(beginTag>0){
TextRenderData textRenderData1 = new TextRenderData(content.substring(0, beginTag), style);
textRenderData.add(textRenderData1);
@@ -1463,7 +1593,8 @@ public class TypesetController {
s.setVertAlign("superscript");
break;
case "blue":
s.setColor("0000FF");
s.setColor("0082AA");
break;
case "sub":
s.setVertAlign("subscript");
@@ -1777,221 +1908,5 @@ 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()) + "/";
String path = IMG_DIR + randomFix;
File folder = new File(IMG_DIR + randomFix);
if (!folder.exists()) {
folder.mkdirs();
}
//获取图片所在位置保存
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);
byte[] data = pictureData.getData();
File filePicture = new File(path + 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();
}
}
}
//其他方式
Document doc = new Document();
doc.loadFromFile(newFileName);
Section sec = doc.getSections().get(0);
String text = doc.getText();
Paragraph para = sec.getParagraphs().get(0);
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);
for (String pic : picNameList) {
String replace = String.valueOf(paragraph.getText()).replace(",", "").replace(" ", "");
if (replace.contains(picMap.get(pic).replace(",", "").replace(" ", ""))) {
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();
//System.out.println("图片长度"+width);
//判断图片中是否包含 文字
// 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 <= 782f) {
//图片为小图 双栏
DocPicture docPicture = paragraph.appendPicture(imgPath);
docPicture.setWidth(250f);
// docPicture.setHeight(216f);
docPicture.setTextWrappingStyle(TextWrappingStyle.Top_And_Bottom);
docPicture.setTextWrappingType(TextWrappingType.Both);
float x = docPicture.getHorizontalPosition();
float y = docPicture.getVerticalPosition();
//获取图片宽度、高度
float width1 = docPicture.getWidth();
float height = docPicture.getHeight();
//获取图片文字环绕
String wrappingstyle = docPicture.getTextWrappingStyle().toString();//图片文字环绕方式
String wrappingtype = docPicture.getTextWrappingType().toString();//环绕文字类型
} else {
//图片为大图 单栏
DocPicture docPicture = paragraph.appendPicture(imgPath);
docPicture.setWidth(495f);
// docPicture.setHeight(150f);
docPicture.setHorizontalPosition(0.0f);
docPicture.setVerticalPosition(10.0f);
docPicture.setTextWrappingStyle(TextWrappingStyle.Top_And_Bottom);
docPicture.setTextWrappingType(TextWrappingType.Both);
float x = docPicture.getHorizontalPosition();
float y = docPicture.getVerticalPosition();
//获取图片宽度、高度
float width1 = docPicture.getWidth();
float height = docPicture.getHeight();
//获取图片文字环绕
String wrappingstyle = docPicture.getTextWrappingStyle().toString();//图片文字环绕方式
String wrappingtype = docPicture.getTextWrappingType().toString();//环绕文字类型
}
// }
}
}
}
}
// 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("-", "");
doc.saveToFile(path + uuid + ".docx");
inputStream.close();
filePath = randomFix + uuid + ".docx";
} catch (Exception e) {
e.printStackTrace();
}
return filePath;
}
}

View File

@@ -1,6 +1,11 @@
package com.example.ts_obj.entity;
import com.fasterxml.jackson.annotation.JsonSetter;
import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
@@ -67,9 +72,9 @@ public class TypesetInfo implements Serializable {
private String userAccount;
private Map<Integer,Map<String,String>> images;
private Map<Integer,Map<String,String>> images=new LinkedHashMap<>();;
private Map<Integer,Map<String,String>> tables;
private Map<Integer,Map<String,String>> tables=new LinkedHashMap<>();;
private static final long serialVersionUID = 1L;
@@ -205,16 +210,30 @@ public class TypesetInfo implements Serializable {
return images;
}
@JsonSetter
public void setImages(Map<Integer, Map<String, String>> images) {
this.images = images;
if (images instanceof List) {
// 处理数组
this.images = new HashMap<>();
// 根据需要将数组转化为 Map
} else {
this.images = (Map<Integer, Map<String, String>>) images;
}
}
public Map<Integer, Map<String, String>> getTables() {
return tables;
}
public void setTables(Map<Integer, Map<String, String>> tables) {
this.tables = tables;
@JsonSetter
public void setTables(Object tables) {
if (tables instanceof List) {
// 处理数组
this.tables = new HashMap<>();
// 根据需要将数组转化为 Map
} else {
this.tables = (Map<Integer, Map<String, String>>) tables;
}
}
public void setStage(String stage) {