【问题标题】:How can i change static color to RGB color/custom color?如何将静态颜色更改为 RGB 颜色/自定义颜色?
【发布时间】:2018-04-10 07:14:20
【问题描述】:

我正在使用 openxml(vb.net) 更改 docx 中的字体颜色。如何将静态颜色更改为 RGB 颜色/自定义颜色?下面是我的代码,但不起作用:

  Function AddColor(ByVal isFontColor As Boolean, ByVal text As String) As Run
    Dim runs = New Run()
    If isFontColor = True Then
        Dim props = New RunProperties()
    Dim fontColor = New Color With {.Val = CType(RGB(255, 200, 50), StringValue)}


        props.AppendChild(fontcolor)
        runs.Append(props)
    End If
    runs.Append(New Text(text))
    Return runs
    End Function

原静态色码:

   Function AddColor(ByVal isFontColor As Boolean, ByVal text As String) As Run
    Dim runs = New Run()
    If isFontColor = True Then
        Dim props = New RunProperties()
        Dim fontColor = New Color With
            {.Val = "red"} 'Dark red
        props.AppendChild(fontColor)
        runs.Append(props)
        'Set font color to default color
    Else isFontColor = False
        Dim props = New RunProperties()
        Dim fontColor = New Color With
                {.Val = “default"} 'Default Color
        props.AppendChild(fontColor)
        runs.Append(props)
    End If
    runs.Append(New Text(text))
    Return runs
End Function

【问题讨论】:

  • 嗨,Eric,实际上没有工作的是什么?您可以发布您如何使用该功能吗?干杯
  • 其实我的意思是我想添加颜色使用RGB格式,而不是把“紫色”然后得到紫色,我想用rgb添加颜色

标签: vb.net openxml-sdk


【解决方案1】:

我想我明白你想要什么。阅读后,看起来您可以将十六进制颜色值作为字符串传递给参数,这应该可以! - 希望:)

我知道我使用了 system.drawing.color 参考,这可能不适合您,但如果您愿意,您可以用另一个函数替换我的函数来将 RGB 计算为您自己的 HEX,但这应该可以完成工作很好。。

Function AddColor(ByVal isFontColor As Boolean, ByVal text As String) As Run
    Dim runs = New Run()
    If isFontColor = True Then
        Dim props = New RunProperties()
        Dim fontColor = New Color With {.Val = RGB_To_HEX(255, 200, 50)}
        props.AppendChild(fontColor)
        runs.Append(props)
    End If
    runs.Append(New Text(text))
    Return runs
End Function

Function RGB_To_HEX(r As Integer, g As Integer, b As Integer, Optional a As Integer = 255) As String
    Dim c As System.Drawing.Color = System.Drawing.Color.FromArgb(a, r, g, b)
    Return c.ToArgb().ToString("X6")
End Function

【讨论】:

  • 呃...我刚才测试了,没报错也没出来。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-23
  • 1970-01-01
  • 2019-02-08
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多