locked
Open Pdf Document Automatically using iTextSharp. RRS feed

  • Question

  • User381809404 posted

    Hi,

    I am success to create pdf file by using iTextSharp. but now i have to open automatically. and its not working.

    here is my code.

    private void GeneratePDF(string path, string fileName)
        {
            var doc = new Document();
            try
            {
                PdfWriter.GetInstance(doc, new FileStream(path + fileName, FileMode.Create)); doc.Open();
                for (int i = 0; i < L.Count; i++)
                {
                    doc.NewPage();
                    using (TextReader sReader = new StringReader(L[i].ToString()))
                    {
                        List<IElement> list = HTMLWorker.ParseToList(sReader, new StyleSheet());
                        foreach (IElement elm in list) { doc.Add(elm); }
                    }
                }
            }
            catch (Exception ex) { ex.Message.ToString(); }
            finally
            {
                doc.Close();
                OpenPDF(doc); 
            }
        }
    
        private void OpenPDF(Document doc)
        {
            Response.AddHeader("Content-Disposition", "attachment;filename=test.pdf");
            Response.ContentType = "application/pdf";
            Response.End();
        }

    i am getting Error: "Adobe Reader could not open because it is either not a supported file type or because the file has been damaged."

    please help



    Wednesday, July 10, 2013 11:15 AM

Answers

  • User551462331 posted

    I dont see where u have provided file for download.... u  r simply sending blank response with pdf file type....

    change this first

    finally
    {
       doc.Close();
       OpenPDF(path + fileName);
    }

    and then

        private void OpenPDF(string filePath)
        {
            Response.AddHeader("Content-Disposition", "attachment;filename=test.pdf");
            Response.ContentType = "application/pdf";
            Response.TransmitFile(Server.MapPath(filePath));
            Response.End();
        }

    hope this helps...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 10, 2013 2:01 PM