Export Grid to PDF

  1. #1

    Export Grid to PDF

    Further to

    EXT.Net Examples Website: http://examples1.ext.net/#/GridPanel...Data_PostBack/
    methode's Concept: http://forums.ext.net/showthread.php?1395-PDF-Export

    This is an example of how GridPanel's data can be exported to PDF.

    VB

        Protected Sub ToPdf(ByVal sender As Object, ByVal e As EventArgs)
            Dim PDFData As New System.IO.MemoryStream()
            Dim newDocument As New iTextSharp.text.Document(PageSize.A4.Rotate(), 10, 10, 10, 10)
            Dim newPdfWriter As iTextSharp.text.pdf.PdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(newDocument, PDFData)
            Dim newDataSet As DataSet = Nothing
    
            Dim json As String = GridData.Value.ToString()
            Dim eSubmit As New StoreSubmitDataEventArgs(json, Nothing)
            Dim xml As XmlNode = eSubmit.Xml
    
            If xml IsNot Nothing Then
                Dim xtr As New XmlTextReader(xml.OuterXml, XmlNodeType.Element, Nothing)
                newDataSet = New DataSet()
                newDataSet.ReadXml(xtr)
    
                Dim totalColumns As Integer = newDataSet.Tables(0).Columns.Count
                Dim newPdfTable As New iTextSharp.text.pdf.PdfPTable(totalColumns)
    
                newPdfTable.DefaultCell.Padding = 4
                newPdfTable.WidthPercentage = 100
                newPdfTable.DefaultCell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT
                newPdfTable.DefaultCell.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE
                newPdfTable.HeaderRows = 1
    
                newPdfTable.DefaultCell.BorderWidth = 1
                newPdfTable.DefaultCell.BorderColor = New iTextSharp.text.BaseColor(193, 211, 236)
                newPdfTable.DefaultCell.BackgroundColor = New iTextSharp.text.BaseColor(255, 255, 255)
    
                For i As Integer = 0 To totalColumns - 1
                    newPdfTable.AddCell(New Phrase(newDataSet.Tables(0).Columns(i).ColumnName, FontFactory.GetFont("Tahoma", 10, Font.BOLD, New iTextSharp.text.BaseColor(21, 66, 157))))
                Next
    
                For Each record As DataRow In newDataSet.Tables(0).Rows
                    For i As Integer = 0 To totalColumns - 1
                        newPdfTable.DefaultCell.BorderColor = New iTextSharp.text.BaseColor(193, 211, 236)
                        newPdfTable.AddCell(New Phrase(record(i).ToString, FontFactory.GetFont("Tahoma", 9, Font.NORMAL, New iTextSharp.text.BaseColor(80, 80, 80))))
                    Next
                Next
    
                newDocument.Open()
                newDocument.Add(New Phrase(Environment.NewLine))
                newDocument.Add(New Phrase(Environment.NewLine))
                newDocument.Add(New Phrase("Title", FontFactory.GetFont("Trebuchet MS", 18, Font.BOLD, New iTextSharp.text.BaseColor(21, 66, 157))))
                newDocument.Add(newPdfTable)
                newDocument.Add(New Phrase(Environment.NewLine))
                newDocument.Add(New Phrase("Printed On: " & DateTime.Now.ToString(), FontFactory.GetFont("Tahoma", 9, Font.NORMAL, New iTextSharp.text.BaseColor(80, 80, 80))))
                newDocument.Close()
    
                Response.ContentType = "application/pdf"
                Response.Cache.SetCacheability(System.Web.HttpCacheability.[Public])
                Response.AppendHeader("Content-Type", "application/pdf")
                Response.AppendHeader("Content-Disposition", "attachment; filename=" & Me.ID & ".pdf")
                Response.OutputStream.Write(PDFData.GetBuffer(), 0, PDFData.GetBuffer().Length)
                Response.OutputStream.Flush()
                Response.OutputStream.Close()
            End If
        End Sub
    CSharp
    protected void ToPdf(object sender, EventArgs e)
    {
    	System.IO.MemoryStream PDFData = new System.IO.MemoryStream();
    	iTextSharp.text.Document newDocument = new iTextSharp.text.Document(PageSize.A4.Rotate(), 10, 10, 10, 10);
    	iTextSharp.text.pdf.PdfWriter newPdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(newDocument, PDFData);
    	DataSet newDataSet = null;
    
    	string json = GridData.Value.ToString();
    	StoreSubmitDataEventArgs eSubmit = new StoreSubmitDataEventArgs(json, null);
    	XmlNode xml = eSubmit.Xml;
    
    	if (xml != null) {
    		XmlTextReader xtr = new XmlTextReader(xml.OuterXml, XmlNodeType.Element, null);
    		newDataSet = new DataSet();
    		newDataSet.ReadXml(xtr);
    
    		int totalColumns = newDataSet.Tables[0].Columns.Count;
    		iTextSharp.text.pdf.PdfPTable newPdfTable = new iTextSharp.text.pdf.PdfPTable(totalColumns);
    
    		newPdfTable.DefaultCell.Padding = 4;
    		newPdfTable.WidthPercentage = 100;
    		newPdfTable.DefaultCell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
    		newPdfTable.DefaultCell.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE;
    		newPdfTable.HeaderRows = 1;
    
    		newPdfTable.DefaultCell.BorderWidth = 1;
    		newPdfTable.DefaultCell.BorderColor = new iTextSharp.text.BaseColor(193, 211, 236);
    		newPdfTable.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(255, 255, 255);
    
    		for (int i = 0; i <= totalColumns - 1; i++) {
    			newPdfTable.AddCell(new Phrase(newDataSet.Tables[0].Columns[i].ColumnName, FontFactory.GetFont("Tahoma", 10, Font.BOLD, new iTextSharp.text.BaseColor(21, 66, 157))));
    		}
    
    		foreach (DataRow record in newDataSet.Tables[0].Rows) {
    			for (int i = 0; i <= totalColumns - 1; i++) {
    				newPdfTable.DefaultCell.BorderColor = new iTextSharp.text.BaseColor(193, 211, 236);
    				newPdfTable.AddCell(new Phrase(record[i].ToString(), FontFactory.GetFont("Tahoma", 9, Font.NORMAL, new iTextSharp.text.BaseColor(80, 80, 80))));
    			}
    		}
    
    		newDocument.Open();
    		newDocument.Add(new Phrase(Environment.NewLine));
    		newDocument.Add(new Phrase(Environment.NewLine));
    		newDocument.Add(new Phrase("Title", FontFactory.GetFont("Trebuchet MS", 18, Font.BOLD, new iTextSharp.text.BaseColor(21, 66, 157))));
    		newDocument.Add(newPdfTable);
    		newDocument.Add(new Phrase(Environment.NewLine));
    		newDocument.Add(new Phrase("Printed On: " + DateTime.Now.ToString(), FontFactory.GetFont("Tahoma", 9, Font.NORMAL, new iTextSharp.text.BaseColor(80, 80, 80))));
    		newDocument.Close();
    
    		Response.ContentType = "application/pdf";
    		Response.Cache.SetCacheability(System.Web.HttpCacheability.Public);
    		Response.AppendHeader("Content-Type", "application/pdf");
    		Response.AppendHeader("Content-Disposition", "attachment; filename=" + this.ID + ".pdf");
    		Response.OutputStream.Write(PDFData.GetBuffer(), 0, PDFData.GetBuffer().Length);
    		Response.OutputStream.Flush();
    		Response.OutputStream.Close();
    	}
    }

    About Attachment
    A sample project is attached along with this thread which shows the described method of exporting Gridpanel's data to PDF in both VisualBasic and CSharp.

    Also Note
    Please add references of the following dependencies before you run the project in Visual Studio or Visual Web Developer.
    Dependencies:
    1. Ext.Net.dll
    2. Ext.Net.Utilities.dll
    3. itextsharp.dll
    4. Newtonsoft.Json.dll


    Itextsharp can be downloaded from

    http://sourceforge.net/projects/itextsharp/
    Attached Files
  2. #2
    Hi,

    Thank you for sharing. Looks good!
  3. #3
    Cool...

    One small question about the code:

    newPdfWriter is never used ?


    -- Edit--
    Works like a charm..forget above question
    -- End Edit--

    Martin
  4. #4

    Greetings

    Thanks CarWise for pointing out the mistake.
    Last edited by NickBin; Nov 15, 2011 at 7:38 PM.
  5. #5
    Ext.NET v2 related thread:
    http://forums.ext.net/showthread.php?22814

Similar Threads

  1. Export the grid to excel
    By Mr.Techno in forum 1.x Help
    Replies: 34
    Last Post: Aug 23, 2013, 12:39 PM
  2. Excel Export in Grid
    By BLOZZY in forum 1.x Help
    Replies: 0
    Last Post: Oct 09, 2011, 12:37 PM
  3. Grid Panel Export
    By muralimk in forum 1.x Help
    Replies: 1
    Last Post: Jan 28, 2011, 1:13 PM
  4. Replies: 1
    Last Post: Apr 19, 2010, 3:44 PM
  5. [CLOSED] grid export all
    By alexp in forum 1.x Premium Help
    Replies: 1
    Last Post: Jan 07, 2010, 7:50 AM

Tags for this Thread

Posting Permissions