[英]ASP.NET/C# How Do I Display a PNG on a PDF
I'm trying to use the following code to display a PNG file on a PDF. 我正在尝试使用以下代码在PDF上显示PNG文件。 I dropped the PNG into the same directory as the code to rule out any issues with incorrect directory paths, but all I'm getting is a little square on the PDF where the PNG should be located.
我将PNG放到了与代码相同的目录中,以排除任何目录路径不正确的问题,但我所得到的只是在PNG应该放置的PDF上有一个小方块。 I'm using NReco.PDFGenerator to create the PDF.
我正在使用NReco.PDFGenerator创建PDF。 Below are the relevant parts of the code:
以下是代码的相关部分:
Storing the PDF into an html table... 将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;
}
The function that builds and pushes out the PDF: 构建和推出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>";
}
Figured it out, hope this helps others. 想通了,希望对别人有帮助。 Instead of using the Image class I discovered the HtmlImage class, and turns out it's quite simple.
我没有使用Image类,而是发现了HtmlImage类,事实证明这很简单。 The only caveat is that it appears you have to use absolute paths to the image you are trying to collect - not a big deal but it was a little trial and error until I figured that out.
唯一的警告是,看来您必须使用要尝试收集的图像的绝对路径-没什么大不了的,但是直到我弄清楚这是一个小小的尝试和错误。
//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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.