|
@@ -41,47 +41,65 @@ import java.util.Map;
|
|
|
public class PoiService extends ServiceImpl<PatentPledgeMapper, PatentPledge> {
|
|
|
public PoiVO readWordFile() throws IOException {
|
|
|
PoiVO poiVO =new PoiVO();
|
|
|
+ List<PoiVO.Pg> pgs =new ArrayList<>();
|
|
|
InputStream is = new FileInputStream("C:\\\\BaiduNetdiskDownload\\\\生成\\\\first.docx");
|
|
|
XWPFDocument doc = new XWPFDocument(is);
|
|
|
List<IBodyElement> bodyElements= doc.getBodyElements();
|
|
|
for(IBodyElement iBodyElement: bodyElements)
|
|
|
- {
|
|
|
-
|
|
|
- if(iBodyElement.getElementType().equals(""))
|
|
|
- {
|
|
|
-
|
|
|
+ { PoiVO.Pg pg = new PoiVO.Pg();
|
|
|
+ if(iBodyElement.getElementType().name().equals("PARAGRAPH"))
|
|
|
+ {XWPFParagraph paragraph= (XWPFParagraph) iBodyElement;
|
|
|
+ if(paragraph.getStyle()!=null&¶graph.getStyle().equals("word")) {
|
|
|
+ XWPFRun rh = paragraph.getRuns().get(0);
|
|
|
+ pg.setFontSize(rh.getFontSize());
|
|
|
+ pg.setFontFamily(rh.getFontFamily());
|
|
|
+ pg.setText(rh.getText(0));
|
|
|
+ pg.setColor(rh.getColor());
|
|
|
+ pg.setAlignment(paragraph.getFontAlignment());
|
|
|
+ pg.setType(paragraph.getStyle());
|
|
|
+ pgs.add(pg);
|
|
|
+ }
|
|
|
+ else if(paragraph.getStyle()!=null&¶graph.getStyle().equals("picture")) {
|
|
|
+ XWPFRun rh = paragraph.getRuns().get(0);
|
|
|
+ pg.setAlignment(paragraph.getFontAlignment());
|
|
|
+ pg.setType(paragraph.getStyle());
|
|
|
+ XWPFPicture picture = rh.getEmbeddedPictures().get(0);
|
|
|
+ pg.setHeight((int) picture.getDepth());
|
|
|
+ pg.setWeight((int) picture.getWidth());
|
|
|
+ pg.setImgUrl(picture.getDescription());
|
|
|
+ pgs.add(pg);
|
|
|
+ }
|
|
|
+ else if(paragraph.getStyle()!=null&¶graph.getStyle().contains("Heading")) {
|
|
|
+ XWPFRun rh = paragraph.getRuns().get(0);
|
|
|
+ pg.setFontSize(rh.getFontSize());
|
|
|
+ pg.setFontFamily(rh.getFontFamily());
|
|
|
+ pg.setText(rh.getText(0));
|
|
|
+ pg.setColor(rh.getColor());
|
|
|
+ pg.setAlignment(paragraph.getFontAlignment());
|
|
|
+ pg.setType(paragraph.getStyle());
|
|
|
+ pgs.add(pg);
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
|
|
|
+ else if(iBodyElement.getElementType().name().equals("TABLE")){
|
|
|
+ XWPFTable table = (XWPFTable) iBodyElement;
|
|
|
+String org = table.getText().replace("\t",",").replace("\n","||");
|
|
|
+String tableText =org.replace(",||","");
|
|
|
+ pg.setText(tableText);
|
|
|
+ pg.setType(table.getStyleID());
|
|
|
+ pgs.add(pg);
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
List<XWPFParagraph> ps=doc.getParagraphs();
|
|
|
PoiVO.Pg title = new PoiVO.Pg();
|
|
|
- List<PoiVO.Pg> pgs =new ArrayList<>();
|
|
|
title.setAlignment(ps.get(0).getFontAlignment());
|
|
|
XWPFRun r = ps.get(0).getRuns().get(0);
|
|
|
title.setColor(r.getColor());
|
|
|
title.setText(r.getText(0));
|
|
|
title.setFontSize(r.getFontSize());
|
|
|
|
|
|
-for(int i=1 ;i<ps.size();i++)
|
|
|
-{ PoiVO.Pg pg = new PoiVO.Pg();
|
|
|
- if(ps.get(i).getStyle().equals("word")) {
|
|
|
- XWPFRun rh = ps.get(i).getRuns().get(0);
|
|
|
- pg.setFontSize(rh.getFontSize());
|
|
|
- pg.setFontFamily(rh.getFontFamily());
|
|
|
- pg.setText(rh.getText(0));
|
|
|
- pg.setColor(rh.getColor());
|
|
|
- pg.setAlignment(ps.get(i).getFontAlignment());
|
|
|
- pg.setType(ps.get(i).getStyle());
|
|
|
- pgs.add(pg);
|
|
|
- }
|
|
|
- else if(ps.get(i).getStyle().equals("picture")) {
|
|
|
- XWPFRun rh = ps.get(i).getRuns().get(0);
|
|
|
- rh.getPictureText();
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
poiVO.setPgs(pgs);
|
|
|
poiVO.setTitle(title);
|
|
|
return poiVO;
|
|
@@ -98,15 +116,20 @@ poiVO.setTitle(title);
|
|
|
r.setBold(title.getBold());//设置为粗体
|
|
|
r.setColor(title.getColor());//设置颜色
|
|
|
r.setFontSize(title.getFontSize());//设置字体大小
|
|
|
+ r.setStyle("Title");
|
|
|
if(title.getAddCarriageReturn()){
|
|
|
r.addCarriageReturn();//回车换行
|
|
|
}
|
|
|
+ doc.createParagraph().createRun().addBreak(BreakType.PAGE);//换页
|
|
|
+
|
|
|
List<PoiVO.Pg> pgs = poiVO.getPgs();
|
|
|
|
|
|
CTSdtBlock ctSdtBlock =doc.getDocument().getBody().addNewSdt();
|
|
|
this.CustomTOC(ctSdtBlock);
|
|
|
+ doc.createParagraph().createRun().addBreak(BreakType.PAGE);//换页
|
|
|
for(int i=0;i<pgs.size();i++){
|
|
|
- if(pgs.get(i).getType().equals("word")) {
|
|
|
+
|
|
|
+ if(pgs.get(i).getType()!=null&&pgs.get(i).getType().equals("word")) {
|
|
|
XWPFParagraph tm = doc.createParagraph();
|
|
|
tm.setStyle("word");
|
|
|
XWPFRun rh = tm.createRun();
|
|
@@ -120,7 +143,7 @@ poiVO.setTitle(title);
|
|
|
rh.addCarriageReturn();//回车换行
|
|
|
}
|
|
|
}
|
|
|
- else if(pgs.get(i).getType().equals("picture")) {
|
|
|
+ else if(pgs.get(i).getType()!=null&&pgs.get(i).getType().equals("picture")) {
|
|
|
XWPFParagraph pImg = doc.createParagraph();
|
|
|
pImg.setStyle("picture");
|
|
|
pImg.setFontAlignment(pgs.get(i).getAlignment());
|
|
@@ -131,9 +154,8 @@ poiVO.setTitle(title);
|
|
|
InputStream stream = new FileInputStream(imgData);
|
|
|
// 向段落中插入图片
|
|
|
rImg.addPicture(stream, Document.PICTURE_TYPE_PNG, pgs.get(i).getImgUrl(), Units.toEMU(pgs.get(i).getWeight()), Units.toEMU(pgs.get(i).getHeight()));
|
|
|
-
|
|
|
}
|
|
|
- else if(pgs.get(i).getType().equals("table")) {
|
|
|
+ else if(pgs.get(i).getType()!=null&&pgs.get(i).getType().equals("table")) {
|
|
|
String org =pgs.get(i).getText();
|
|
|
int rows =0;
|
|
|
int cols =0;
|
|
@@ -145,7 +167,7 @@ String[] row =org.split("\\|\\|");
|
|
|
}
|
|
|
|
|
|
XWPFTable table1 = doc.createTable(rows, cols);
|
|
|
- table1.setStyleID("");
|
|
|
+ table1.setStyleID(pgs.get(i).getType());
|
|
|
table1.setWidthType(TableWidthType.AUTO);
|
|
|
for(int t=0;t<row.length;t++){
|
|
|
String[] tem = row[t].split(",");
|
|
@@ -158,7 +180,7 @@ String[] row =org.split("\\|\\|");
|
|
|
doc.setTable(0, table1);
|
|
|
|
|
|
}
|
|
|
- else if(pgs.get(i).getType().equals("Heading1")){
|
|
|
+ else if(pgs.get(i).getType().contains("Heading")){
|
|
|
XWPFParagraph Paragraph = doc.createParagraph();
|
|
|
CTBookmark ctBookmark1 = Paragraph.getCTP().addNewBookmarkStart();//开始书签
|
|
|
ctBookmark1.setId(BigInteger.valueOf(i));//开始书签和结束书签通过ID关联
|
|
@@ -170,12 +192,10 @@ String[] row =org.split("\\|\\|");
|
|
|
run1.setFontSize(pgs.get(i).getFontSize());
|
|
|
run1.setBold(true);
|
|
|
Paragraph.getCTP().addNewBookmarkEnd().setId(BigInteger.valueOf(i));
|
|
|
- this.addItem2TOC(doc,ctSdtBlock);
|
|
|
-
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
+ this.addItem2TOC(doc,ctSdtBlock);
|
|
|
+ Integer pageCount = doc.getProperties().getExtendedProperties().getUnderlyingProperties().getPages();
|
|
|
FileOutputStream out = new FileOutputStream("C:\\\\BaiduNetdiskDownload\\\\生成\\\\first.docx");
|
|
|
CTSectPr sectPr = doc.getDocument().getBody().addNewSectPr();
|
|
|
XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(doc, sectPr);
|
|
@@ -202,15 +222,25 @@ String[] row =org.split("\\|\\|");
|
|
|
|
|
|
public void addItem2TOC( XWPFDocument doc ,CTSdtBlock block) {
|
|
|
List<XWPFParagraph> paragraphs = doc.getParagraphs();
|
|
|
+ int num =1;
|
|
|
for (XWPFParagraph par : paragraphs) {
|
|
|
String parStyle = par.getStyle();
|
|
|
+ List<CTR> ctrlist = par.getCTP().getRList();//获取<w:p>标签下的<w:r>list
|
|
|
+ for(int j=0; j < ctrlist.size(); j++) { //遍历r
|
|
|
+ CTR r = ctrlist.get(j);
|
|
|
+
|
|
|
+ List<CTEmpty> breaklist = r.getLastRenderedPageBreakList();//判断是否存在此标签
|
|
|
+ if(breaklist.size() > 0) {
|
|
|
+ num++; //页数添加
|
|
|
+ }
|
|
|
+ }
|
|
|
if (parStyle != null && parStyle.startsWith("Heading")) {
|
|
|
List<CTBookmark> bookmarkList=par.getCTP().getBookmarkStartList();
|
|
|
try {
|
|
|
int level = Integer.parseInt(parStyle.substring("Heading".length()));
|
|
|
|
|
|
//添加标题
|
|
|
- this.addRow(level, par.getText(), 2, bookmarkList.get(0).getName(),block);
|
|
|
+ this.addRow(level, par.getText(), num, bookmarkList.get(0).getName(),block);
|
|
|
} catch (NumberFormatException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
@@ -284,6 +314,9 @@ String[] row =org.split("\\|\\|");
|
|
|
pPr.addNewRPr().addNewNoProof();//不检查语法
|
|
|
CTR run = p.addNewR();
|
|
|
run.addNewRPr().addNewNoProof();
|
|
|
+ for(int i=0;i<level;i++){
|
|
|
+ title="\n"+title;
|
|
|
+ }
|
|
|
run.addNewT().setStringValue(title);//添加标题文字
|
|
|
//设置标题字体
|
|
|
CTRPr pRpr = run.getRPr();
|