当前位置: 首页 > 知识库问答 >
问题:

使用HtmlAgilitPack更改字体系列和字体大小

毋炳
2023-03-14

我需要将给定HTML的字体家族和字体大小更改为特定的字体家族和大小。(例如:Times New Romen,尺寸:12)你知道如何使用Htmlagilitypack来完成吗?

字体大小可以在给定的HTML中以多种方式定义。例如:使用,也使用样式标记。因此,我需要改变所有特定的字体大小。

    <html><H3 style="MARGIN: 0in 0in 0pt 0.5in"><SPAN style="mso-bidi-font-family: &#39;Tw Cen MT Condensed Extra Bold&#39;; mso-fareast-font-family: &#39;Tw Cen MT Condensed Extra Bold&#39;"><SPAN style="mso-list: Ignore"><FONT size="5" face="Tw Cen MT Condensed Extra Bold">1.1.1</FONT><SPAN style="FONT: 7pt &#39;Times New Roman&#39;">&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT size="5" face="Tw Cen MT Condensed Extra Bold">Sample text1: The following code iterates through all the items in the ListBox and addsPictureBoxes dynamically to a FlowLayoutPanel using the image sources retrieved in the previous step.</FONT></H3>
<P style="MARGIN: 0in 0in 0pt" class="MsoNormal"><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /?></P>
<H3 style="MARGIN: 0in 0in 0pt 0.5in"><SPAN style="mso-bidi-font-family: &#39;Tw Cen MT Condensed Extra Bold&#39;; mso-fareast-font-family: &#39;Tw Cen MT Condensed Extra Bold&#39;"><SPAN style="mso-list: Ignore"><FONT size="5" face="Tw Cen MT Condensed Extra Bold">1.1.2</FONT><SPAN style="FONT: 7pt &#39;Times New Roman&#39;">&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT size="5" face="Tw Cen MT Condensed Extra Bold">Sample text 2: The following code iterates through all the items in the ListBox and addsPictureBoxes dynamically to a FlowLayoutPanel using the image sources retrieved in the previous step.</FONT></H3>
<P style="TEXT-INDENT: -0.25in; MARGIN: 0in 0in 0pt 0.5in; mso-list: l0 level1 lfo2; tab-stops: list .5in" class="MsoNormal"><SPAN style="FONT-FAMILY: &#39;Bauhaus 93&#39;; FONT-SIZE: 20pt; mso-bidi-font-family: &#39;Bauhaus 93&#39;; mso-fareast-font-family: &#39;Bauhaus 93&#39;"><SPAN style="mso-list: Ignore">a.<SPAN style="FONT: 7pt &#39;Times New Roman&#39;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="FONT-FAMILY: &#39;Bauhaus 93&#39;; FONT-SIZE: 20pt">Sample text 3: The following code iterates through all the items in the ListBox and addsPictureBoxes dynamically to a FlowLayoutPanel using the image sources retrieved in the previous step.</SPAN></P>
<P style="MARGIN: 0in 0in 0pt 0.25in" class="MsoNormal"><SPAN style="FONT-FAMILY: &#39;Bauhaus 93&#39;; FONT-SIZE: 20pt"></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 0.5in" class="MsoNormal"><SPAN style="FONT-FAMILY: &#39;Bradley Hand ITC&#39;; FONT-SIZE: 18pt">Sample Text 4: The following code iterates through all the items in the ListBox and addsPictureBoxes dynamically to a FlowLayoutPanel using the image sources retrieved in the previous step.</SPAN></P></html>

共有1个答案

柯振濂
2023-03-14

我是使用HtmLagilitypack完成的。下面是我开发的代码。

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(inputHtml);
var elementsWithStyleAttribute = doc.DocumentNode.SelectNodes(string.Concat("//", tagName));

if (null == elementsWithStyleAttribute)
{
    return inputHtml;
}

foreach (var element in elementsWithStyleAttribute)
{
    var classElement = element.GetAttributeValue("class", null);

    if (!string.IsNullOrWhiteSpace(classElement))
    {
        // Remove class attribute.
        element.Attributes["class"].Remove();
    }

    var styles = element.GetAttributeValue("style", null);

    if (!string.IsNullOrWhiteSpace(styles)) //&& (styles.ToUpper().Contains("FONT-FAMILY:") || styles.ToUpper().Contains("FONT-SIZE:")))
    {
        element.Attributes["style"].Remove();

        string[] splitter = { ";" };
        string[] styleClasses = styles.Split(splitter, StringSplitOptions.None);

        StringBuilder sbStyles = new StringBuilder("font-family:Arial; font-size:10pt;");

        if (null != styleClasses && styleClasses.Length > 0)
        {
            foreach (var item in styleClasses)
            {
                if (!string.IsNullOrWhiteSpace(item) && !item.ToUpper().Contains("FONT-FAMILY:")
                    && !item.ToUpper().Contains("FONT-SIZE:") && !item.ToUpper().Contains("FONT:"))
                {
                    // Add existing styles except font size and font family styles.
                    sbStyles.Append(item.Trim());
                    sbStyles.Append(";");
                }
            }
        }

        element.SetAttributeValue("style", sbStyles.ToString());
    }
    else
    {
        element.SetAttributeValue("style", "font-family:Arial; font-size:10pt;");
    }
}

return doc.DocumentNode.InnerHtml;
 类似资料:
  • 除了这个问题:更改超文本标记语言电子邮件正文字体类型和大小在VBA,我想知道如何才能改变字体大小 当我从代码中删除字体大小时,它确实改变了字体系列。 谢谢你的协助,

  • 所以我开始了一个个人简历网站,我偶然发现了一个非常酷的东西:font awesome,它以文本的形式提供图形,允许你通过CSS添加字体效果。我的问题是一切都很好,直到我试图改变字体大小,无论什么原因,它就是不会改变。你有什么想法吗?我也是新的这里,我已经阅读通过如何使帖子,但如果我做错了,请让我知道。

  • 我想把我的字体大小从9改为9。但它会弹出“编译错误:预期表达式”。

  • 问题内容: 我打算在微调器中将文本字体更改为arialbold。以下是我的代码:- 问题答案: 类: 版面: my_spinner_style.xml TTF文件

  • 问题内容: 您可以更改JOptionPane的字体和文本大小吗?我尝试了它,只有当我在那个特定的Java类上“运行文件”时,它才起作用。如果启动整个项目,则不会更改字体。我只想更改一个特定的JOptionPane而不是全部。 这是代码: 问题答案: 真的很简单。JOption窗格不仅接受字符串,还接受组件。因此,您可以创建一个标签集并设置其字体并将其用作消息。 我不明白为什么以前没人回答这个问题

  • 本文向大家介绍iOS更改字体,包括了iOS更改字体的使用技巧和注意事项,需要的朋友参考一下 示例 Swift Objective-C