Hi hsku6482,
Check this example. Now please take its reference and correct your code.
Namespaces
C#
using System.Data;
using System.IO;
using System.Web.UI.DataVisualization.Charting;
using iTextSharp.text;
using iTextSharp.text.pdf;
VB.Net
Imports System.Data
Imports System.IO
Imports System.Web.UI.DataVisualization.Charting
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
}
protected void OnGenerate(object sender, EventArgs e)
{
BindChart();
}
private void BindChart()
{
DataTable dt = new DataTable();
dt.Columns.Add("Department");
dt.Columns.Add("Period_Shown");
dt.Columns.Add("Num_Int");
dt.Rows.Add("Dept_AED(EMW)", "2018_10", 0);
dt.Rows.Add("Dept_AED(EMW)", "2018_11", 2.89);
dt.Rows.Add("Dept_AED(EMW)", "2018_12", 0);
dt.Rows.Add("Dept_AED(EMW)", "2019_01", 0);
dt.Rows.Add("Dept_AED(EMW)", "2019_02", 0);
dt.Rows.Add("Dept_AED(EMW)", "2019_02", 2.39);
dt.Rows.Add("Dept_AED(EMW)", "2019_03", 2.06);
dt.Rows.Add("Dept_AED(EMW)", "2019_04", 0);
dt.Rows.Add("Dept_AED(EMW)", "2019_05", 0);
dt.Rows.Add("Dept_AED(EMW)", "2019_06", 0);
dt.Rows.Add("Dept_AED(EMW)", "2019_07", 0);
dt.Rows.Add("Dept_AED(EMW)", "2019_08", 0);
dt.Rows.Add("Dept_AED(EMW)", "2019_09", 0);
dt.Rows.Add("Dept_AED(EMW)", "2019_10", 0);
dt.Rows.Add("Dept_AED(EMW)", "2019_11", 2.24);
dt.Rows.Add("Dept_AED(EMW)", "2019_12", 0);
dt.Rows.Add("Dept_MG", "2017_01", 0.61);
dt.Rows.Add("Dept_MG", "2017_02", 0.35);
dt.Rows.Add("Dept_MG", "2017_03", 0.45);
dt.Rows.Add("Dept_MG", "2017_04", 0.66);
dt.Rows.Add("Dept_MG", "2017_05", 0.87);
dt.Rows.Add("Dept_MG", "2017_06", 0.71);
dt.Rows.Add("Dept_MG", "2017_07", 0.49);
dt.Rows.Add("Dept_MG", "2017_08", 0.51);
dt.Rows.Add("Dept_MG", "2017_09", 0.41);
dt.Rows.Add("Dept_MG", "2017_10", 0.4);
dt.Rows.Add("Dept_MG", "2017_11", 0.34);
dt.Rows.Add("Dept_MG", "2017_12", 0.5);
dt.Rows.Add("Dept_MG", "2018_01", 0.64);
dt.Rows.Add("Dept_MG", "2018_02", 0.46);
dt.Rows.Add("Dept_MG", "2018_03", 0.6);
dt.Rows.Add("Dept_MG", "2018_04", 0.82);
dt.Rows.Add("Dept_MG", "2018_05", 0.69);
dt.Rows.Add("Dept_MG", "2018_06", 0.71);
dt.Rows.Add("Dept_MG", "2018_07", 0.73);
dt.Rows.Add("Dept_MG", "2018_08", 0.95);
dt.Rows.Add("Dept_MG", "2018_09", 0.45);
dt.Rows.Add("Dept_MG", "2018_10", 0.53);
dt.Rows.Add("Dept_MG", "2018_11", 0.8);
dt.Rows.Add("Dept_MG", "2018_12", 0.41);
dt.Rows.Add("Dept_MG", "2019_01", 0.56);
dt.Rows.Add("Dept_MG", "2019_02", 0.44);
dt.Rows.Add("Dept_MG", "2019_03", 0.23);
dt.Rows.Add("Dept_MG", "2019_04", 1.03);
dt.Rows.Add("Dept_MG", "2019_05", 0.65);
dt.Rows.Add("Dept_MG", "2019_06", 0.36);
dt.Rows.Add("Dept_MG", "2019_07", 0.48);
dt.Rows.Add("Dept_MG", "2019_08", 0.78);
dt.Rows.Add("Dept_MG", "2019_09", 0.62);
dt.Rows.Add("Dept_MG", "2019_10", 0.72);
dt.Rows.Add("Dept_MG", "2019_11", 0.64);
gvInt.DataSource = dt;
gvInt.DataBind();
DataTable dtInt = new DataTable("dtInts");
foreach (TableCell cell in gvInt.HeaderRow.Cells)
{
dtInt.Columns.Add(cell.Text.Trim());
}
foreach (GridViewRow row in gvInt.Rows)
{
dtInt.Rows.Add();
for (int k = 0; k < row.Cells.Count; k++)
{
dtInt.Rows[row.RowIndex][k] = row.Cells[k].Text.Trim();
}
}
List<string> departments = (from p in dtInt.AsEnumerable()
select p.Field<string>("Department")).Distinct().ToList();
if (RateChart.Series.Count() == 1)
{
RateChart.Series.Remove(RateChart.Series[0]);
}
for (int i = 0; i < departments.Count; i++)
{
string[] x = (from p in dtInt.AsEnumerable()
orderby p.Field<string>("Period_Shown")
select p.Field<string>("Period_Shown")).Distinct().ToArray();
List<decimal> y = new List<decimal>();
for (int j = 0; j < x.Length; j++)
{
decimal numInt = (from p in dtInt.AsEnumerable()
where p.Field<string>("Department") == departments[i]
&& p.Field<string>("Period_Shown") == x[j]
select Convert.ToDecimal(p.Field<string>("Num_Int"))).FirstOrDefault();
y.Add(numInt);
}
RateChart.Series.Add(new Series(departments[i]));
RateChart.Series[departments[i]].IsValueShownAsLabel = false;
RateChart.Series[departments[i]].BorderWidth = 3;
RateChart.Series[departments[i]].ChartType = SeriesChartType.Line;
RateChart.Series[departments[i]].Points.DataBindXY(x, y);
RateChart.Series[departments[i]].MarkerStyle = MarkerStyle.Star10;
}
RateChart.Legends.Add("Default");
RateChart.Legends[0].Docking = Docking.Right;
RateChart.Legends[0].IsTextAutoFit = true;
RateChart.Legends[0].Alignment = System.Drawing.StringAlignment.Center;
RateChart.Legends[0].Enabled = true;
RateChart.ChartAreas[0].AxisX.Interval = 1;
RateChart.ChartAreas[0].AxisX.Title = "Period";
RateChart.ChartAreas[0].AxisX.TitleForeColor = System.Drawing.Color.Red;
RateChart.ChartAreas[0].AxisY.Title = "Number of incidents";
RateChart.ChartAreas[0].AxisY.TitleForeColor = System.Drawing.Color.Red;
RateChart.ChartAreas[0].AxisY.TitleAlignment = System.Drawing.StringAlignment.Far;
RateChart.ChartAreas[0].AxisY.TextOrientation = TextOrientation.Rotated90;
}
protected void OnPrint(object sender, EventArgs e)
{
// Code for rebind the RateChart.
BindChart();
RateChart.Visible = true;
Document pdfDoc = new Document(PageSize.A4.Rotate(), 10, 10, 10, 0);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
string chFontPath = @"c:\\windows\\fonts\\times.ttf";
BaseFont chBaseFont = BaseFont.CreateFont(chFontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font chFont = new Font(chBaseFont, 12);
Phrase simplePhr1 = new Phrase("Patient Fall");
Phrase simplePhr2 = new Phrase("Incedent Rate in O&G");
Phrase simplePhr3 = new Phrase("Department and Wards");
Phrase simplePhr4 = new Phrase("From January 2015 to December 2020");
Phrase simplePhr5 = new Phrase("");
Paragraph simplePara = new Paragraph();
simplePara.Add(simplePhr1);
simplePara.Add(simplePhr2);
simplePara.Add(simplePhr3);
simplePara.Add(Environment.NewLine);
simplePara.Add(simplePhr4);
simplePara.Add(Environment.NewLine);
simplePara.Add(simplePhr5);
MemoryStream stream = new MemoryStream();
RateChart.SaveImage(stream, ChartImageFormat.Png);
iTextSharp.text.Image chartImage = iTextSharp.text.Image.GetInstance(stream.GetBuffer());
chartImage.ScalePercent(75);
pdfDoc.Add(simplePara);
pdfDoc.Add(chartImage);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Chart.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
End Sub
Protected Sub OnGenerate(ByVal sender As Object, ByVal e As EventArgs)
BindChart()
End Sub
Private Sub BindChart()
Dim dt As DataTable = New DataTable()
dt.Columns.Add("Department")
dt.Columns.Add("Period_Shown")
dt.Columns.Add("Num_Int")
dt.Rows.Add("Dept_AED(EMW)", "2018_10", 0)
dt.Rows.Add("Dept_AED(EMW)", "2018_11", 2.89)
dt.Rows.Add("Dept_AED(EMW)", "2018_12", 0)
dt.Rows.Add("Dept_AED(EMW)", "2019_01", 0)
dt.Rows.Add("Dept_AED(EMW)", "2019_02", 0)
dt.Rows.Add("Dept_AED(EMW)", "2019_02", 2.39)
dt.Rows.Add("Dept_AED(EMW)", "2019_03", 2.06)
dt.Rows.Add("Dept_AED(EMW)", "2019_04", 0)
dt.Rows.Add("Dept_AED(EMW)", "2019_05", 0)
dt.Rows.Add("Dept_AED(EMW)", "2019_06", 0)
dt.Rows.Add("Dept_AED(EMW)", "2019_07", 0)
dt.Rows.Add("Dept_AED(EMW)", "2019_08", 0)
dt.Rows.Add("Dept_AED(EMW)", "2019_09", 0)
dt.Rows.Add("Dept_AED(EMW)", "2019_10", 0)
dt.Rows.Add("Dept_AED(EMW)", "2019_11", 2.24)
dt.Rows.Add("Dept_AED(EMW)", "2019_12", 0)
dt.Rows.Add("Dept_MG", "2017_01", 0.61)
dt.Rows.Add("Dept_MG", "2017_02", 0.35)
dt.Rows.Add("Dept_MG", "2017_03", 0.45)
dt.Rows.Add("Dept_MG", "2017_04", 0.66)
dt.Rows.Add("Dept_MG", "2017_05", 0.87)
dt.Rows.Add("Dept_MG", "2017_06", 0.71)
dt.Rows.Add("Dept_MG", "2017_07", 0.49)
dt.Rows.Add("Dept_MG", "2017_08", 0.51)
dt.Rows.Add("Dept_MG", "2017_09", 0.41)
dt.Rows.Add("Dept_MG", "2017_10", 0.4)
dt.Rows.Add("Dept_MG", "2017_11", 0.34)
dt.Rows.Add("Dept_MG", "2017_12", 0.5)
dt.Rows.Add("Dept_MG", "2018_01", 0.64)
dt.Rows.Add("Dept_MG", "2018_02", 0.46)
dt.Rows.Add("Dept_MG", "2018_03", 0.6)
dt.Rows.Add("Dept_MG", "2018_04", 0.82)
dt.Rows.Add("Dept_MG", "2018_05", 0.69)
dt.Rows.Add("Dept_MG", "2018_06", 0.71)
dt.Rows.Add("Dept_MG", "2018_07", 0.73)
dt.Rows.Add("Dept_MG", "2018_08", 0.95)
dt.Rows.Add("Dept_MG", "2018_09", 0.45)
dt.Rows.Add("Dept_MG", "2018_10", 0.53)
dt.Rows.Add("Dept_MG", "2018_11", 0.8)
dt.Rows.Add("Dept_MG", "2018_12", 0.41)
dt.Rows.Add("Dept_MG", "2019_01", 0.56)
dt.Rows.Add("Dept_MG", "2019_02", 0.44)
dt.Rows.Add("Dept_MG", "2019_03", 0.23)
dt.Rows.Add("Dept_MG", "2019_04", 1.03)
dt.Rows.Add("Dept_MG", "2019_05", 0.65)
dt.Rows.Add("Dept_MG", "2019_06", 0.36)
dt.Rows.Add("Dept_MG", "2019_07", 0.48)
dt.Rows.Add("Dept_MG", "2019_08", 0.78)
dt.Rows.Add("Dept_MG", "2019_09", 0.62)
dt.Rows.Add("Dept_MG", "2019_10", 0.72)
dt.Rows.Add("Dept_MG", "2019_11", 0.64)
gvInt.DataSource = dt
gvInt.DataBind()
Dim dtInt As DataTable = New DataTable("dtInts")
For Each cell As TableCell In gvInt.HeaderRow.Cells
dtInt.Columns.Add(cell.Text.Trim())
Next
For Each row As GridViewRow In gvInt.Rows
dtInt.Rows.Add()
Dim k As Integer = 0
For k = 0 To row.Cells.Count - 1
dtInt.Rows(row.RowIndex)(k) = row.Cells(k).Text.Trim()
Next
Next
Dim departments As List(Of String) = New List(Of String)()
departments = (From p In dtInt.AsEnumerable()
Select p.Field(Of String)("Department")).Distinct().ToList()
If RateChart.Series.Count() = 1 Then
RateChart.Series.Remove(RateChart.Series(0))
End If
Dim i As Integer = 0
For i = 0 To departments.Count - 1 Step 1
Dim x As String() = (From p In dtInt.AsEnumerable()
Order By p.Field(Of String)("Period_Shown")
Select p.Field(Of String)("Period_Shown")).Distinct().ToArray()
Dim y As List(Of Decimal) = New List(Of Decimal)()
Dim j As Integer = 0
For j = 0 To x.Length - 1 Step 1
Dim numInt As Decimal = (From p In dtInt.AsEnumerable()
Where p.Field(Of String)("Department") = departments(i) _
AndAlso p.Field(Of String)("Period_Shown") = x(j)
Order By p.Field(Of String)("Period_Shown")
Select Convert.ToDecimal(p.Field(Of String)("Num_Int"))).FirstOrDefault()
y.Add(numInt)
Next
RateChart.Series.Add(New Series(departments(i)))
RateChart.Series(departments(i)).IsValueShownAsLabel = False
RateChart.Series(departments(i)).BorderWidth = 3
RateChart.Series(departments(i)).ChartType = SeriesChartType.Line
RateChart.Series(departments(i)).Points.DataBindXY(x, y)
RateChart.Series(departments(i)).MarkerStyle = MarkerStyle.Star10
Next
RateChart.Legends.Add("Default")
RateChart.Legends(0).Docking = Docking.Bottom
RateChart.Legends(0).IsTextAutoFit = True
RateChart.Legends(0).Alignment = Drawing.StringAlignment.Center
RateChart.Legends(0).Enabled = True
RateChart.ChartAreas(0).AxisX.Interval = 1
RateChart.ChartAreas(0).AxisX.Title = "Period"
RateChart.ChartAreas(0).AxisX.TitleForeColor = System.Drawing.Color.Red
RateChart.ChartAreas(0).AxisY.Title = "Number of incidents"
RateChart.ChartAreas(0).AxisY.TitleForeColor = Drawing.Color.Red
RateChart.ChartAreas(0).AxisY.TitleAlignment = Drawing.StringAlignment.Far
RateChart.ChartAreas(0).AxisY.TextOrientation = TextOrientation.Rotated90
End Sub
Protected Sub OnPrint(ByVal sender As Object, ByVal e As EventArgs)
BindChart()
RateChart.Visible = True
Dim pdfDoc As Document = New Document(PageSize.A4.Rotate(), 10, 10, 10, 0)
Dim writer As PdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
pdfDoc.Open()
Dim chFontPath As String = "c:\\windows\\fonts\\times.ttf"
Dim chBaseFont As BaseFont = BaseFont.CreateFont(chFontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED)
Dim chFont As Font = New Font(chBaseFont, 12)
Dim simplePhr1 As Phrase = New Phrase("Patient Fall")
Dim simplePhr2 As Phrase = New Phrase("Incedent Rate in O&G")
Dim simplePhr3 As Phrase = New Phrase("Department and Wards")
Dim simplePhr4 As Phrase = New Phrase("From January 2015 to December 2020")
Dim simplePhr5 As Phrase = New Phrase("")
Dim simplePara As Paragraph = New Paragraph()
simplePara.Add(simplePhr1)
simplePara.Add(simplePhr2)
simplePara.Add(simplePhr3)
simplePara.Add(Environment.NewLine)
simplePara.Add(simplePhr4)
simplePara.Add(Environment.NewLine)
simplePara.Add(simplePhr5)
Dim stream As MemoryStream = New MemoryStream()
RateChart.SaveImage(stream, ChartImageFormat.Png)
Dim chartImage As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(stream.GetBuffer())
chartImage.ScalePercent(75)
pdfDoc.Add(simplePara)
pdfDoc.Add(chartImage)
pdfDoc.Close()
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment;filename=Chart.pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.Write(pdfDoc)
Response.End()
End Sub
Screenshot