替代过时的 iTextSharp HtmlUtilities?

Replacement for obsolete iTextSharp HtmlUtilities?

每当我使用 iTextSharp.text.html.HtmlUtilities.DecodeColor 时,我都会收到一条验证警告,指出 HtmlUtilities is obsolete。但是,在 https://github.com/itext/itextsharp 搜索代码,我发现他们仍在许多地方使用它。

因此,我假设此 class 没有替代品。有没有任何人知道的计划或有任何其他我应该知道的信息?

正在 the code 寻找 iTextSharp.text.html.HtmlUtilities.DecodeColor:

public static BaseColor DecodeColor(String s) {
    if (s == null)
        return null;
    s = s.ToLowerInvariant().Trim();
    try {
        return WebColors.GetRGBColor(s);
    }
    catch {
        return null;
    }
}

你可以看到它基本上包装了对 WebColors.GetRGBColor 的调用,which is not 标记为已过时。

因此,一个好的选择是直接调用 WebColors.GetRGBColor 以避免警告。或者,您可以将对 DecodeColor 的调用包装在 pragma 语句中:

private static BaseColor GetBaseColor(string value)
{
#pragma warning disable 612, 618
    return iTextSharp.text.html.HtmlUtilities.DecodeColor(value);
#pragma warning restore 612, 618
}

此外,WebColors.GetRGBColor 解码命名颜色以及 html 格式化颜色值( 例如 #AARRGGBB)。如果您只需要命名的颜色,您可以使用 System.Drawing.Color.FromName,如评论中所指出的那样。