如何在C#中使用html在pdf文件中设置背景颜色和表格边框 [英] how to set background color and table border in pdf file using html in c#
本文介绍了如何在C#中使用html在pdf文件中设置背景颜色和表格边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经使用html模板创建了pdf文件生成.但是在h4标签上生成pdf背景颜色并且颜色也未设置后,建议我
i have created pdf file genration using html template. but after generating pdf background color on h4 tag and and color also not set please suggest me
我尝试在html中设置背景颜色和表格适当的边框颜色以内联css,但是在生成pdf之后,此css无法应用
i have try to inline css on set background color and table proper border color in html but after generating pdf this css not apply it
控制器:-
public void abcd()
{
try
{
string abc = Request.QueryString["abc"];
string strFileName = abc;
string strFileExtension = ".pdf";
string strContentType = FileManager.FileContentType_application_msexcel;
string strExportData = string.Empty;
Document pdfDoc = new Document(PageSize.A4, 43f, 50f, 5f, 50f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
var output = new MemoryStream();
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
StringReader sr;
sr = new StringReader(Convert.ToString(ExportData.abcUserDetails(abc)));
PdfWriter.GetInstance(pdfDoc, output);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
strFileName = strFileName.Replace(" - ", "-").Replace(" ", "-").Replace("--", "-");
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}{1}", strFileName, strFileExtension));
Response.ContentType = strContentType;
Response.Charset = "";
Response.BinaryWrite(output.ToArray());
Response.Flush();
Response.End();
}
catch (Exception ex)
{
Settings.exceptionLog.Publish(ex);
}
}
型号:-
public static string abcUserDetails(string abc)
{
string strHtmlBody = (new WebClient()).DownloadString(HttpContext.Current.Server.MapPath("~/Templates/abcUserDetails.html"));
string DomainURL = Settings.GetSettingsKeyValue("DomainURL");
IDictionary<string, string> objData = new Dictionary<string, string>();
dynamic dtUser = User.GetuserDetails(abc);
foreach (dynamic m in dtUser)
{
objData.Add("User.Name", Convert.ToString(m["Name"]));
objData.Add("User.RegNo", Convert.ToString(m["RegNo"]));
objData.Add("User.Gender", Convert.ToString(m["Gender"]));
objData.Add("User.EmailId", Convert.ToString(m["EmailId"]));
objData.Add("User.Address", Convert.ToString(m["VLEAddress"]));
}
string strValue = string.Empty;
foreach (KeyValuePair<string, string> data in objData)
{
strValue = HttpUtility.HtmlEncode(data.Value);
strHtmlBody = strHtmlBody.Replace(string.Format("%{0}%", data.Key), strValue);
}
return strHtmlBody;
}
html模板:-
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<link href="../css/custom/style.css" rel="stylesheet" />
<title></title>
</head>
<body style="font-family:Arial;font-size:7.5px;vertical-align:top!important;">
<h4 style="background-color:#d48d88;text-transform: uppercase;font-weight:bold;width:100%;">
User Details
</h4>
<table border="1" style="color:#000000;border-color:#6f6f6f;border-style: ridge;">
<tr>
<td valign="top" style="font-weight:bold;">
<b>Name</b>
</td>
<td valign="top">
%User.Name%
</td>
<td valign="top" style="font-weight:bold;">
<b>Mobile number</b>
</td>
<td valign="top">
%User.Mobile%
</td>
<td valign="top" style="font-weight:bold;">
<b>Email Id</b>
</td>
<td valign="top">
%User.Email%
</td>
</tr>
</table>
</body>
</html>
预期输出:-
推荐答案
thead th{
background: #ddd;
}
table {
width: 100% !important;
max-width: 100%;
border-spacing: 0;
border-collapse: collapse;
border-bottom: none;
}
th, td {
border: 1px solid #a9a6a6;
padding: 5px 8px;
text-align: left;
}
h4 {
background-color: #d48d88;
color: white;
padding: 5px;
text-align: center;
font-size: 16px;
}
<h4>User Details</h4>
<table>
<thead>
<tr>
<th>Name </th>
<th> %User.Name%</th>
<th>Mobile number</th>
<th> %User.Mobile%</th>
<th>Email Id</th>
<th>%User.Email%</th>
</tr>
</thead>
<tbody>
<tr>
<td> your name </td>
<td> user name </td>
<td> your mobile </td>
<td> your mobile no </td>
<td> example@gmail.com </td>
<td> User email</td>
</tr>
<tr>
<td> your name </td>
<td> user name </td>
<td> your mobile </td>
<td> your mobile no </td>
<td> example@gmail.com </td>
<td> User email</td>
</tr>
</tbody>
</table>
[希望它会对您有所帮助:::::::::::::让我知道它是否有效.]
[ hope it'll help you :::::::::::: let me know if it works.]
这篇关于如何在C#中使用html在pdf文件中设置背景颜色和表格边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文