Click here to Skip to main content
14,265,813 members
Rate this:
Please Sign up or sign in to vote.
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 4 days ago
Comments
F-ES Sitecore 4 days ago
   
The error is self-explanatory?
milepredy 4 days ago
   
Is there a work-around my code? Am I doing the right thing?
Rate this:
Please Sign up or sign in to vote.

Solution 3

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
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[^]
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

Seems, you're on the right track...


//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...
   
Comments
milepredy 4 days ago
   
How is the implementation of your code?
Maciej Los 4 days ago
   
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100