Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-05-12
7
Medium Priority
?
787 Views
Last Modified: 2013-12-16
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

0
Comment
Question by:awilderbeast
  • 3
  • 3
7 Comments
 
LVL 22

Expert Comment

by:Om Prakash
ID: 35745630
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();
0
 
LVL 1

Author Comment

by:awilderbeast
ID: 35745637
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
0
 
LVL 9

Accepted Solution

by:
mayank_joshi earned 2000 total points
ID: 35745681
 iTextSharp.text.Document PDFDoc = new iTextSharp.text.Document();

        //string filepath = "http://net.cityworks.org.uk/_customer_support/_documents/YourCV.pdf";
        string filepath = System.Web.HttpContext.Current.Server.MapPath("_documents/YourCV.pdf");
        
        PdfWriter.GetInstance(PDFDoc, new FileStream(filepath, FileMode.Create));

        PDFDoc.Open();

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

        PDFDoc.Close();

          MemoryStream ms=new MemoryStream();

  Response.ContentType = "application/pdf";
  Response.AddHeader("content-disposition", "attachment;filename=" + filepath) ;
  Response.Buffer = true;
   Response.Clear();
    Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
    Response.OutputStream.Flush();

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:awilderbeast
ID: 35745699
how can i make it open in a new window?

Thanks
0
 
LVL 1

Author Closing Comment

by:awilderbeast
ID: 35745793
thanks
0
 
LVL 9

Expert Comment

by:mayank_joshi
ID: 35745858
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

0
 
LVL 9

Expert Comment

by:mayank_joshi
ID: 35745876
there is no need of MemoryStream
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question