downloaded pdf does not have the pdf icon, but it opens and displays in pdf

Donald Symmons 3,066 Reputation points
2024-01-06T03:11:58.3033333+00:00

In my pdf download, I have a code that downloads a particular section of a web page in pdf. There are images on the page as well, so the pdf renders the images on the page along with other HTML contents. It download though but when I check my download files, the downloaded file does not show the PDF icon. However, upon double-clicking the downloaded file, I will be given options with different applications that can be used to open the pdf file (like Adobe reader, Adobe illustrator, Notepad etc...). May I please know why the icon pdf is not showing on this downloaded file?

If I use the same download code and add code to send to an email, and when the user receives the file and downloads from the email, the downloaded file shows the pdf icon.

Here is what I mean, the icon at the far left is just a blank file icon, it should have the pdf file icon.

pdf image

Could it be that I need to update my itext package libraries from NuGet manager?

protected void Button3_Click(object sender, EventArgs e)
        {
            try
            {
                using (SqlConnection con = new SqlConnection())
                {
                    con.ConnectionString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
                    using (SqlCommand cmd = new SqlCommand("SELECT * FROM TableReceipt WHERE reference =@reference ", con))
                    {
                        cmd.Parameters.AddWithValue("@reference ", lblprefix.Text);
                        con.Open();
                        SqlDataReader dr = cmd.ExecuteReader();
                        if (dr.Read())
                        {
                            byte[] image = (byte[])dr["logo"];
                            imgFileUpload.ImageUrl = "data:image/jpeg;base64," + Convert.ToBase64String(image);

                            byte[] bytes = (byte[])dr["signature"];
                            Image2.ImageUrl = "data:image/jpeg;base64," + Convert.ToBase64String(bytes);

                            File.WriteAllBytes(Server.MapPath("logo.jpg"), image);
                            File.WriteAllBytes(Server.MapPath("sign.jpg"), bytes);
                            Image2.ImageUrl = GetUrl("sign.jpg");
                            imgFileUpload.ImageUrl = GetUrl("logo.jpg");
                        }
                        var ImagUrl = imgFileUpload.ImageUrl;
                        var ImgeUrl = Image2.ImageUrl;

                        StringWriter sw = new StringWriter();
                        HtmlTextWriter hw = new HtmlTextWriter(sw);
                        Panel1.RenderControl(hw);
                        StringReader sr = new StringReader(sw.ToString());
                        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
                        PdfWriter PdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
                        HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
                        htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
                        ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
                        cssResolver.AddCssFile(Server.MapPath("~/css/style2.css"), true);
                        IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(pdfDoc, PdfWriter)));
                        var worker = new XMLWorker(pipeline, true);
                        var xmlParse = new XMLParser(true, worker);
                        pdfDoc.Open();
                        xmlParse.Parse(sr);
                        xmlParse.Flush();
                        pdfDoc.Close();
                        Response.ContentType = "application/pdf";
                        Response.AddHeader("content-disposition", "attachment;filename=" + nameLbl.Text + " Receipt - " + DateTime.Now.ToString("MMM dd, yyyy") + ".pdf");
                        Response.Cache.SetCacheability(HttpCacheability.NoCache);
                        Response.Write(pdfDoc);
                        File.Delete(Server.MapPath("sign.jpg"));
                        File.Delete(Server.MapPath("logo.jpg"));
                        Image2.ImageUrl = ImgeUrl;
                        imgFileUpload.ImageUrl = ImagUrl;
                        Response.End();
                        con.Close();
                    }
                }
            }
            catch (SqlException ex)
            {
                string msg = "Error:";
                msg += ex.Message;
                throw new Exception(msg);
            }
        }
Developer technologies | .NET | Other
Developer technologies | ASP.NET | Other
{count} votes

Accepted answer
  1. Lan Huang-MSFT 30,191 Reputation points Microsoft External Staff
    2024-01-08T07:00:43.0166667+00:00

    Hi @Donald Symmons,

    The reason for this problem is that the file name is wrong, mainly in DateTime format, and it will work normally if the comma is removed.

    Response.AddHeader("content-disposition", "attachment;filename=" + nameLbl.Text + " Receipt - " + DateTime.Now.ToString("MMM dd yyyy") + ".pdf");
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. AgaveJoe 30,126 Reputation points
    2024-01-06T14:16:24.2066667+00:00

    Your screenshot does not show the file extension! By default Windows hides common file extension. Change the view to see the file extension.

    https://support.microsoft.com/en-us/windows/common-file-name-extensions-in-windows-da4a4430-8e76-89c5-59f7-1cdbbc75cb01

    The Windows "Default Apps" associates file extensions with an installed application. Check default app.

    https://support.microsoft.com/en-us/windows/change-default-programs-in-windows-e5d82cad-17d1-c53b-3505-f10a32e1894d

    Lastly, the file extension could be missing which indicates a problem with the code. Simply run your code through the Visual Studio debugger to find where the code does not execute as expected.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.