插入空段落使用 insertHtml

huangapple go评论61阅读模式
英文:

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(&quot;tmp.docx&quot;);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToBookmark(&quot;Text&quot;, false, false);
builder.insertHtml(&quot;&lt;p&gt;Before empty paragraph&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;After empty paragraph&lt;/p&gt;&quot;, true);
doc.save(&quot;C:/TMP/doc.docx&quot;);

答案1

得分: 2

空的 HTML 段落在 Aspose.Words 中被忽略,以模仿 MS Word 和浏览器的行为。如果在任何浏览器中打开以下 HTML,您将看不到空的段落。

<html>
<body>
    <p>空段落之前</p><p></p><p>空段落之后</p>
</body>
</html>

此外,如果在 MS Word 中打开它,您也会注意到没有空的段落。
插入空段落使用 insertHtml

但如果添加不间断空格,空的段落就存在了。
插入空段落使用 insertHtml

因此有两种方法可以保留一个空的段落,要么在您的 HTML 字符串中的空段落中插入不间断空格:

builder.insertHtml("<p>空段落之前</p><p>&amp;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.

&lt;html&gt;
&lt;body&gt;
    &lt;p&gt;Before empty paragraph&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;After empty paragraph&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;

Also, if you open it in MS Word you will notice that there is no empty paragraph too.
插入空段落使用 insertHtml

But if add non-breaking space, the empty paragraph is there.
插入空段落使用 insertHtml

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(&quot;&lt;p&gt;Before empty paragraph&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;After empty paragraph&lt;/p&gt;&quot;, true);

or use DocumentBuilder.writeln method

builder.insertHtml(&quot;&lt;p&gt;Before empty paragraph&lt;/p&gt;&quot;, true);
builder.writeln();
builder.insertHtml(&quot;&lt;p&gt;After empty paragraph&lt;/p&gt;&quot;, true);

huangapple
  • 本文由 发表于 2020年9月30日 21:29:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64138670.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定