【问题标题】:ASP.NET/C# How Do I Display a PNG on a PDFASP.NET/C# 如何在 PDF 上显示 PNG
【发布时间】:2019-02-20 18:18:25
【问题描述】:

我正在尝试使用以下代码在 PDF 上显示 PNG 文件。我将 PNG 放入与代码相同的目录中,以排除目录路径不正确的任何问题,但我得到的只是 PDF 上应该放置 PNG 的一个小方块。我正在使用 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>";
}

【问题讨论】:

    标签: c# image pdf


    【解决方案1】:

    想通了,希望这对其他人有所帮助。我没有使用 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);
    

    【讨论】:

      猜你喜欢
      • 2014-01-07
      • 2014-02-17
      • 1970-01-01
      • 2012-06-08
      • 1970-01-01
      • 2023-03-28
      • 2019-02-14
      • 2013-12-29
      • 1970-01-01
      相关资源
      最近更新 更多