locked
itextsharp image rendering RRS feed

  • Question

  • User-735431710 posted

    Hi all, 

    I am using itextsharp for converting html to pdf, the image gets resized in the rendered pdf, i dont know why this is happening,

    for example i have image in html like <img width="300" height="202" src="http://localhost/images/2004-12-09-0.gif"/>

    and after pdf is generated the image size looks like width around 430px, and height around 270px. i am not scaling images, here is my code can some one help me in this

    i don't want to add the images separately, as the html is dynamically generated, and i got all absolute image paths.


    Document document = new Document(PageSize.A4, 72, 72, 72, 72);
            //Document document = new Document(PageSize.A4, 80, 50, 30, 65);
            StreamReader reader = null;
            try
            {
                string repvalue = Request.QueryString[QueryParameter];
                //Request.QueryString["EQVReport"];
                string repname = String.Format("{0}.pdf", repvalue);
                string appath = Request.ApplicationPath;
                string pathname = String.Format("{0}PDFs\\{1}", Server.MapPath(PortalSettings.HomeDirectory), repname);
                string filename = Server.MapPath(String.Format("{0}{1}{2}/index.html", @appath, urlPath, repvalue));
                //writer - have our own path!!! and see you have write permissions...
                PdfWriter.GetInstance(document, new FileStream(pathname, FileMode.Create));
                document.Open();
                reader = new StreamReader(filename);
                string html = reader.ReadToEnd();
                Regex rxImages = new Regex(" src=\"(?!http|https://)([^\"]*)\"", RegexOptions.IgnoreCase & RegexOptions.IgnorePatternWhitespace);
                //Regex rxImages = new Regex("\<img.+?src=['""](?!http\:\/\/)([^>]*?\>", RegexOptions.IgnoreCase & RegexOptions.IgnorePatternWhitespace);
                MatchCollection mc = rxImages.Matches(html);
                while (mc.Count > 0)
                {
                    Match m = mc[0];
                    html = html.Insert(m.Index + 6, string.Format("http://{0}", Request.Url.Host));
                    mc = rxImages.Matches(html);
                }
                StyleSheet styles = new StyleSheet();
                styles.LoadTagStyle("ol", "leading", "16,0");
                styles.LoadTagStyle("p", "size", "24px");
                styles.LoadTagStyle("p", "face", "times-roman");
                styles.LoadTagStyle("li", "face", "garamond");
                styles.LoadTagStyle("span", "size", "8px");
                styles.LoadTagStyle("body", "font-family", "times new roman");
                styles.LoadTagStyle("body", "font-size", "10px");
                //make an arraylist ....with STRINGREADER since its no IO reading file...
                ArrayList htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(html), styles);
                
                //HtmlParser.Parse(document, html);
                // step 1: creation of a document-object
                //Document document1 = new Document(PageSize.A4, 80, 50, 30, 65);
                //// step 2:
                //// we create a writer that listens to the document
                //// and directs a XML-stream to a file
                //PdfWriter.GetInstance(document, new FileStream("C:\\Websites\\DNN\\test.pdf", FileMode.Create));
                //document1.Open();
                //// step 3: we parse the document
                //HtmlParser.Parse(document1, filename);
                //document1.Close();
                //add the collection to the document
                iTextSharp.text.Image gif = iTextSharp.text.Image.GetInstance("http://www.aviationweek.com/awinimages/AWIN.k.gif");
                gif.ScaleToFit(200f,200f);
                document.Add(gif);
                document.Add(new iTextSharp.text.Paragraph(" "));
                for (int k = 0; k < htmlarraylist.Count; k++)
                {
                    document.Add((IElement)htmlarraylist[k]);
                }

    Document document = new Document(PageSize.A4, 72, 72, 72, 72);   

            StreamReader reader = null;

            try

            {


                string pathname = String.Format("{0}PDFs\\{1}", Server.MapPath(PortalSettings.HomeDirectory), repname);

                string filename = Server.MapPath(String.Format("{0}{1}{2}/index.html", @appath, urlPath, repvalue));

              

                PdfWriter.GetInstance(document, new FileStream(pathname, FileMode.Create));

                document.Open();


                reader = new StreamReader(filename);

                string html = reader.ReadToEnd();


                //make an arraylist ....with STRINGREADER since its no IO reading file...

                ArrayList htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(html), null);

                

                for (int k = 0; k < htmlarraylist.Count; k++)

                {

                    document.Add((IElement)htmlarraylist[k]);

                }

    }

    Wednesday, December 29, 2010 3:27 PM

Answers

  • User-821857111 posted

    You are confusing pixels with Points. iTextSharp doesn't understand pixel widths for images. They are measured in Points, which are 1/72 of an inch. A pixel doesn't have a fixed size.

    http://auctionrepair.com/pixels.html


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 30, 2010 12:19 AM