修改标题获取

This commit is contained in:
wuchunlei
2024-12-05 14:22:43 +08:00
parent 94682e011a
commit 0240b6fa94

View File

@@ -462,7 +462,22 @@ public class BookServiceImpl extends ServiceImpl<BookDao, BookEntity> implements
BookEntity bookEntity = this.getBaseMapper().selectById(bookId);
String novel = bookEntity.getNovel();
InputStream inputStream = FileDownloadUtil.getInputStream(novel);
XWPFDocument document = new XWPFDocument(inputStream);
InputStream inputStreamCopy = FileDownloadUtil.getInputStream(novel);
List heading1Llist = new ArrayList();
List heading2Llist = new ArrayList();
Document doc = new Document(inputStream);
for (int i = 0; i < doc.getSections().getCount(); i++) {
Section section = doc.getSections().get(i);
for (int j = 0; j < section.getParagraphs().getCount() - 1; j++) {
Paragraph paragraph = section.getParagraphs().get(j);
if (StringUtils.isNotBlank(paragraph.getText())&&paragraph.getStyleName().equals("Heading1")) {
heading1Llist.add(paragraph.getText());
}else if (StringUtils.isNotBlank(paragraph.getText())&&paragraph.getStyleName().equals("Heading2")){
heading2Llist.add(paragraph.getText());
}
}
}
XWPFDocument document = new XWPFDocument(inputStreamCopy);
// 遍历文档中的所有元素(段落和表格)
List<IBodyElement> bodyElements = document.getBodyElements();
String chapterName = "";
@@ -474,7 +489,7 @@ public class BookServiceImpl extends ServiceImpl<BookDao, BookEntity> implements
XWPFParagraph paragraph = (XWPFParagraph) element;
String text = paragraph.getText();
if (text != null && !text.isEmpty()) {//处理段落或正文
if (paragraph.getRuns().get(0).getFontSize()>=topText){//判断是否是顶级标题
if (heading1Llist.contains(text)||paragraph.getRuns().get(0).getFontSize()>=topText){//判断是否是顶级标题
chapterName = text;
BookChapterEntity bookChapter = new BookChapterEntity();
bookChapter.setBookId(bookId);
@@ -493,7 +508,7 @@ public class BookServiceImpl extends ServiceImpl<BookDao, BookEntity> implements
}
}
}
}else if (paragraph.getRuns().get(0).getFontSize()>=secText){//判断是否是二级标题
}else if (heading2Llist.contains(text)||paragraph.getRuns().get(0).getFontSize()>=secText){//判断是否是二级标题
if (StringUtils.isNotBlank(chapterName)){
BookChapterEntity bookChapter = new BookChapterEntity();
bookChapter.setBookId(bookId);