itextsharp scale image sent as a string

谁都会走 提交于 2019-12-25 03:32:54

问题


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?


回答1:


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);


来源:https://stackoverflow.com/questions/24505866/itextsharp-scale-image-sent-as-a-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!