Itextsharp scale image sent as a string

I am sending the following line

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

at

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

via:

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

to create PDF using itextsharp, it works very well, except that the image is too large! Is there a way to check if a string includes width and height, and if you don't add an image scale?

0
c # itextsharp
Jul 01 '14 at 8:39
source share
1 answer

As Bruno said, upgrade it to XMLWorker.

What you need to do is implement the no longer supported IHTMLTagProcessor interface for the HTML tag that interests you. in the img tag, so you just want to use basically what iText already does, but with your own logic. Unfortunately, their class is private, so you cannot just subclass it, but see its contents here . Basically you get 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 by holding the tag you are targeting and an instance of this class:

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

Finally, change your syntax call to use one of the overloads. We do not need the fourth parameter (providers), so we pass it a zero.

 var elements = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(sr, styles, processors, null); 
0
Jul 01 '14 at 14:10
source share



All Articles