0

I am using Itextsharp to generate PDF while exporting i have a image which is coming from server which is in byte format . It shows Illegal characters in path. please help me.

.aspx

<asp:Image ID="img_dealer" runat="server" Width="150px" Height="150px" />

.cs

 if (dt.Rows[0]["Photo"].ToString().Trim() != null || dt.Rows[0]["Photo"].ToString().Trim() != "")
        {
          Session["Photo"] = (byte[])dt.Rows[0]["Photo"];
          string encrypt_id = Utilities.encrypt(Utilities.convt("3"), ConfigurationManager.AppSettings["AadharEncKey"]);                   
          img_dealer.ImageUrl = "Image.aspx?id=" + encrypt_id + "";
            Session["ImageUrl"] = "Image.aspx?id=" + encrypt_id + "";
        }

private byte[] GetImage(string RegNO)
    {
        if (con.State == ConnectionState.Closed)
            con.Open();            
        string sTSQL = "select Photo from FL_Form_A1 where Reg_No='" + RegNO + "'";
        SqlCommand objCmd = new SqlCommand(sTSQL, con);
        objCmd.CommandType = CommandType.Text;
        object result = objCmd.ExecuteScalar();
        con.Close();
        return (byte[])result;
    } 

protected void export_click(object sender, EventArgs e)
    {
        string strid = Utilities.Decrypt(Request.QueryString["lbid1122"].ToString().Trim().Replace(" ", "+"), ConfigurationManager.AppSettings["AadharEncKey"]);
        string appPath = HttpContext.Current.Request.ApplicationPath;
        govlogo.ImageUrl = Server.MapPath(".") + "\\Image\\kar_logo4.jpg";       
        iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(GetImage(strid));           
        string path = Server.MapPath(appPath + "eSign/eSignfiles/" + strid + ".pdf");
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=" + strid + ".pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);        
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        this.Page.RenderControl(hw);
        dynamic output = new FileStream(path, FileMode.Create);
        StringReader sr = new StringReader(sw.ToString());      
        Document pdfDoc = new Document(PageSize.A4, 88f, 88f, 10f, 10f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, output);
        pdfDoc.Open();
        //pdfDoc.Add(img);
        htmlparser.Parse(sr);
        pdfDoc.Close();
    }
chethu
  • 386
  • 1
  • 3
  • 26
  • I suggest using one of the methods from: [Is there a way of making strings file-path safe in c#?](https://stackoverflow.com/q/333175/1115360) on `strid`. Also, `Response.AddHeader("content-disposition", "attachment;filename=\"" + strid + ".pdf\"");` so that the filename is enclosed in double-quotes. – Andrew Morton Feb 07 '20 at 09:58
  • @AndrewMorton how does it help to my question . my issue is only about image export. If i remove `img_dealer` everything will work fine – chethu Feb 07 '20 at 10:02
  • I imagined that it wasn't letting the user save it due to illegal characters in the filename. So, which line does the error happen on? – Andrew Morton Feb 07 '20 at 10:05
  • @AndrewMorton at `htmlparser.Parse(sr);` – chethu Feb 07 '20 at 10:06
  • When you create the StringReader sr - what value does sw have? – PaulF Feb 07 '20 at 10:11
  • It *could* be a bug in HtmlWorker which will never be fixed: [itextsharp HTMLWorker (deprecated)](https://stackoverflow.com/questions/38851976/itextsharp-htmlworker-deprecated). Can you use the [XmlWorkerHelper](https://www.aspsnippets.com/Articles/iTextSharp-HTMLWorker-is-Obselete-Depreciated-Replacement-of-iTextSharp-HTMLWorker-in-ASPNet-C-and-VBNet.aspx) instead? – Andrew Morton Feb 07 '20 at 10:11
  • @AndrewMorton tried with XmlWorkerHelper error is resolved but not able to display image in PDF – chethu Feb 07 '20 at 10:34
  • Without looking into it very much at all, a quick search brings up things like [rendering images XmlWorkerHelper(itextpdf)](https://stackoverflow.com/a/34487964/1115360), which might have useful information. – Andrew Morton Feb 07 '20 at 10:45
  • Please share the stack trace of the error. – mkl Feb 07 '20 at 11:36

0 Answers0