英文:
Insert empty paragraph using insertHtml
问题
我尝试使用Aspose Words for Java(版本20.6)中的DocumentBuilder和insertHtml方法来填充文档。但是,当我插入空段落时,Word文档会忽略它们。使用insertHtml方法无法插入空段落吗?
示例:
Document doc = new Document("tmp.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToBookmark("Text", false, false);
builder.insertHtml("<p>空段落之前</p><p></p><p>空段落之后</p>", true);
doc.save("C:/TMP/doc.docx");
英文:
I try to fill a document in aspose words java (v.20.6) using DocumentBuilder and insertHtml.
But when i insert empty paragraphs they are ignored in word.
Is it not possible to insert empty paragraphs with insertHtml?
Example:
Document doc = new Document("tmp.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToBookmark("Text", false, false);
builder.insertHtml("<p>Before empty paragraph</p><p></p><p>After empty paragraph</p>", true);
doc.save("C:/TMP/doc.docx");
答案1
得分: 2
空的 HTML 段落在 Aspose.Words 中被忽略,以模仿 MS Word 和浏览器的行为。如果在任何浏览器中打开以下 HTML,您将看不到空的段落。
<html>
<body>
<p>空段落之前</p><p></p><p>空段落之后</p>
</body>
</html>
此外,如果在 MS Word 中打开它,您也会注意到没有空的段落。
因此有两种方法可以保留一个空的段落,要么在您的 HTML 字符串中的空段落中插入不间断空格:
builder.insertHtml("<p>空段落之前</p><p>&nbsp;</p><p>空段落之后</p>", true);
要么使用 DocumentBuilder.writeln 方法:
builder.insertHtml("<p>空段落之前</p>", true);
builder.writeln();
builder.insertHtml("<p>空段落之后</p>", true);
英文:
Empty paragraphs in HTML are ignored by Aspose.Words to mimic MS Word and browsers behavior. If you open the following HTML in any browser you will not see an empty paragraph.
<html>
<body>
<p>Before empty paragraph</p><p></p><p>After empty paragraph</p>
</body>
</html>
Also, if you open it in MS Word you will notice that there is no empty paragraph too.
But if add non-breaking space, the empty paragraph is there.
So there are two options to keep an empty paragraph, either put non-breaking space into the empty paragraph in your HTML string
builder.insertHtml("<p>Before empty paragraph</p><p>&nbsp;</p><p>After empty paragraph</p>", true);
or use DocumentBuilder.writeln method
builder.insertHtml("<p>Before empty paragraph</p>", true);
builder.writeln();
builder.insertHtml("<p>After empty paragraph</p>", true);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论