Link to home
Start Free TrialLog in
Avatar of awilderbeast
awilderbeastFlag for United Kingdom of Great Britain and Northern Ireland

asked on

c# itextsharp no errors everything fine, pdf just not opening?

Hi all,

this my first time using this dll, all my code seems to be ok compared to samples ive seen but the pdf isnt opening, anyone tell me why?

Thanks
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;


public partial class _CustomerSupport_CV : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void CreatePDF(object sender, EventArgs e)
    {
        Document PDFDoc = new Document();
        try
        {
            string filepath = System.Web.HttpContext.Current.Server.MapPath("_documents/YourCV.pdf");

            PdfWriter.GetInstance(PDFDoc, new FileStream(filepath, FileMode.Create));

            PDFDoc.Open();

            PDFDoc.Add(new Paragraph("Hello There"));

        }
        catch (DocumentException de)
        {
            Errors.Text = de.ToString();
        }
        catch (IOException ioe)
        {
            Errors.Text = ioe.ToString();
        }

        PDFDoc.Close();
    }
}

Open in new window

Avatar of Om Prakash
Om Prakash
Flag of India image

I tried the same code and it seems to be working. i am able to open the pdf that is created.

            iTextSharp.text.Document PDFDoc = new iTextSharp.text.Document();
            string filepath = @"D:\testfile.pdf";
            PdfWriter.GetInstance(PDFDoc, new FileStream(filepath, FileMode.Create));
            PDFDoc.Open();
            PDFDoc.Add(new iTextSharp.text.Paragraph("Hello There"));
            PDFDoc.Close();
Avatar of awilderbeast

ASKER

ok ive seen the problem

the document is edited progrmattically, but its not shown to the user. how can i open the file in the users browser or make it download?

THanks
ASKER CERTIFIED SOLUTION
Avatar of mayank_joshi
mayank_joshi
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
how can i make it open in a new window?

Thanks
thanks
sorry the code posted by me has some errors.
change the code after PDFDoc.Close();
as:-

     Response.ContentType = "application/pdf";
               Response.AddHeader("content-disposition", "attachment;filename=" + filepath);
               Response.Buffer = true;
               Response.Clear();
               Response.WriteFile(filepath, true);
               Response.End();

Open in new window

there is no need of MemoryStream