简体   繁体   English

ASP.NET/C#如何在PDF上显示PNG

[英]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.

相关问题 如何在不使用C#的库的情况下修改PDF并将其流回ASP.NET中的客户端? - How do I modify PDF without a library using C# and stream it back to client in ASP.NET? 如何使用带有 Asp.Net 的 PDFBox 来解析带有 c# 的 pdf 文件? - How do I use PDFBox with Asp.Net for parsing pdf files with c#? 如何使用 ASP.NET Core MVC c# 生成 PDF 文档 - How do I a generate PDF document using ASP.NET Core MVC c# 如何获取 Asp.Net Core DropdownList 以在 C# 枚举中显示 [Display()] 值? - How do I get an Asp.Net Core DropdownList to show the [Display()] values in a C# enum? 如何让 ListBox 显示来自文本框的“ID”的所有信息(ASP.NET C#) - How do I get a ListBox to display all info for an "ID" from a Textbox (ASP.NET C#) C#在asp.net页面的iframe中显示pdf文档 - C# display pdf document in iframe in asp.net page 如何在ASP.NET/C#中创建选择列表? - How do I create a picklist in ASP.NET/C#? 如何从asp.net C#中的Blob存储中显示pdf文件, - How to display pdf file from Blob storage in asp.net C#, 如何在 ASP.Net 和 C# web 页面中将 Reporting Services 报表显示为内联 PDF? - How to display a Reporting Services report as an inline PDF, in a ASP.Net and C# web page? C#ASP.NET MVC:如何让浏览器从我的响应中自动下载pdf? - C# ASP.NET MVC: How do I get the browser to automatically download a pdf from my response?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM