简体   繁体   English

用gembox创建Word文件

[英]Creating word file with gembox

I want to create a Word file with GemBox.Document, but I get an InvalidOperationException : 我想用GemBox.Document创建一个Word文件,但是得到一个InvalidOperationException

Dim text As String = "Foo" + vbTab + "Bar"
Dim document As New DocumentModel()

document.Sections.Add(
    New Section(document,
        New Paragraph(document, text)))

System.InvalidOperationException: 'Text cannot contain new line ('\\n'), carriage return ('\\r') or tab ('\\t') characters. System.InvalidOperationException:'文本不能包含换行符('\\ n'),回车符('\\ r')或制表符('\\ t')。 If you want to break the text or insert a tab, insert SpecialCharacter instance with specific SpecialCharacterType to the parent's InlineCollection.' 如果要中断文本或插入选项卡,请将具有特定SpecialCharacterType的SpecialCharacter实例插入父级的InlineCollection。

How can I do that "break the text or insert a tab" ? 我该如何“破坏文本或插入标签”

UPDATE: 更新:

In the current latest bug fix version for GemBox.Document this is no longer the case. 在GemBox.Document的当前最新错误修复版本中,情况不再如此。
That Paragraph 's constructor from now own handles the special characters for you. 从现在起,该Paragraph的构造函数将为您处理特殊字符。

However, note that nothing has changed regarding the Run's constructor and Run's Text property, they still do not accept special characters. 但是,请注意,关于Run的构造函数和Run的Text属性没有任何更改,它们仍然不接受特殊字符。


First note that using this Paragraph 's constructor: 首先请注意使用此 Paragraph的构造函数:

New Paragraph(document, text)

Is the same as using something like the following: 与使用以下内容相同:

Dim paragraph As New Paragraph(document)
Dim run As New Run(document, text)
paragraph.Inlines.Add(run)

And the problem is that Run element cannot contain any special characters (see Run.Text property remarks ). 问题是Run元素不能包含任何特殊字符(请参见Run.Text属性备注 )。
Those characters are represented with their own elements so you need something like the following: 这些字符用它们自己的元素表示,因此您需要以下内容:

document.Sections.Add(
    New Section(document,
        New Paragraph(document,
            New Run(document, "Foo"),
            New SpecialCharacter(document, SpecialCharacterType.Tab),
            New Run(document, "Bar"))))

Or alternativaly you could take advantage of LoadText method which can handle those special characters for you, like the following: 或者,您可以利用LoadText方法,该方法可以为您处理那些特殊字符,如下所示:

Dim text As String = "Foo" + vbTab + "Bar" + vbNewLine + "Sample"
Dim document As New DocumentModel()

Dim section As New Section(document)
document.Sections.Add(section)

Dim paragraph As New Paragraph(document)
paragraph.Content.LoadText(text)
section.Blocks.Add(paragraph)

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM