Visual Studio .NET - Export to PDF - Asked By Lokesh M on 11-Sep-08 01:52 AM

If anybody knows how to export gridview/table data to pdf, please help me...

solution - Perry replied to Lokesh M on 11-Sep-08 01:56 AM

Hi,

Please refer following links you will get everything what you wants.

You could the the techniques in the following article to iterate through the data:

http://www.c-sharpcorner.com/UploadFile/DipalChoksi/exportxl_asp2_dc11032006003657AM/exportxl_asp2_dc.aspx?ArticleID=000c64fb-8a22-414a-8247-984335aaa0eb

And then use the following free library, iTextSharp, to put it into PDF format:

http://itextsharp.sourceforge.net/

That should do it for you.

http://www.dot-net-search.com/export+gridview+to+PDF/

http://geekswithblogs.net/vivek/archive/2006/09/26/92316.aspx

http://forums.asp.net/p/1103953/1684416.aspx

http://www.dotnetspider.com/forum/ViewForum.aspx?ForumId=81855

Regards,

Paresh

Export to PDF - Binny ch replied to Lokesh M on 11-Sep-08 01:59 AM

Public Shared Sub FromGridView(ByVal Grid As GridView, ByVal ReportName As String)

Dim sw As New StringWriter()

Dim htw As New HtmlTextWriter(sw)

Dim page As New Page

Dim form As New HtmlForm

Dim GridStyle As String

page.Controls.Add(form)

form.Controls.Add(Grid)

form.RenderControl(htw)

GridStyle = sw.ToString()

'Set up the response

HttpContext.Current.Response.Clear()

HttpContext.Current.Response.AddHeader(
"content-disposition", "attachment;filename=" & ReportName & Date.Now.ToShortTimeString & ".pdf")

HttpContext.Current.Response.Charset = ""

HttpContext.Current.Response.ContentType = "application/pdf"

'Create pdf document

Dim PdfDocument As New iTextSharp.text.Document(PageSize.A4)

'Create pdf writer, output directly to OutputStream

Dim writer As pdf.PdfWriter = pdf.PdfWriter.GetInstance(PdfDocument, HttpContext.Current.Response.OutputStream)

PdfDocument.Open()

'Create tempfile to hold the HTML:

'Dim tempFile As String = Path.GetTempFileName()

'Using tempwriter As New StreamWriter(tempFile, False)

' tempwriter.Write(GridStyle)

'End Using

Dim _xmlr As New System.Xml.XmlTextReader(New StringReader(GridStyle))

'Parse the HTML into the document

iTextSharp.text.html.HtmlParser.Parse(PdfDocument, _xmlr)

'Cleanup

PdfDocument.Close()

writer.Close()

 

'Delete the tempfile:

'File.Delete(tempFile)

writer = Nothing

PdfDocument = Nothing

HttpContext.Current.Response.[End]()

End Sub

how to export GridView to PDF by using one of the free libraries – iTextSharp.

Binny ch replied to Lokesh M on 11-Sep-08 02:01 AM
using iTextSharp.text;
using iTextSharp.text.pdf;

protected void Page_Load(object sender, EventArgs e)
{
  ExportToPDF();
}

private void ExportToPDF()
{
  Document document = new Document(PageSize.A4, 0, 0, 50, 50);
  System.IO.MemoryStream msReport = new System.IO.MemoryStream();

  try {
    // creation of the different writers
    PdfWriter writer = PdfWriter.GetInstance(document, msReport);

    // we add some meta information to the document
    document.AddAuthor("eJuly");
    document.AddSubject("Export to PDF");

    document.Open();

    iTextSharp.text.Table datatable = new iTextSharp.text.Table(7);

    datatable.Padding = 2;
    datatable.Spacing = 0;

    float[] headerwidths = { 6, 20, 32, 18, 8, 8, 8 };
    datatable.Widths = headerwidths;

    // the first cell spans 7 columns
    Cell cell = new Cell(new Phrase("System Users Report", FontFactory.GetFont(FontFactory.HELVETICA, 16, Font.BOLD)));
    cell.HorizontalAlignment = Element.ALIGN_CENTER;
    cell.Leading = 30;
    cell.Colspan = 7;
    cell.Border = Rectangle.NO_BORDER;
    cell.BackgroundColor = new iTextSharp.text.Color(System.Drawing.Color.Gray);
    datatable.AddCell(cell);

    // These cells span 2 rows
    datatable.DefaultCellBorderWidth = 1;
    datatable.DefaultHorizontalAlignment = 1;
    datatable.DefaultRowspan = 2;
    datatable.AddCell("No.");
    datatable.AddCell(new Phrase("Full Name", FontFactory.GetFont(FontFactory.HELVETICA, 14, Font.NORMAL)));
    datatable.AddCell("Address");
    datatable.AddCell("Telephone No.");

    // This cell spans the remaining 3 columns in 1 row
    datatable.DefaultRowspan = 1;
    datatable.DefaultColspan = 3;
    datatable.AddCell("Just Put Anything");

    // These cells span 1 row and 1 column
    datatable.DefaultColspan = 1;
    datatable.AddCell("Col 1");
    datatable.AddCell("Col 2");
    datatable.AddCell("Col 3");

    datatable.DefaultCellBorderWidth = 1;
    datatable.DefaultRowspan = 1;

    for (int i = 1; i < 20; i++) {
      datatable.DefaultHorizontalAlignment = Element.ALIGN_LEFT;
      datatable.AddCell(i.ToString());
      datatable.AddCell("This is my name.");
      datatable.AddCell("I have a very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very long long address.");
      datatable.AddCell("0123456789");

      datatable.DefaultHorizontalAlignment = Element.ALIGN_CENTER;
      datatable.AddCell("No");
      datatable.AddCell("Yes");
      datatable.AddCell("No");
    }

    document.Add(datatable);
  }
  catch (Exception e) {
    Console.Error.WriteLine(e.Message);
  }

  // we close the document
  document.Close();

  Response.Clear();
  Response.AddHeader("content-disposition", "attachment;filename=Export.pdf");
  Response.ContentType = "application/pdf";
  Response.BinaryWrite(msReport.ToArray());
  Response.End();
}
try this links export to pdf - silna chandra replied to Lokesh M on 11-Sep-08 02:27 AM

hi

http://www.filebuzz.com/fileinfo/39013/DataGridView_Extension.html

http://www.softforall.com/SoftwareDev/ComponentsLibraries/ExportToPDF_.NET_assembly08030696.htm

1. use #develope (http://www.icsharpcode.net/OpenSource/SD/Default.aspx),
> it's an OpenSource (free) IDE for .NET
> 2. use VC# express (
http://msdn.microsoft.com/vcsharp/) a free IDE from
> Microsoft for .NET 2.0
> 3. use iTextSharp (
http://itextsharp.sourceforge.net/) for your PDF?


thanks

silna

Export to PDF - Shailendrasinh Parmar replied to Lokesh M on 11-Sep-08 02:43 AM

See this article you will find what you want

http://www.eggheadcafe.com/community/aspnet/17/10036623/how-to-export-gridview-to.aspx

Hope it helps.

See this to Convert gridview to PDF - Sagar P replied to Lokesh M on 11-Sep-08 02:46 AM

You had to use Crystal Report or any 3rd party tool like pdf.net from tallcomponents to extract the u r datagird data to PDF format.

See these links also;

http://www.dot-net-search.com/export+gridview+to+pdf/

http://www.pdfsharp.com/PDFsharp/index.php?option=com_frontpage&Itemid=1

check this article...

http://www.codeproject.com/KB/graphics/giospdfnetlibrary.aspx

OR

http://geekswithblogs.net/vivek/archive/2006/09/26/92316.aspx

Many times we would like to export our page as an Excel sheet, Word doc or PDF file. This can be simply achieved by changing the ContentType of the Response object and overriding the RenderControl method of the control to be exported:

Page Load()
{
 //bind data to data bound controls and do other stuff

Response.Clear(); //this clears the Response of any headers or previous output
Response.Buffer = true; //make sure that the entire output is rendered simultaneously

///
///Set content type to MS Excel sheet
///Use "application/msword" for MS Word doc files
///"application/pdf" for PDF files
///

Response.ContentType = "application/vnd.ms-excel";
StringWriter stringWriter = new StringWriter(); //System.IO namespace should be used

HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);

///
///Render the entire Page control in the HtmlTextWriter object
///We can render individual controls also, like a DataGrid to be
///exported in custom format (excel, word etc)
///
this.RenderControl(htmlTextWriter);
Response.Write(stringWriter.ToString());
Response.End();
} //end page load

For exporting ASP.NET pages as PDF files, you need to know the Adobe PDF specs for generating the PDf correctly. There is already a wonderful component which does the same and its free! See this article along with sample source code on how to export a page to PDF:

http://www.codeproject.com/cs/library/giospdfnetlibrary.asp

http://itextsharp.sourceforge.net/tutorial/index.html

Hope this would be helpful...!

Best Luck!!!!!!!!!!!!!!!
Sujit.

regarding namespace - shekhar kumar replied to Binny ch on 11-Sep-08 03:10 AM
You have used two namespace using iTextSharp.text; using iTextSharp.text.pdf; Will I have to add any reference or any pdf to be installed for these namespace. Thanks Shekhar
ya u have to download this dll Itextsharp - Binny ch replied to Lokesh M on 11-Sep-08 03:49 AM
http://sourceforge.net/projects/itextsharp/
This link will help you download the the dll.

Tell if it works.
thanks it worked - shekhar kumar replied to Binny ch on 11-Sep-08 05:28 AM
thanks a lot for the url. It helped me a lot.
Now what namespace for Document. - shekhar kumar replied to Binny ch on 11-Sep-08 05:41 AM
Server Error in '/UnaidsMisNew' Application. Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: CS0246: The type or namespace name 'Document' could not be found (are you missing a using directive or an assembly reference?) Source Error: Line 186: if (GridView1.Visible == true) Line 187: { Line 188: Document document = new Document(PageSize.A4, 0, 0, 50, 50); Line 189: System.IO.MemoryStream msReport = new System.IO.MemoryStream(); Line 190: this is the error I am getting
It is woking. - shekhar kumar replied to shekhar kumar on 11-Sep-08 06:52 AM
I was not using the assembly properly so I was getting the error. Now it is working very fine. Thanks a lot for the code. Shekhar
Did u get it - Binny ch replied to Lokesh M on 11-Sep-08 07:11 AM
end of post
Did u get it - Binny ch replied to shekhar kumar on 11-Sep-08 07:12 AM
Did u get it
Thanks - shekhar kumar replied to Binny ch on 11-Sep-08 07:14 AM
Yes I get the solution. Thanks again shekhar
:) ur welcome - Binny ch replied to shekhar kumar on 11-Sep-08 08:38 AM
end of post
Exporting to PDF - Lokesh M replied to Sagar P on 12-Sep-08 01:16 AM

Hi, Below code is working fine.. all the data exported to pdf from Gridview/Table, and there are 4 columns But for first column occupies half of the document, rests are displaying in another half. Same problem if i export html table also..

Please suggest me

Code:

Dim sw As New StringWriter()

Dim htw As New HtmlTextWriter(sw)

Dim page As New Page

Dim form As New HtmlForm

Dim GridStyle As String

page.Controls.Add(form)

'form.Controls.Add(chkPDF)

form.Controls.Add(testPDF)

form.RenderControl(htw)

GridStyle = sw.ToString()

'Set up the response

HttpContext.Current.Response.Clear()

HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" & "pdfRep" & Date.Now.ToShortTimeString & ".pdf")

HttpContext.Current.Response.Charset = ""

HttpContext.Current.Response.ContentType = "application/pdf"

'Create pdf document

Dim PdfDocument As New iTextSharp.text.Document(PageSize.A4)

'Create pdf writer, output directly to OutputStream

Dim writer As pdf.PdfWriter = pdf.PdfWriter.GetInstance(PdfDocument, HttpContext.Current.Response.OutputStream)

PdfDocument.Open()

Dim _xmlr As New System.Xml.XmlTextReader(New StringReader(GridStyle))

'Parse the HTML into the document

iTextSharp.text.html.HtmlParser.Parse(PdfDocument, _xmlr)

'Cleanup

PdfDocument.Close()

writer.Close()

writer = Nothing

PdfDocument = Nothing

HttpContext.Current.Response.[End]()

Cell Height - shekhar kumar replied to Binny ch on 12-Sep-08 07:08 AM
Can we set the height of the cell. Thanks shekhar
Is it the Height Of A Tablerow u need Shekar.'ve forgot the posting i sent u earlier
Binny ch replied to shekhar kumar on 12-Sep-08 01:16 PM

If yes, here it is

<table height="100%" cellpadding="0" cellspacing="0">
<tr>
<td height="##" colspan="2">
HEAD CELL
</td>
</tr>
<tr>
<td valign="top" width="##">
CONTENT LISTING CELL
</td>
<td valign="top">
MAIN CONTENT CELL
</td>
</tr>
</table>

ok ok - Binny ch replied to shekhar kumar on 12-Sep-08 01:23 PM

Go through this if it helps, sorry for the previous post its not relevant to ur requirement,Actually i forgot ur reqiuremnt.

http://bytes.com/forum/thread498919.html also this

http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework.windowsforms/topic50029.aspx.

To auto resize:

After populating the grid, this is what I did in a similar situation:

for (int i = 0; i < this.dataGridView1.Rows.Count; i++)

{

this.dataGridView1.AutoResizeRow(i);

}

Regarding the cell of pdf documet - shekhar kumar replied to Binny ch on 13-Sep-08 01:00 PM

Thanks,

But my question was regarding the cells which have been added to the pdf document.

 iTextSharp.text.Table datatable = new iTextSharp.text.Table(7);

    datatable.Padding = 2;
    datatable.Spacing = 0;

    float[] headerwidths = { 6, 20, 32, 18, 8, 8, 8 };
    datatable.Widths = headerwidths;

    // the first cell spans 7 columns
    Cell cell = new Cell(new Phrase("System Users Report", FontFactory.GetFont(FontFactory.HELVETICA, 16, Font.BOLD)));

How cant it be set.

Shekhar