I want to convert my HTML to PDF. All is working fine but the Font-Family.
I tried alot of things but all in vain.
This is my code.
private string CreatePDF()
{
try
{
string html = @"<table width='100%' cellpadding='0' cellspacing='0' border='0'>
<tr>
<td width='10%' align='center' valign='middle'>
<img src='http://38.105.68.211/media/fsl_logo_pdf.jpg' />
</td>
<td align='center' valign='middle'>
<font face='TimesNewRoman' size='8'>
<b>APPLICATION FOR GROUP TERM LIFE INSURANCE</b>
</font>
</br>
<p>
Underwritten by Fidelity Security Life Insurance Company, Kansas City, MO
</p>
<p style='text-align:right'>
TN - 100
</p>
</td>
<td width='10%'>
</td>
</tr>
</table>
<b>
<p>APPLICANT INFORMATION:</p>
</b>
<table>
<tr>
<th> Name:</th>
<th><p>" + ViewState["fname"].ToString() + @" </p></th>
<th><p>" + ViewState["middlename"].ToString() + @" </p></th>
<th><p>" + ViewState["lastname"].ToString() + @" </p></th>
</tr>
<tr>
<td></td>
<td> First </td>
<td> Middle </td>
<td> Last </td>
</tr>
</table>
<br/>
<table>
<tr>
<th width='20%'>Address:</th>
<th width='20%'><p>" + ViewState["address_line1"].ToString() + @" </p></th>
<th width='20%'><p>" + ViewState["city"].ToString() + @" </p></th>
<th width='20%'><p>" + ViewState["state"].ToString() + @" </p></th>
<th width='20%'><p>" + ViewState["zip"].ToString() + @" </p></th>
</tr>
<tr>
<td width='20%'></td>
<td width='20%'>Street </td>
<td width='20%'>City</td>
<td width='20%'>State</td>
<td width='20%'>Zip
</td>
</tr>
</table>
</br>
</br>
<table style='border-spacing: unset;''>
<tr>
<td>Sex:</td>
<td>
<img src='http://38.105.68.211/media/male.png'/>
</td>
<td>
</td>
<td></td>
<td>Date of Birth:</td>
<td>26/07/1989</td>
<td></td>
</tr>
<br/>
</table>
<b>
<p>SIGNATURE:</p>
</b>
<table>
< tr>
<td>
<p>
I understand that if death occurs from natural causes within 12 months from the effective date of insurance, or from suicide within 12
months from the effective date of insurance, no death benefit is payable, and the Company’s only obligation will be to refund all
premiums paid for that person.
</p>
</br>
<p>
I understand and acknowledge that by applying for this group insurance, I am a member of the State Department Federal Credit
Union. I understand the insurance applied for will become effective on the date specified by the Company only if this application is
accepted by the Company and the first premium is paid prior to the death of any proposed insured. I represent that all statements and
answers recorded on this application are true and complete as of the date I signed this application and are made to obtain the insurance
applied for. I understand that any false statement or material misrepresentations in the application may result in claim denial or
rescission of coverage, and that if coverage is rescinded, the Company’s only obligation will be to refund all premiums paid for that
person.
</p>
</br>
<b>WARNING: It is a crime to provide false or misleading information to an insurer for the purpose of defrauding the insurer
or any other person. Penalties include imprisonment and/or fines. In addition, an insurer may deny insurance benefits if false
information materially related to a claim was provided by the applicant.
</b>
</td>
</tr>
</table>
<br/>
<br/>
<table>
<tr>
<td width='25%'>Date:</td>
<td width='25%'></td>
<td width='25%'>Signature of Applicant:</td>
<td width='25%'></td>
</tr>
</table>
<br/>
<br/>
<table>
<tr>
<td width='700'>
A-01166DC
</td>
<td width='300'>
Policy Form No. M-1064
</td>
</tr>
</table>
";
return html;
}
catch (Exception)
{
throw;
}
}
private void SendPDFEmailImage()
{
try
{
if (Session["subscriber_person_id"] != DBNull.Value && Session["subscriber_person_id"] != null)
{
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
StringBuilder sb = new StringBuilder();
string Body = CreateEmailBody();
conn.Open();
SqlCommand cmd1 = new SqlCommand("GetSignatureText", conn);
cmd1.CommandType = CommandType.StoredProcedure;
cmd1.Parameters.AddWithValue("@Person_Id", Session["subscriber_person_id"].ToString());
cmd1.ExecuteNonQuery();
conn.Close();
DataTable dtSigText = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd1);
da.Fill(dtSigText);
//sb.Append("<table width='100%' cellspacing='0' cellpadding='2'>");
//sb.Append("<tr>");
//sb.Append("<td>");
//sb.Append("Congratulations Mr." + ViewState["Name"].ToString());
//sb.Append("<p style='color: black; text - align: justify; '> You have successfully enrolled in Clements worldwide insurance program.</p>");
// sb.Append("<head><title></title><meta charset = 'utf-8' /></head> ");
// sb.Append("<body>");
//// sb.Append("<label style='margin-left:220px; color: Red;'><b> APPLICATION FOR GROUP TERM LIFE INSURANCE </b></label>");
// sb.Append("<p style='color: Red; margin-left:220px'>Saad You have successfully enrolled in Clements worldwide insurance program.</p>");
// sb.Append("<p style='color:black; margin-left:220px;'>Congratulations Mr. " + ViewState["Name"].ToString() + @" </p>");
// sb.Append("</body>");
// if (dtSigText.Rows.Count > 0)
//sb.Append(dtSigText.Rows[0]["value"].ToString());
/////////////////////////For Image////////////
//conn.Open();
//SqlCommand cmd = new SqlCommand("GetPicture_Test", conn);
//cmd.CommandType = CommandType.StoredProcedure;
//cmd.Parameters.AddWithValue("@Person_Id", Session["subscriber_person_id"].ToString());
//cmd.ExecuteNonQuery();
//conn.Close();
//DataTable dt = new DataTable();
//SqlDataAdapter ds = new SqlDataAdapter(cmd);
//ds.Fill(dt);
//sb.Append("</tr>");
//sb.Append("</table>");
string PdfHtml = CreatePDF();
Document pdfDoc = new Document(PageSize.A4, 20f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
using (MemoryStream memoryStream = new MemoryStream())
{
//To Send Email
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
sb.Append(PdfHtml);
//sb.Append("<table>");
//sb.Append("<tr>");
//sb.Append("<td>");
//sb.Append("</td>");
//sb.Append("</tr>");
//sb.Append("</table>");
StringReader sr = new StringReader(sb.ToString());
// To Save PDF FIle
string date = System.DateTime.Now.ToString();
string filename = "../PDFData/" + Session["subscriber_person_id"].ToString() + ".pdf";
string path = Server.MapPath(filename);
PdfWriter.GetInstance(pdfDoc, new FileStream(path, FileMode.Create));
pdfDoc.Open();
// htmlparser.Parse(sr);
// pdfDoc.Add(footerTbl);
//if (dt.Rows.Count > 0)
//{
// // byte[] bytes1 = (byte[])dt.Rows[0]["value_image"];
// // string base64String = Convert.ToBase64String(bytes1, 0, bytes1.Length);
// // var image = iTextSharp.text.Image.GetInstance(bytes1);
// // image.ScaleAbsolute(80f, 85.25f);
// // pdfDoc.Add(image);
//}
//BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
//Font times = new Font(bfTimes, 12, Font.ITALIC);
//string arialuniTff = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIALUNI.TTF");
//FontFactory.Register(arialuniTff);
//StyleSheet ST = new StyleSheet();
//ST.LoadTagStyle(HtmlTags.TABLE, HtmlTags.FACE, "Arial Unicode MS");
//ST.LoadTagStyle(HtmlTags.TABLE, HtmlTags.ENCODING, BaseFont.IDENTITY_H);
//htmlparser.SetStyleSheet(ST);
//Console.ReadKey();
//StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
//styles.LoadTagStyle("th", "color", "red");
//styles.LoadTagStyle("th", "frontsize", "5");
//pdfDoc.Add(new Header(HtmlTags.TABLE, "Style.css"));
htmlparser.Parse(sr);
pdfDoc.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
//////MErge Documents///////////
// String[] files;
String[] files = new String[2];
files[0] = path;
files[1] = "E:\\Clements_FSL\\PDFData\\DC.pdf";
// String[] files = @"E:\\Clements_FSL\\PDFData\\DC.pdf,E:\Clements_FSL\PDFData\DC1.pdf".Split(',');
MergeFiles(@"E:\Clements_FSL\PDFData\New1.pdf", files);
//string filePath = @"E:\Clements_FSL\PDFData\Dc.pdf";
//string directoryName;
//directoryName = Path.GetDirectoryName(filePath);
//string filenamenew = "../PDFData/NewCreated/" + Session["subscriber_person_id"].ToString() +"1"+ ".pdf";
//string pathnew = Server.MapPath(filenamenew);
//CreateMergedPDF(pathnew, directoryName);
/////////////////////////
MailMessage mm = new MailMessage("noreply@clements.com", ViewState["emailid"].ToString());
mm.Subject = "Clements policy Document";
//Test
mm.Body = Body;
// mm.Attachments.Add(new Attachment(Body));
// mm.Attachments.Add(new Attachment(new MemoryStream(bytes), "ClementsPolicyDocument.pdf"));
Response.Redirect("~/enrollment/Step_add_Confirmaion_MsgBox.aspx", false);
}
}
}
}
}
catch (Exception ex)
{
throw ex;
}
}