0

Frenz,

I used the below code to export to PDF format from my ASP.NET application.
However while opening the generated PDF file, it says the file has corrupted.
Please let me know if I coded anything wrong, or I need at add/remove any properties in the code, Or if there is any other reasons.

Response.Clear() 
            Response.Buffer = True 
            Response.ContentType = "application/pdf" 
            Response.ContentEncoding = System.Text.Encoding.UTF7 
            Response.AddHeader("Content-Disposition", "attachment;filename=Estimates.pdf") 
            Response.Cache.SetCacheability(HttpCacheability.NoCache) 
            Response.Charset = "" 
            Me.EnableViewState = False 
            Response.Write(objStrWriter.ToString()) 
            Response.End()

In this code, objStrWriter is a StringWriter object which contains a HTML code as string.....

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by textbox
0

I think your encoding is incorrect. Here is an example I found while looking around if you want to specify encoding:

string pdfpath = e.CommandArgument.ToString();
            Response.Clear();
            Response.Charset = "utf-8";
            Response.Buffer = true;
            this.EnableViewState = false;
            Response.ContentEncoding = System.Text.Encoding.UTF8;      
            Response.ContentType = "application/pdf";
            Response.WriteFile(pdfpath);
            Response.Flush();
            Response.Close();
            Response.End();

I don't actually set the charset and I just send it down (probably not the best idea):

if (acct != null)
            {
              downloading = true;
              List<AcctInfo> lst = new List<AcctInfo>();
              lst.Add(acct);
              using (MemoryStream ms = new MemoryStream())
              {
                lst.SaveToStream(ms);
                ms.Position = 0;
                ms.Seek(0, SeekOrigin.Begin);

                Response.ContentType = MimeType(".pdf");
                Response.AddHeader("Content-Disposition", "attachment; filename = " + prof.AcctProfileId.ToString("F0") + ".omd");
                Response.AddHeader("Content-Length", ms.Length.ToString());
                Response.BinaryWrite(ms.ToArray());
                Response.Flush();
                return;
              }
            }
    public static string MimeType(string Extension)
    {
      string mime = "application/octetstream";
      if (string.IsNullOrEmpty(Extension))
        return mime;

      string ext = Extension.ToLower();
      Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
      if (rk != null && rk.GetValue("Content Type") != null)
        mime = rk.GetValue("Content Type").ToString();
      return mime;
    }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.