Hi comunidadmexi...,
Loop through the DataTable rows and check the columns with condition.
Refer below modified code.
string filename;
string html;
string dest;
Paragraph contents;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[]
{
new DataColumn("sID"),
new DataColumn("sUnity"),
new DataColumn("contents")
});
dt.Rows.Add(83, "Q400", "Set n.1");
dt.Rows.Add(84, "Q400", "- Par 1.1");
dt.Rows.Add(85, "Q400", "<b>bold text</b>");
dt.Rows.Add(86, "Q400", "- Par 1.2");
dt.Rows.Add(87, "Q400", "normal text");
dt.Rows.Add(88, "Q400", "Set n.2");
dt.Rows.Add(89, "Q400", "- Par 2.1");
dt.Rows.Add(90, "Q400", "<i>italic text</i>");
dt.Rows.Add(91, "Q400", "- Par 2.2");
dt.Rows.Add(92, "Q400", "<u>underline text</u>");
dt.Rows.Add(93, "Q400", "- Par 2.3");
filename = @"C:\Users\developer4\Desktop\Management_" + Guid.NewGuid() + ".pdf";
PdfWriter writer = new PdfWriter(filename);
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf);
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
contents = new Paragraph(dt.Rows[i]["contents"].ToString())
.SetTextAlignment(TextAlignment.JUSTIFIED).SetFontSize(12);
if (dt.Rows[i]["contents"].ToString().StartsWith("Set ") && i != 0)
{
document.Add(new AreaBreak(AreaBreakType.NEXT_PAGE));
}
IElement lst = HtmlConverter.ConvertToElements(dt.Rows[i]["contents"].ToString()).FirstOrDefault();
IBlockElement element = (IBlockElement)lst;
if (dt.Rows[i]["contents"].ToString().StartsWith("Set"))
{
contents.SetFontSize(12)
.SetBold()
.SetFontColor(ColorConstants.BLUE);
}
else if (dt.Rows[i]["contents"].ToString().StartsWith("- "))
{
contents.SetFontSize(11)
.SetBold()
.SetFontColor(ColorConstants.BLACK);
}
else
{
contents.SetTextAlignment(TextAlignment.JUSTIFIED_ALL)
.SetFontSize(10)
.SetFontColor(ColorConstants.BLACK);
}
document.Add(element);
}
dest = filename.ToString();
}
else
{
Console.WriteLine("No rows found.");
}
document.Close();
Response.Clear();
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + dest);
Response.TransmitFile(dest);
Response.End();
}
}