0

I send the following string

string text = <img alt=\"\" src=\"http://localhost:6666/content/userfiles/admin/images/q4.png\" /><br/>

to:

public static Paragraph CreateSimpleHtmlParagraph (String text)
        {

        string fontpath = System.Web.HttpContext.Current.Server.MapPath("~/Content/");
        BaseFont bf = BaseFont.CreateFont(fontpath + "ARIALUNI.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        var f = new Font(bf, 10, Font.NORMAL);
        var p = new Paragraph
        {
            Alignment = Element.ALIGN_LEFT,
            Font = f
        };
        var styles = new StyleSheet();
        styles.LoadTagStyle(HtmlTags.SPAN, HtmlTags.FONTSIZE, "10");
        styles.LoadTagStyle(HtmlTags.BODY, HtmlTags.ENCODING, BaseFont.IDENTITY_H);
        using (var sr = new StringReader(text))
            {
            var elements = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(sr, styles);
            foreach (var e in elements)
                {
                p.Add(e);
                }
            }
        return p;
        }

using:

  document.Add(CreateSimpleHtmlParagraph("<span style='font-size:10;'>" + "<b><u>" +
                  "Notes" + "</u></b>" + ":  " + "<br/><br/>" + text + "</span>"));

to generate PDF using itextsharp, It works very well except the image is too large! Is there a way to check if the string includes width and height and if not add the to scale the image?

hncl
  • 2,295
  • 7
  • 63
  • 129

1 Answers1

0

As Bruno said, please upgrade to XMLWorker.

What you need to do is implement the no longer supported IHTMLTagProcessor interface for the HTML tag that you are interested in. You are interested in the img tag so you'll want to just use basically what iText is already doing but with your own logic. Unfortunately their class is private so you can't just subclass it but you can see its contents here. You'll basically end up with a class like this:

public class MyImageTagProcessor : IHTMLTagProcessor {
    void IHTMLTagProcessor.EndElement(HTMLWorker worker, string tag) {
        //No used
    }

    void IHTMLTagProcessor.StartElement(HTMLWorker worker, string tag, IDictionary<string, string> attrs) {
        if (!attrs.ContainsKey(HtmlTags.WIDTH)) {
            //Do something special here
            attrs.Add(HtmlTags.WIDTH, "400px");
        }

        if (!attrs.ContainsKey(HtmlTags.HEIGHT)) {
            //Do something special here
            attrs.Add(HtmlTags.HEIGHT, "400px");
        }

        worker.UpdateChain(tag, attrs);
        worker.ProcessImage(worker.CreateImage(attrs), attrs);
        worker.UpdateChain(tag);
    }
}

Then in your code create a Dictionary holding the tag that you are targeting and an instance of that class:

var processors = new Dictionary<string, IHTMLTagProcessor>();
processors.Add(HtmlTags.IMG, new MyImageTagProcessor());

Finally, change your parsing call to use one of the overloads. We don't need to fourth parameter (providers) so we're passing null to that.

var elements = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(sr, styles, processors, null);
Chris Haas
  • 53,986
  • 12
  • 141
  • 274