使用 gembox 创建 word 文件
Creating word file with gembox
我想用 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. If you want to break the text or insert a tab, insert SpecialCharacter instance with specific SpecialCharacterType to the parent's InlineCollection.'
我该怎么做"break the text or insert a tab"?
更新:
在 GemBox.Document 的当前最新错误修复版本中,情况已不再如此。
Paragraph
的构造函数从现在开始为您处理特殊字符。
但是,请注意 运行 的构造函数和 运行 的文本 属性 没有任何变化,它们仍然不接受特殊字符。
首先注意使用这个Paragraph
的构造函数:
New Paragraph(document, text)
与使用类似下面的内容相同:
Dim paragraph As New Paragraph(document)
Dim run As New Run(document, text)
paragraph.Inlines.Add(run)
问题是 Run
元素不能包含任何特殊字符(参见 Run.Text property remarks)。
这些字符用它们自己的元素表示,因此您需要如下内容:
document.Sections.Add(
New Section(document,
New Paragraph(document,
New Run(document, "Foo"),
New SpecialCharacter(document, SpecialCharacterType.Tab),
New Run(document, "Bar"))))
或者您可以利用 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)
希望对您有所帮助。
我想用 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. If you want to break the text or insert a tab, insert SpecialCharacter instance with specific SpecialCharacterType to the parent's InlineCollection.'
我该怎么做"break the text or insert a tab"?
更新:
在 GemBox.Document 的当前最新错误修复版本中,情况已不再如此。
Paragraph
的构造函数从现在开始为您处理特殊字符。
但是,请注意 运行 的构造函数和 运行 的文本 属性 没有任何变化,它们仍然不接受特殊字符。
首先注意使用这个Paragraph
的构造函数:
New Paragraph(document, text)
与使用类似下面的内容相同:
Dim paragraph As New Paragraph(document)
Dim run As New Run(document, text)
paragraph.Inlines.Add(run)
问题是 Run
元素不能包含任何特殊字符(参见 Run.Text property remarks)。
这些字符用它们自己的元素表示,因此您需要如下内容:
document.Sections.Add(
New Section(document,
New Paragraph(document,
New Run(document, "Foo"),
New SpecialCharacter(document, SpecialCharacterType.Tab),
New Run(document, "Bar"))))
或者您可以利用 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)
希望对您有所帮助。