PDA

View Full Version : Export Grid to PDF



NickBin
Dec 23, 2010, 8:43 AM
Further to

EXT.Net Examples Website: http://examples1.ext.net/#/GridPanel/Miscellaneous/Export_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(newDocum ent, 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.HttpCach eability.[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(newDocum ent, 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.HttpCach eability.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:

Ext.Net.dll
Ext.Net.Utilities.dll
itextsharp.dll
Newtonsoft.Json.dll


Itextsharp can be downloaded from

http://sourceforge.net/projects/itextsharp/

Daniil
Dec 24, 2010, 5:26 AM
Hi,

Thank you for sharing. Looks good!

CarWise
Dec 24, 2010, 8:06 AM
Cool...

One small question about the code:

newPdfWriter is never used ?


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

Martin

NickBin
Dec 24, 2010, 8:27 AM
Thanks CarWise for pointing out the mistake.

Daniil
Dec 28, 2012, 5:02 AM
Ext.NET v2 related thread:
http://forums.ext.net/showthread.php?22814