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 |
![]() |
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 |
![]() |
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 |
![]() |
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 |
![]() |
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 |
![]() |
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 |
![]() |
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 |
![]() |
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 |
![]() |
Other pdf files are opening but only the one from this system is not opening.
![]() |
0 |
![]() |
Hello amit.jain,
Thank very much, It is working.
![]() |
0 |
![]() |
Hello amit.jain,
Thank very much, It is working.
![]() |
0 |
![]() |