Hi,
This is table t_contents_2021 on database MySql version 8.0.17
On the table t_contents_2021 in field contents there is some html code
<b>bold text</b>
<i>italic text</i>
<u>underline text</u>
Now I use iText (Version 7.1.13) to convert html to pdf using HtmlConverter (html2pdf version 2.0.1)
string filename;
string html;
string dest;
Paragraph contents;
protected void Page_Load(object sender, EventArgs e)
{
PdfWriter writer = new PdfWriter(Response.OutputStream);
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf);
using (MySqlConnection cn = new MySqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
{
using (MySqlCommand cmd = new MySqlCommand("SP", cn))
{
cmd.Connection.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("tunity", "Q400");
MySqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
contents = new Paragraph(reader.GetString("contents"))
.SetTextAlignment(TextAlignment.JUSTIFIED)
.SetFontSize(12);
if (reader.GetString("contents").StartsWith("Set"))
{
contents.SetFontSize(12)
.SetBold()
.SetFontColor(ColorConstants.BLUE);
document.Add(newline);
}
else if (reader.GetString("contents").StartsWith("- "))
{
contents.SetFontSize(10)
.SetBold()
.SetFontColor(ColorConstants.BLACK);
document.Add(newline);
}
else
{
contents.SetFontSize(10)
.SetFontColor(ColorConstants.BLACK);
}
if (!String.IsNullOrEmpty(reader["sID_content_image"].ToString()))
{
Image image_path = new Image(ImageDataFactory
.Create(reader["sID_content_image"].ToString()))
.SetTextAlignment(TextAlignment.CENTER);
image_path.SetAutoScale(false);
image_path.SetBorderLeft(new SolidBorder(ColorConstants.DARK_GRAY, 2));
image_path.SetBorderRight(new SolidBorder(ColorConstants.DARK_GRAY, 2));
image_path.SetBorderTop(new SolidBorder(ColorConstants.DARK_GRAY, 2));
image_path.SetBorderBottom(new SolidBorder(ColorConstants.DARK_GRAY, 2));
document.Add(image_path);
}
if (!String.IsNullOrEmpty(reader["sID_content_ppt"].ToString()))
{
string IMG1 = "C:\\inetpub\\wwwroot\\aspnet\\img\\powerpoint-png-picture-2.jpg";
Image img1 = new Image(ImageDataFactory.Create(IMG1));
Table tablelink = new Table(UnitValue.CreatePercentArray(1)).UseAllAvailableWidth();
PdfFont font = PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD);
Cell cell = new Cell().Add(img1.SetAutoScale(false));
cell.SetHorizontalAlignment(HorizontalAlignment.CENTER);
img1.SetHorizontalAlignment(HorizontalAlignment.CENTER);
cell.SetBorder(Border.NO_BORDER);
cell.SetAction(PdfAction.CreateURI(
reader["sID_content_ppt_link"].ToString()));
tablelink.AddCell(cell);
document.Add(tablelink);
document.Add(newline);
}
if (!String.IsNullOrEmpty(reader["sID_content_xls"].ToString()))
{
string IMG1 = "C:\\inetpub\\wwwroot\\img\\excel-2.png";
Image img1 = new Image(ImageDataFactory.Create(IMG1));
Table tablelink = new Table(UnitValue.CreatePercentArray(1)).UseAllAvailableWidth();
PdfFont font = PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD);
Cell cell = new Cell().Add(img1.SetAutoScale(false));
cell.SetHorizontalAlignment(HorizontalAlignment.CENTER);
img1.SetHorizontalAlignment(HorizontalAlignment.CENTER);
cell.SetBorder(Border.NO_BORDER);
cell.SetAction(PdfAction.CreateURI(
reader["sID_content_xls_link"].ToString()));
tablelink.AddCell(cell);
document.Add(tablelink);
document.Add(newline);
}
if (!String.IsNullOrEmpty(reader["sID_content_pdf"].ToString()))
{
string IMG1 = "C:\\inetpub\\wwwroot\\img\\pdf-png-picture-2.gif";
Image img1 = new Image(ImageDataFactory.Create(IMG1));
Table tablelink = new Table(UnitValue.CreatePercentArray(1)).UseAllAvailableWidth();
PdfFont font = PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD);
Cell cell = new Cell().Add(img1.SetAutoScale(false));
cell.SetHorizontalAlignment(HorizontalAlignment.CENTER);
img1.SetHorizontalAlignment(HorizontalAlignment.CENTER);
cell.SetBorder(Border.NO_BORDER);
cell.SetAction(PdfAction.CreateURI(
reader["sID_content_pdf_link"].ToString()));
tablelink.AddCell(cell);
document.Add(tablelink);
document.Add(newline);
}
document.Add(contents);
}
filename = @"C:\\inetpub\\wwwroot\\public\\Management_" + Guid.NewGuid() + ".pdf";
html = contents.ToString();
dest = filename.ToString();
HtmlConverter.ConvertToPdf(html, new FileStream(dest, FileMode.Create));
}
else
{
Console.WriteLine("No rows found.");
}
reader.Close();
cmd.Connection.Close();
}
}
document.Close();
Response.Clear();
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + dest);
Response.TransmitFile(dest);
Response.End();
}
The generated pdf file Management_14a40257-547f-4b6f-b991-eaa4aeb47a9a.pdf doesn't contains the values from table t_contents_2021 but contains only text
iText.Layout.Element.Paragraph
Help me to do it
This is the Stored procedure in MySql