If you want to use JSON data instead of directly fetching data
from the database, you can modify the controller to accept JSON input and then
deserialize it into the respective models. Here's how you can do it
using Microsoft.AspNetCore.Mvc;
using System.Data;
using System.Data.SqlClient;
using iTextSharp.text.pdf;
using iTextSharp.text;
using System.Text;
namespace WebApplication.Controllers
{
public class HomeController1 : Controller
{
// Connection string to your database
private string _connectionString = "*********************************************************************************";
public HomeController1(string connectionString)
{
_connectionString = connectionString;
}
public class TableAModel
{
public Guid Id { get; set; }
public string Description { get; set; }
public bool IsDeleted { get; set; }
}
public DataSet ConvertJSONToData(string json)
{
DataSet dataSet
= new DataSet();
using (SqlConnection connection = new
SqlConnection(_connectionString))
{
using (SqlCommand command = new SqlCommand("ConvertJSONToData",
connection))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@json", json);
connection.Open();
using (SqlDataAdapter adapter = new SqlDataAdapter(command))
{
adapter.Fill(dataSet);
}
}
}
return dataSet;
}
public List<TableAModel>
GetTableAData()
{
List<TableAModel> tableAData = new List<TableAModel>();
using (SqlConnection connection = new
SqlConnection(_connectionString))
{
string query = "SELECT * FROM TableA";
using (SqlCommand command = new SqlCommand(query,
connection))
{
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
TableAModel item = new TableAModel
{
Id = (Guid)reader["Id"],
// Populate other properties as needed
};
tableAData.Add(item);
}
}
}
return tableAData;
}
static void Main(string[] args)
{
string json = "*********************************************************************************";
string connectionString = "*********************************************************************************";
HomeController1
dataProcessor = new HomeController1(connectionString);
DataSet result =
dataProcessor.ConvertJSONToData(json);
//
Access the result sets
DataTable tableA
= result.Tables[0];
DataTable tableB
= result.Tables[1];
DataTable tableC
= result.Tables[2];
// Fetch data from TableA, TableB, and TableC
List<TableAModel> tableAData = dataProcessor.GetTableAData();
// Fetch data from TableB and TableC in a similar manner
// Generate HTML tables
string HtmltableA =
GenerateHtmlTable(tableA);
string HtmltableB =
GenerateHtmlTable(tableB);
string HtmltableC =
GenerateHtmlTable(tableC);
// Generate HTML tables for TableA, TableB, and TableC
// Generate and save PDF file
string pdfFilePath = "data.pdf";
GenerateAndSavePdf(new string[] { HtmltableA, HtmltableB, HtmltableC }, pdfFilePath);
Console.WriteLine($"PDF file generated
and saved: {pdfFilePath}");
}
static void GenerateAndSavePdf(string[] htmlTables, string pdfFilePath)
{
PdfWriter writer
= new
PdfWriter(pdfFilePath);
PdfDocument pdf
= new
PdfDocument(writer);
Document
document = new Document(pdf);
foreach (string htmlTable in htmlTables)
{
document.Add(new Paragraph("Table:"));
document.Add(new AreaBreak());
using (MemoryStream ms = new
MemoryStream(Encoding.UTF8.GetBytes(htmlTable)))
{
iText.Html2pdf.HtmlConverter.ConvertToPdf(ms, pdf);
}
document.Add(new AreaBreak());
}
document.Close();
}
static string GenerateHtmlTable(DataTable
dataTable)
{
StringBuilder
htmlTable = new StringBuilder();
htmlTable.Append("<table
border='1'>");
htmlTable.Append("<tr>");
foreach (DataColumn col in dataTable.Columns)
{
htmlTable.Append("<th>").Append(col.ColumnName).Append("</th>");
}
htmlTable.Append("</tr>");
foreach (DataRow row in dataTable.Rows)
{
htmlTable.Append("<tr>");
foreach (DataColumn col in dataTable.Columns)
{
htmlTable.Append("<td>").Append(row[col]).Append("</td>");
}
htmlTable.Append("</tr>");
}
htmlTable.Append("</table>");
return htmlTable.ToString();
}
}
}