gpt4 book ai didi

c# - 如何在 C# 中使用 html 在 pdf 文件中设置背景颜色和表格边框

转载 作者:行者123 更新时间:2023-11-27 23:12:17 25 4
gpt4 key购买 nike

我已经使用 html 模板创建了 pdf 文件生成。但是在 h4 标签上生成 pdf 背景颜色后,颜色也没有设置,请给我建议

我已经尝试在 html 中设置背景颜色和表格适当的边框颜色内联 css,但是在生成 pdf 之后,这个 css 不应用它

Controller :-

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>

预期输出:- enter image description here

最佳答案

默认情况下,HTML 到 PDF 转换器将为“屏幕”呈现 HTML 文档,但是当将另一种媒体类型分配给 HtmlToPdf.MediaType 属性时,这可以更改。

例如,当此属性设置为“打印”时,将在呈现 HTML 时使用“@media print”规则定义的 CSS 属性,而不是使用定义的 CSS 属性'@media screen' 规则。

 p { 
font-family: Verdana;
font-size: 14px;
font-style: italic;
color: Green;

}

@media print {
p {
border-style: dashed;
}
}

关于c# - 如何在 C# 中使用 html 在 pdf 文件中设置背景颜色和表格边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58457407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com