How to generate PDF report from datagrid using Gios PDF .NET or itextsharp library ?


I have a problem.
I am developing my web application using csharp on visual studio 2003. I am able to generate datagrid without any problem. But I want records on datagrid to be exported on pdf document. Have tried using Gios PDF .NET library, from http://www.codeproject.com/KB/graphics/giospdfnetlibrary.aspx. But the problem I have is when declare in my code using Gios PDF. it is saying that it does not know about Gios PDF. I have tried to put references but not working. How can I do it? So that I use this powerful library. The problem I have is calling the Gios PDF .NET library in my files.

 Apart from from that I have tried itextsharp from  http://sourceforge.net/projects/itextsharp/. I am still facing the same problem. Please advise . All want I want is to export data from datagrid to pdf report.
 Anyone who has worked with these libarary Please help.

0
Mobzam
12/6/2008 1:10:58 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

10 Replies
1985 Views

Similar Articles

[PageSpeed] 17

 Hi read this article about itextsharp

http://www.aspfree.com/c/a/BrainDump/Working-with-iTextSharp/1/

it will surely help u 


Don't say thanks rather mark my reply as "Answer" if it helps you ,Doing so u'll get points too
amiT jaiN

C#.NET Articles
0
amit
12/6/2008 2:21:02 PM

  Hello  amit.jain,
Thanks for the link, But it is not providing information do with exporting information on datagrid to pdf. How can I do it? I need information specify to export information from datagrid to pdf document.

0
Mobzam
12/8/2008 4:14:30 PM

 Hi Mobzam, i've created a sample for you , try it

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
            <Columns>
               
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
            SelectCommand="SELECT [Name], [Location] FROM [Test]"></asp:SqlDataSource>
   
    </div>
        <br />
        <asp:Button ID="btnExport" runat="server" OnClick="btnExport_Click" Text="Export to PDF" />
    </form>
</body>
</html>

 

protected void btnExport_Click(object sender, EventArgs e)
    {
        HtmlForm form = new HtmlForm();
        form.Controls.Add(GridView1);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hTextWriter = new HtmlTextWriter(sw);
        form.Controls[0].RenderControl(hTextWriter);
        string html = sw.ToString();
        Document Doc = new Document();
        PdfWriter.GetInstance(Doc, new FileStream("c:\\AmitJain.pdf", FileMode.Create));
        Doc.Open();
        System.Xml.XmlTextReader xmlReader = new System.Xml.XmlTextReader(new StringReader(html));
        HtmlParser.Parse(Doc, xmlReader);
        Doc.Close();
        ShowPdf("c:\\AmitJain.pdf");
       
    }

    private void ShowPdf(string strS)
    {
        Response.ClearContent();
        Response.ClearHeaders();
        Response.AddHeader("Content-Disposition", "inline;filename=" + strS);
        Response.ContentType = "application/pdf";
        Response.WriteFile(strS);
        Response.Flush();
        Response.Clear();
 
    }

 

Check the sample code attached 

http://rapidshare.com/files/171534687/ExportGridViewToPdf.rar.html

 


Don't say thanks rather mark my reply as "Answer" if it helps you ,Doing so u'll get points too
amiT jaiN

C#.NET Articles
0
amit
12/8/2008 7:55:08 PM

  Hello amit.jain,
 Thank the solution you provided butI still have two problems.  And these are as follows;
First problem is that I can not create or generate same report more than once it is giving the error below. How can do it in such that I can generate the same report more than once.
Then  apart from that I would like the report to be on pc desktop. How can I do that? I have modified the code you gave me a bit  see it below.

Server Error in '/report' Application.
--------------------------------------------------------------------------------

The file 'c:\inetpub\wwwroot\report\Chap0101.pdf' already exists.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.


Exception Details: System.IO.IOException: The file 'c:\inetpub\wwwroot\report\Chap0101.pdf' already exists.

Second problem I can not open the report automatically it is giving the following error. How ca n I ensure that report is opened in a web browser?

The adobe Acrobat\Reader that is running can not be used to view PDF files in a web Browser. Please exit Adobe Acrobat\Reader and try again

 

CODE


        private void Btnupload_Click(object sender, System.EventArgs e)
        {
           
           
            HtmlForm form = new HtmlForm();
            form.Controls.Add(Customergd);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hTextWriter = new HtmlTextWriter(sw);
            form.Controls[0].RenderControl(hTextWriter);
            string html = sw.ToString();
            Document Doc = new Document();
           
           
            PdfWriter.GetInstance(Doc, new FileStream(HttpContext.Current.Server.MapPath("Chap0101.pdf"), FileMode.CreateNew));
            Doc.Open();
            System.Xml.XmlTextReader xmlReader = new System.Xml.XmlTextReader(new StringReader(html));
            HtmlParser.Parse(Doc, xmlReader);
            Doc.Close();
            ShowPdf(HttpContext.Current.Server.MapPath("Chap0101.pdf"));
 
           
        }

        private void ShowPdf(string strS)
        {
            Response.ClearContent();
            Response.ClearHeaders();
            Response.AddHeader("Content-Disposition", "inline;filename=" + strS);
            Response.ContentType = "application/pdf";
            Response.WriteFile(strS);
            Response.Flush();
            Response.Clear();
 
        }

0
Mobzam
12/10/2008 5:55:12 AM

 Hello Mobzam, first check wheter u r able to open any pdf in internet explorer or not ?

2 . change code mentioned in my previous post like this

in btnExport_Click Event  change this line

//PdfWriter.GetInstance(Doc, new FileStream("c:\\AmitJain.pdf", FileMode.Create));

To 

PdfWriter.GetInstance(Doc, new FileStream(Request.PhysicalApplicationPath + "\\AmitJain.pdf", FileMode.Create));

And this line to 

 //ShowPdf("c:\\AmitJain.pdf");

Response.Redirect("~/AmitJain.pdf");

And u'll be able to open the PDF in IE itself 


Don't say thanks rather mark my reply as "Answer" if it helps you ,Doing so u'll get points too
amiT jaiN

C#.NET Articles
0
amit
12/10/2008 7:01:45 AM

 Hello amit.jain,
I changed the code as you advised. It is now opening the pdf file in the web broswer with the same error message as below,

Error

The adobe Acrobat\Reader that is running can not be used to view PDF files in a web Browser. Please exit Adobe Acrobat\Reader and try again

However, It is not saving the file on desktop of pc, but  saving on the server in the same folder with the asp.net pages.
I do not want it to save the document on the same but on the user or client machine.
So how can I solve this problem of opening pdf file with an error and the not to save the report or pdf file on the server?
Thanks in advance

0
Mobzam
12/11/2008 5:56:29 AM

 Hi ur acrobat reader is not configured properly to open pdf in internet explorer

Uninstall it , get a letest version and install it as i've checked the code and it's working fine for me 

Read these for more info about acrobat reader 

http://kb.adobe.com/selfservice/viewContent.do?externalId=328233

http://kb.adobe.com/selfservice/viewContent.do?externalId=328637



Don't say thanks rather mark my reply as "Answer" if it helps you ,Doing so u'll get points too
amiT jaiN

C#.NET Articles
0
amit
12/11/2008 7:45:03 AM

 Other pdf files are opening but only the one from this system is not opening.

0
Mobzam
12/11/2008 2:01:29 PM

 Hello  amit.jain,

Thank very much, It is working.

0
Mobzam
12/12/2008 3:07:35 PM

 Hello  amit.jain,

Thank very much, It is working.

0
Mobzam
12/12/2008 3:10:20 PM
Reply:

Similar Artilces:

How to generate big PDF report generated from datagrid with more than 23 columns using itextsharp library ?
Normal 0 false false false MicrosoftInternetExplorer4 /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-b...

How to add headers and footers to PDF report generated from datagrid using itextsharp library ?
Hello people, How can I add headers and footers to PDF Report genereted  from datagrid using  itextsharp library ? I want the header and footer to have messages. Apart from that hearder should have a logo which is an image. How can I do it or achieve this? ...

How can I ensure that columns on datagrid are same in size both when display and when printed onto a PDF report using itextsharp library?
Normal 0 false false false MicrosoftInternetExplorer4 /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-b...

PDF Creation using Gios PDF Library
I found the following link http://www.codeproject.com/cs/library/giospdfnetlibrary.asp but I have absolutely no idea what to do with it.  I have downloaded the source file but there isn't any documnetation on how to use this.  Has anyone implimented this?  I am very new to ASP.NET and I'm using Visual Studio 2005.Please provide any examples in C#Thanks in advance,Steamaway To clarify I have an Invoice Print page that I want to create a PDF out of. Please provide any examples in C#Thanks in advance,Steamaway Just in case someone is looking for a solutoin I found a...

What is the best .NET pdf library to use?
I have to create a asp.net system that generates pdf files. What is the best .NET pdf library/component to use to do this? Thx Mark Hi Mark, You could check the below link... http://www.colorpilot.com/pdfsample_aspnet.html Hope this should be helping you achieve it. In case you get anything better, please post a message back to me. Tx Cheers, --> Uday...

Generating a PDF file using c#.net
Hello,  I' m having to create  a pdf file at the click of a button. I' m new to this can you please suggest a procedure I could follow or a tutorial I could read. thanks Mythili You would be best advised to use a component to help you with this. It's going to be very hard to do by hand. Google should be able to find you alternatives fairly easily.  check this 3rd party control  HTML to PDF converter. The conversion can be done with only a few lines of code.  and also check this linkMy Blog"Don't be afraid to be wrong; otherwise you'l...

how to convert dynamicaly loading images to pdf using HTML to PDF Converter for .NET
how to convert dynamicaly loading images to pdf using HTML to PDF Converter for .NET Now am converting using how to convert dynamicaly loading images to pdf using HTML to PDF Converter for .NET  tool. But the dynamicaly loading images are not showing in the pdf .     Are you using 3rd party tool or you are using iTextSharp free utility? If it is 3rd party tool, it's always suggested to ask the company which built the tool. Sreedharhttp://www.w3coder.orgweblog http://weblogs.asp.net/skoganti Hi vishnusekhar, As to convert images/douments ...

Sending SMS using vb.net or C#.net using vb.net or c#.net
Hi  My requirement is I hav one csv file with these fields id,mobilenum,messgae,status.intiallu staus is 0. once i read the all fileds and take that mobile number.using tat mobile number  i need to send sms .after sending sms i shuld change status as 1 How to send sms thru coding (please dont provide any links.if it is provide also please give working links becox i checked codeproject .i didnt get any nice link. and also provide the how to update the status field im csv file   Thank ssandhya   To send SMS, you need some third party SMS providers. if you consul...

Aspose.Pdf.Form 1.0 Released!
Aspose.Pdf.Form 1.0 Released! Version 1.0.0.0 Released on January 03 2005 ...

How does .Net generates Pdf ?
Hi, Its said that there is no class or technique in .Net by which we can read or create pdf on fly. I guess many of you must have used Microsoft's Sql Reporting Services. In that  the reportviewer control gives an Option for export the report in pdf and Excel. How does .Net renders the simple html/dataset of report in Pdf ? How can I know the inner working of pdf rendering done by .Net?Thanks and regards,mmk_digs Hi,.NET indeed doesn't support pdf out of the box but you can take a look here: http://search.live.com/results.aspx?q=.NET+pdf+generate&go=Zoek...

PDF generation in .net
    Hi          I am  using Active PDF to generate PDF documents . i am using Adobe writer to create          templates and then use  Active PDF to write content to the template and store it        Active PDF dll can be refered in .net and using it we can generate PDFs . The problem is        that PDF size is very large . Using  Adobe writer we can create Form field in the template and      using  Active PDF dl...

Using PDF with .Net
Hello all,    Is there any way to create, manipulate PDF documents from .Net code?  You would need to use a third party component.  There is no native PDF functionality within the .NET framework. Google "PDF ASP NET" Regards Mike [MVP - ASP/ASP.NET]My site  Thanks Mike.. I found out an open source project which has several pdf functionalities.. u would like to have a look at thishttp://sourceforge.net/projects/pdfsharp/ ...

Datagrid(.net 1.1) to GridView(.net 2.0) and using the RowUpdating event how to extract the values from the controls.
Ok  This is the settings: I use a Viewgrid with only itemtemplate colums for example. <asp:TemplateField HeaderText="Test"><ItemTemplate><asp:Label id="LBL_Test" text='<%# DataBinder.Eval(Container.DataItem, "Test1") %>' runat="server" meta:resourcekey="LBL_TestResource1"></asp:Label></font></ItemTemplate> <EditItemTemplate><asp:TextBox ID="HDN_Test" Text='<%# DataBinder.Eval(Container.DataItem, "Test1") %>' Runat="server" meta:resourcekey="HDN_TestResource1" /></EditItemTemplate></asp:TemplateField>  The ...

Creating MS Access reports as PDF's using .Net Windows Services
I have a windows service running on a Windows 2000 server that opens up an instance of Microsoft Access 2000, saves an access report as a pdf file on the server, and then emails the pdf file to a group of users.  Just recently, the section of the code that creates the physical pdf file stopped working.  The service code was written in vb.net and opens up an instance of MS Access using impersonation of a user that has appropriate rights on the server.  I have recreated this service code as a vb.net console app, and when executing the console .exe, the pdf file is created and ...

Web resources about - How to generate PDF report from datagrid using Gios PDF .NET or itextsharp library ? - asp.net.presentation-controls

Facebook Stack Overflow
Stack Exchange log in - careers - chat - meta - about - faq Questions Tags Users Badges Unanswered Ask Question Top Questions active 5 featured ...

Latest Updates
Latest Updates - Free source code and tutorials for Software developers and Architects.; Updated: 15 Feb 2013

PDFs Using iTextSharp
... libraries on the market now that allow you to create PDF documents from your .NET applications. The one I’ve chosen to use is To use iTextSharp, ...

pdfbinder - A simple tool to merge several PDF documents into one - Google Project Hosting
PDFBinder is a simple tool that lets you merge any number of PDF documents into one, without having to resort to slow, user-unfriendly, expensive ...

Introducing PDF and iText
About us - News - Contact iText ® Licenses Support You are here: Home > Support > Book > Chapter 1 Chapters Chapter 1 Chapter 2 Using iText's ...

Mad Scientist Hut
We supply products for the home electronic enthusiast and small labs

Items by Category on MikesDotnetting
Mikesdotnetting - 15 September 2009 22:16 If you are developing a business application using ASP.NET MVC, and need to produce some kind of dashboard-style ...

How to Generate PDFs in Python for Google App Engine
One of my last projects based on google app engine and python involved storing form data in GAE datastore and generating PDF documents that the ...

Sports-Gazer - About Us
Sports-Gazer is an application for ranking and rating sports teams. It also can project scores for upcoming games. Certain parts of this application ...

anatone.net anatone.net - links
globally recognized as the best site on the internet with the name anatone.net

Resources last updated: 1/19/2016 5:01:35 AM