我正在尝试使用以下代码在PDF上显示PNG文件。我将PNG放到了与代码相同的目录中,以排除任何目录路径不正确的问题,但我所得到的只是在PNG应该放置的PDF上有一个小方块。我正在使用NReco.PDFGenerator创建PDF。以下是代码的相关部分:
将PDF存储到html表中...
protected void expPDF_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
HtmlGenericControl div = new HtmlGenericControl("div");
div = BuildHTMLTable();
export dmc = new export();
var sb = new StringBuilder();
div.RenderControl(new HtmlTextWriter(new StringWriter(sb)));
string s = sb.ToString();
dmc.exportPDF("JCPS Collegiate Transcript", "Portrait", s);
}
protected HtmlGenericControl BuildHTMLTable()
{
//Inner Div
HtmlGenericControl div = new HtmlGenericControl("div");
HtmlGenericControl divHeader = new HtmlGenericControl("div");
//HeaderLabel
Table t = new Table();
t.Width = Unit.Percentage(100);
TableRow tr = new TableRow();
tr.VerticalAlign = VerticalAlign.Middle;
tr.HorizontalAlign = HorizontalAlign.Center;
tr.Font.Name = "Calibri";
//JCPS Logo
TableCell tc = new TableCell();
Image img = new Image();
img.ImageUrl = "JCPS Logo.png";
tc.Controls.Add(img);
tr.Cells.Add(tc);
t.Rows.Add(tr);
divHeader.Controls.Add(t);
div.Controls.Add(divHeader);
return div;
}
构建和推出PDF的功能:
public void exportPDF(string fileName, string Orientation, string html)
{
HtmlToPdfConverter pdf = new HtmlToPdfConverter();
html = html.Replace("\n", "");
html = html.Replace("\t", "");
html = html.Replace("\r", "");
html = html.Replace("\"", "'");
switch (Orientation)
{
case "Portrait":
pdf.Orientation = PageOrientation.Portrait;
break;
case "Landscape":
pdf.Orientation = PageOrientation.Landscape;
break;
default:
pdf.Orientation = PageOrientation.Default;
break;
}
//In case needed for future
//pdf.CustomWkHtmlArgs = "--margin-top 35 --header-spacing 0 --margin-left 0 --margin-right 0";
pdf.Margins = new PageMargins { Top = 5, Bottom = 5, Left = 5, Right = 5 };
pdf.PageFooterHtml = createPDFFooter();
var pdfBytes = pdf.GeneratePdf(createPDFScript() + html + "</body></html>");
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".pdf");
HttpContext.Current.Response.AddHeader("Content-Length", pdfBytes.Length.ToString());
HttpContext.Current.Response.BinaryWrite(pdfBytes);
HttpContext.Current.Response.Flush();
}
private string createPDFScript()
{
return "<html><head><style>td,th{line-height:20px;} tr { page-break-inside: avoid }</style><script>function subst() {var vars={};var x=document.location.search.substring(1).split('&');for(var i in x) {var z=x[i].split('=',2);vars[z[0]] = unescape(z[1]);}" +
"var x=['frompage','topage','page','webpage','section','subsection','subsubsection'];for(var i in x) {var y = document.getElementsByClassName(x[i]);" +
"for(var j=0; j<y.length; ++j) y[j].textContent = vars[x[i]];}}</script></head><body onload=\"subst()\">";
}
private string createPDFFooter()
{
return "<div><table style='font-family:Tahoma; font-size:9px; width:100%'><tr><td style='text-align:left'>Research Dept|DD:FC:jpg</td><td style='text-align:right'>Page <span class=\"page\"></span> of <span class=\"topage\"></span></td></div>";
}
答案 0 :(得分:0)
弄清楚了,希望这对其他人有帮助。我没有使用Image类,而是发现了HtmlImage类,事实证明这很简单。唯一的警告是,看来您必须使用要尝试收集的图像的绝对路径-没什么大不了的,但这是一个反复的尝试,直到我弄清楚为止。
//JCPS Logo
TableCell tc = new TableCell();
HtmlImage studPic = new HtmlImage();
if (HttpContext.Current.Request.IsLocal)
studPic.Src = "/Dev/Data Management Center/Images/JCPS Logo.png";
else
studPic.Src = "/Website/DMC/Images/JCPS Logo.png";
studPic.Height = 125;
studPic.Width = 150;
tc.Controls.Add(studPic);
tr.Cells.Add(tc);