HtmlAgilitPack을 사용하여 글꼴 패밀리 및 글꼴 크기 변경

c# html html-agility-pack

문제

주어진 글꼴 군글꼴 크기 를 특정 글꼴 군과 크기로 변경해야합니다. (예 : Times New Romen, 크기 : 12) HtmlAgilityPack을 사용하여 어떻게 수행 할 수 있는지 알고 있습니까?

글꼴 크기는 주어진 html에서 다양한 방법으로 정의 할 수 있습니다. 예 : <Font Size="" tag, <H3> , Style 태그 사용. 따라서 모든 글꼴을 특정 글꼴 크기로 변경해야합니다.

다음은 샘플 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>

수락 된 답변

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;



아래 라이선스: CC-BY-SA with attribution
와 제휴하지 않음 Stack Overflow
이 KB는 합법적입니까? 예, 이유를 알아보십시오.
아래 라이선스: CC-BY-SA with attribution
와 제휴하지 않음 Stack Overflow
이 KB는 합법적입니까? 예, 이유를 알아보십시오.