首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OpenXml设置文本大小

OpenXml设置文本大小
EN

Stack Overflow用户
提问于 2014-04-18 09:46:59
回答 1查看 1.6K关注 0票数 0

使用OpenXML导出到word文件,我搜索文本并将其替换为另一个文本。工作守则如下:

代码语言:javascript
复制
Dim body = wdDoc.MainDocumentPart.Document.Body
Dim paras = body.Elements(Of DocumentFormat.OpenXml.Wordprocessing.Paragraph)()
Dim txtOfDoc = body.Elements(Of DocumentFormat.OpenXml.Wordprocessing.Text)()

For Each para In paras
    For Each run In para.Elements(Of DocumentFormat.OpenXml.Wordprocessing.Run)()
        For Each test In run.Elements(Of DocumentFormat.OpenXml.Wordprocessing.Text)()
            If (test.Text.Contains(stringToReplace)) Then
                test.Text = test.Text.Replace(stringToReplace, "newString")
                Exit For
            End If
        Next
    Next
Next

现在我想设置不同字体大小的字符串"newString“.

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-22 08:59:36

我的解决办法是:

代码语言:javascript
复制
Dim body = wdDoc.MainDocumentPart.Document.Body
Dim paras = body.Elements(Of DocumentFormat.OpenXml.Wordprocessing.Paragraph)()
Dim txtOfDoc = body.Elements(Of DocumentFormat.OpenXml.Wordprocessing.Text)()

For Each para In paras
    For Each run In para.Elements(Of DocumentFormat.OpenXml.Wordprocessing.Run)()
        For Each test In run.Elements(Of DocumentFormat.OpenXml.Wordprocessing.Text)()
            If (test.Text.Contains(stringToReplace)) Then

                Dim tbl As New DocumentFormat.OpenXml.Wordprocessing.Table()
                Dim tr As New DocumentFormat.OpenXml.Wordprocessing.TableRow()
                Dim tc As New DocumentFormat.OpenXml.Wordprocessing.TableCell()

                tc.Append(getFontDraft("DRAFT"))
                tc.Append(New DocumentFormat.OpenXml.Wordprocessing.StyleTableCellProperties(New DocumentFormat.OpenXml.Wordprocessing.TableCellVerticalAlignment() With {.Val = DocumentFormat.OpenXml.Wordprocessing.TableVerticalAlignmentValues.Center}))
                tr.Append(tc)

                tbl.Append(tr)

                para.AppendChild(tbl)

                Exit For

            End If
        Next
    Next
Next


Private Function getFontDraft(ByVal textIn As String) As DocumentFormat.OpenXml.Wordprocessing.Paragraph

    Dim body As New DocumentFormat.OpenXml.Wordprocessing.Body()
    Dim paragraph As New DocumentFormat.OpenXml.Wordprocessing.Paragraph()
    Dim run_paragraph As New DocumentFormat.OpenXml.Wordprocessing.Run()
    Dim text_paragraph As New DocumentFormat.OpenXml.Wordprocessing.Text(TextIn)

    Dim RunProperties As DocumentFormat.OpenXml.Wordprocessing.RunProperties = run_paragraph.AppendChild(New DocumentFormat.OpenXml.Wordprocessing.RunProperties())
    Dim Bold As New DocumentFormat.OpenXml.Wordprocessing.Bold
    Bold.Val = OnOffValue.FromBoolean(True)
    Dim fontSize As New DocumentFormat.OpenXml.Wordprocessing.FontSize
    fontSize.Val = "32"
    RunProperties.AppendChild(fontSize)
    RunProperties.AppendChild(Bold)

    run_paragraph.AppendChild(text_paragraph)
    paragraph.Append(run_paragraph)

    getFontDraft = paragraph

End Function
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23151560

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档