派作为一个字符串iTextSharp的图像规模 [英] itextsharp scale image sent as a string

查看:149
本文介绍了派作为一个字符串iTextSharp的图像规模的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发送以下字符串

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

要生成使用iTextSharp的PDF,它工作得非常好,除了图像过大!有没有一种方法来检查字符串包括宽度和高度,如果不加缩放图像?

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?

推荐答案

由于布鲁诺说,请升级到XMLWorker。

As Bruno said, please upgrade to XMLWorker.

您需要做的是落实不再支持的 IHTMLTagProcessor ​​接口> HTML标记,你有兴趣,你感兴趣的 IMG 标记,所以你要只使用基本上是iText的是已经在做,但有自己的逻辑。不幸的是他们班是私有的,所以你不能只是继承它,但你可以的此处查看其内容。你会基本结束了这样一个类:

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

这篇关于派作为一个字符串iTextSharp的图像规模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆