如何将静态颜色更改为RGB颜色/自定义颜色?

时间:2018-04-10 07:14:20

标签: vb.net openxml-sdk

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

  // Maximum value
  int max = int.MinValue;
  // Second greatest   
  int max2 = int.MinValue;

  // i >= 0: Comparing with 0 is slightly faster then with Count
  for (int i = input.Count - 1; i >= 0; --i) {
    int v = input[i];

    if (v > max) {
      max2 = max;
      max = v;
    }
    else if (v > max2 && v != max)
      max2 = v;
  }

原始静态颜色代码:

  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

1 个答案:

答案 0 :(得分:0)

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

我知道我已经使用了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