使用24.12版本输出的word文档存在空白内容的页面,使用removeBlankPages方法也无法进行删除,有什么方法可以进行判断出文档的最后一页为空白页?
doc_temp1.docx (46.8 KB)
result.docx (24.9 KB)
使用24.12版本输出的word文档存在空白内容的页面,使用removeBlankPages方法也无法进行删除,有什么方法可以进行判断出文档的最后一页为空白页?
doc_temp1.docx (46.8 KB)
result.docx (24.9 KB)
@LXY133 正如我在“result.docx”中看到的,第一页有很多内容,MS Word算法添加了新的页面。我们对此无能为力. 唯一的方法是将内容放在第一页上,例如在“ CNV-seq(染色体检测)知情同意书”段落后更改空格.
在另一种情况下,如果最后一页可能是空的,您可以使用以下代码将其删除:
int lastPageNum = doc.getPageCount() - 1;
Document lastPage = doc.extractPages(lastPageNum - 1, 1);
if (lastPage.getText().trim().isEmpty())
doc.removeBlankPages();
当使用html加入空白word中后出现当前doc_temp1.docx的空白页时有办法进行删除吗?同时用上述代码判断最后一页时,如果主体内容非文本外的对象(除页眉、页脚的内容)如何进行确认页面非空?
@LXY133 我这边的“doc_temp1.docx”文件没有空白页。你能提供更多关于添加HTML的信息吗?
您可以使用以下代码:
int lastPageNum = doc.getPageCount();
Document lastPage = doc.extractPages(lastPageNum - 1, 1);
NodeCollection nodes = lastPage.getFirstSection().getBody().getChildNodes(NodeType.ANY, true);
if (nodes.getCount() == 1 && nodes.get(0).getText().trim().isEmpty())
doc.removeBlankPages();
doc_temp.docx (25.0 KB)
0601-1.docx (26.5 KB)
cnv.zip (4.7 KB)
这个中是带有一个空白页的,主体内容是有html插入到0601的word中,html中zip中
NodeCollection nodes = lastPage.getFirstSection().getBody().getChildNodes(NodeType.ANY, true);
if (nodes.getCount() == 1 && nodes.get(0).getText().trim().isEmpty())
doc.removeBlankPages();
上述代码我试过,但好像无法获取到空白页面的节点对象
@LXY133 例如,您可以使用以下代码来避免空白页:
Document doc = new Document("0601-1.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
String html = FileUtils.readFileToString(new File("cnv.html"), StandardCharsets.UTF_8);
builder.insertHtml(html);
NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
for (Paragraph paragraph : (Iterable<Paragraph>)paragraphs) {
paragraph.getParagraphFormat().setSpaceAfter(0);
}
doc.save("output.docx");