ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Blank PDF generated while exporting ASP.Net Chart to PDF using iTextSharp<p>Hi <a class="username" rel="ada20">ada20</a>,</p> <p>Refer below links -</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:DropDownList ID=&#34;ddlCountries&#34; runat=&#34;server&#34; OnSelectedIndexChanged=&#34;ddlCountries_SelectedIndexChanged&#34; AutoPostBack=&#34;true&#34;&gt; &lt;/asp:DropDownList&gt; &lt;hr /&gt; &lt;asp:Chart ID=&#34;Chart1&#34; runat=&#34;server&#34; Height=&#34;300px&#34; Width=&#34;400px&#34;&gt; &lt;Titles&gt; &lt;asp:Title ShadowOffset=&#34;3&#34; Name=&#34;Items&#34; /&gt; &lt;/Titles&gt; &lt;Legends&gt; &lt;asp:Legend Alignment=&#34;Center&#34; Docking=&#34;Bottom&#34; IsTextAutoFit=&#34;False&#34; Name=&#34;Default&#34; LegendStyle=&#34;Row&#34; /&gt; &lt;/Legends&gt; &lt;Series&gt; &lt;asp:Series Name=&#34;Default&#34; /&gt; &lt;/Series&gt; &lt;ChartAreas&gt; &lt;asp:ChartArea Name=&#34;ChartArea1&#34; BorderWidth=&#34;0&#34; /&gt; &lt;/ChartAreas&gt; &lt;/asp:Chart&gt; &lt;br /&gt; &lt;asp:Button ID=&#34;btnExportPDF&#34; runat=&#34;server&#34; Text=&#34;Export to PDF&#34; OnClick=&#34;btnExportPDF_Click&#34; /&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Configuration; using System.IO; using iTextSharp.text; using iTextSharp.text.pdf; using System.Web.UI.DataVisualization.Charting; using System.Data; using System.Data.SqlClient;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.IO Imports iTextSharp.text Imports iTextSharp.text.pdf Imports System.Web.UI.DataVisualization.Charting Imports System.Data Imports System.Data.SqlClient</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string query = &#34;select distinct shipcountry from orders&#34;; DataTable dt = GetData(query); ddlCountries.DataSource = dt; ddlCountries.DataTextField = &#34;shipcountry&#34;; ddlCountries.DataValueField = &#34;shipcountry&#34;; ddlCountries.DataBind(); ddlCountries.Items.Insert(0, new System.Web.UI.WebControls.ListItem(&#34;Select&#34;, &#34;&#34;)); } } protected void ddlCountries_SelectedIndexChanged(object sender, EventArgs e) { CreatChart(); } private void CreatChart() { Chart1.Visible = ddlCountries.SelectedValue != &#34;&#34;; string query = string.Format(&#34;select shipcity, count(orderid) from orders where shipcountry = &#39;{0}&#39; group by shipcity&#34;, ddlCountries.SelectedValue); DataTable dt = GetData(query); string[] x = new string[dt.Rows.Count]; int[] y = new int[dt.Rows.Count]; for (int i = 0; i &lt; dt.Rows.Count; i++) { x[i] = dt.Rows[i][0].ToString(); y[i] = Convert.ToInt32(dt.Rows[i][1]); } Chart1.Series[0].Points.DataBindXY(x, y); Chart1.Series[0].ChartType = SeriesChartType.Pie; Chart1.ChartAreas[&#34;ChartArea1&#34;].Area3DStyle.Enable3D = true; Chart1.Legends[0].Enabled = true; } private static DataTable GetData(string query) { DataTable dt = new DataTable(); SqlCommand cmd = new SqlCommand(query); String constr = ConfigurationManager.ConnectionStrings[&#34;ConString&#34;].ConnectionString; SqlConnection con = new SqlConnection(constr); SqlDataAdapter sda = new SqlDataAdapter(); cmd.CommandType = CommandType.Text; cmd.Connection = con; sda.SelectCommand = cmd; sda.Fill(dt); return dt; } protected void btnExportPDF_Click(object sender, EventArgs e) { CreatChart(); Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f); PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); using (MemoryStream stream = new MemoryStream()) { Chart1.SaveImage(stream, ChartImageFormat.Png); iTextSharp.text.Image chartImage = iTextSharp.text.Image.GetInstance(stream.GetBuffer()); chartImage.ScalePercent(75f); pdfDoc.Add(chartImage); pdfDoc.Close(); Response.ContentType = &#34;application/pdf&#34;; Response.AddHeader(&#34;content-disposition&#34;, &#34;attachment;filename=Chart.pdf&#34;); Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.Write(pdfDoc); Response.End(); } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not IsPostBack Then Dim query As String = &#34;select distinct shipcountry from orders&#34; Dim dt As DataTable = GetData(query) ddlCountries.DataSource = dt ddlCountries.DataTextField = &#34;shipcountry&#34; ddlCountries.DataValueField = &#34;shipcountry&#34; ddlCountries.DataBind() ddlCountries.Items.Insert(0, New System.Web.UI.WebControls.ListItem(&#34;Select&#34;, &#34;&#34;)) End If End Sub Protected Sub ddlCountries_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) CreatChart() End Sub Private Sub CreatChart() Chart1.Visible = ddlCountries.SelectedValue &lt;&gt; &#34;&#34; Dim query As String = String.Format(&#34;select shipcity, count(orderid) from orders where shipcountry = &#39;{0}&#39; group by shipcity&#34;, ddlCountries.SelectedValue) Dim dt As DataTable = GetData(query) Dim x As String() = New String(dt.Rows.Count - 1) {} Dim y As Integer() = New Integer(dt.Rows.Count - 1) {} For i As Integer = 0 To dt.Rows.Count - 1 x(i) = dt.Rows(i)(0).ToString() y(i) = Convert.ToInt32(dt.Rows(i)(1)) Next Chart1.Series(0).Points.DataBindXY(x, y) Chart1.Series(0).ChartType = SeriesChartType.Pie Chart1.ChartAreas(&#34;ChartArea1&#34;).Area3DStyle.Enable3D = True Chart1.Legends(0).Enabled = True End Sub Private Shared Function GetData(ByVal query As String) As DataTable Dim dt As DataTable = New DataTable() Dim cmd As SqlCommand = New SqlCommand(query) Dim constr As String = ConfigurationManager.ConnectionStrings(&#34;ConString&#34;).ConnectionString Dim con As SqlConnection = New SqlConnection(constr) Dim sda As SqlDataAdapter = New SqlDataAdapter() cmd.CommandType = CommandType.Text cmd.Connection = con sda.SelectCommand = cmd sda.Fill(dt) Return dt End Function Protected Sub btnExportPDF_Click(ByVal sender As Object, ByVal e As EventArgs) CreatChart() Dim pdfDoc As Document = New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F) PdfWriter.GetInstance(pdfDoc, Response.OutputStream) pdfDoc.Open() Using stream As MemoryStream = New MemoryStream() Chart1.SaveImage(stream, ChartImageFormat.Png) Dim chartImage As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(stream.GetBuffer()) chartImage.ScalePercent(75.0F) pdfDoc.Add(chartImage) pdfDoc.Close() Response.ContentType = &#34;application/pdf&#34; Response.AddHeader(&#34;content-disposition&#34;, &#34;attachment;filename=Chart.pdf&#34;) Response.Cache.SetCacheability(HttpCacheability.NoCache) Response.Write(pdfDoc) Response.[End]() End Using End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/3sJ6JMy.gif" alt="" width="453" height="529" /></p>https://www.aspforums.net:443/Threads/147096/Blank-PDF-generated-while-exporting-ASPNet-Chart-to-PDF-using-iTextSharp/https://www.aspforums.net:443/Threads/147096/Blank-PDF-generated-while-exporting-ASPNet-Chart-to-PDF-using-iTextSharp/Thu, 23 May 2019 02:58:43 GMT