Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am able to export the text elements to PDF but Image, glyphicons, buttons not displaying. My major concern for this question is exporting the Image, but if you assist me with the glyphicons and buttons not displaying, I will be most grateful.

Error I get is:
The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.

which points at code:
iTextSharp.text.Image picture = iTextSharp.text.Image.GetInstance(imageURL);


Please help.

What I have tried:

This is the code for selecting the image from the database

string queryimg = "Select `picture` from image where frpid=" + Session["app_id"].ToString();
            command = new MySqlCommand(queryimg, con);
            byte[] bytes = (byte[])command.ExecuteScalar();

            string strBase64 = Convert.ToBase64String(bytes);

            if (strBase64 != null)
            {
                prvimage.ImageUrl = "data:Image/png;base64," + strBase64;
            }
            else
            {
                Response.Write("Must upload an Image");
            }


This is the code for exporting the webform to PDF

using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter hw = new HtmlTextWriter(sw))
            {
                form1.RenderControl(hw);
                StringReader sr = new StringReader(sw.ToString());
                Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
                PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
                pdfDoc.Open();
                string imageURL = prvimage.ImageUrl;
                iTextSharp.text.Image picture = iTextSharp.text.Image.GetInstance(imageURL);
                picture.ScaleToFit(105F, 120F);
                pdfDoc.Add(picture);
                XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
                pdfDoc.Close();
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "attachment;filename=DigiCV.pdf");
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.Write(pdfDoc);
                Response.End();
            }
        }
    }
Posted
Updated 13-Aug-19 4:11am
Comments
F-ES Sitecore 13-Aug-19 6:51am    
The error is self-explanatory?
milepredy 13-Aug-19 6:52am    
Is there a work-around my code? Am I doing the right thing?

Please don't use code which is known to be vulnerable. SQL Injection was identified over 20 years ago and to this day is in the top 10 website hacks
REWRITE: "Select `picture` from image where frpid=" + Session["app_id"]

The proper way to do this would be to parameterize your SQL command
C#
string queryimg = "SELECT `picture` FROM image WHERE frpid=@AppID";
command = new MySqlCommand(queryimg, con);
command.Parameters.AddWithValue("@AppID", Session["app_id"].ToString());
byte[] bytes = (byte[])command.ExecuteScalar();
References:
MySqlParameterCollection.AddWithValue Method[^]
MySqlCommand.Parameters Property[^]
 
Share this answer
 
Seems, you're on the right track...


C#
//get image from BLOB
string queryimg = "Select `picture` from image where frpid=" + Session["app_id"].ToString();
command = new MySqlCommand(queryimg, con);
byte[] bytes = (byte[])command.ExecuteScalar();
//read image from stream
MemoryStream ms = new MemoryStream(bytes);
System.Drawing.Image sdi = System.Drawing.Image.FromStream(ms);
Image img = Image.GetInstance(sdi, ImageFormat.Png);
//add image to pdf
pdfDoc.Add(img);
//further instruction...
 
Share this answer
 
Comments
milepredy 13-Aug-19 7:58am    
How is the implementation of your code?
Maciej Los 13-Aug-19 8:07am    
As it was provided in my answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900