简体   繁体   English

使用 itextsharp 下载后图像不会出现在 PDF 上

[英]Image will not appear on PDF after download while using itextsharp

I was able to successfully create a simple HTML table and then convert it it to a PDF file.我能够成功创建一个简单的 HTML 表格,然后将其转换为 PDF 文件。 However, the image I have for the table, will not appear on the PDF file after the download.但是,我为表格准备的图像在下载后不会出现在 PDF 文件中。 Can somebody tell me why this may be occurring?有人能告诉我为什么会发生这种情况吗? I have provided the code as to how I have it layed out.. I am using itextsharp and itextsharp.xmlworker to conver it to a PDF.我已经提供了有关如何布局的代码。我正在使用 itextsharp 和 itextsharp.xmlworker 将其转换为 PDF。

@{
    Layout = null;
}

<!DOCTYPE html>


<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <style type="text/css">
        body {
            font-family: Arial;
            font-size: 9pt;
        }
    </style>
</head>
<body>

    <hr />
    <div id="Grid" style="align-content: center">
        <img src="~/Content/images/logo.png">
        <h1 style="text-align:center;">Week of 01/01/2020 Report for Pizza Planet</h1>
        <h4>Compliance</h4>
        <table cellpadding="5" cellspacing="0" style="border: 1px solid #ccc;font-size: 9pt; width: 1000px">
            <tr>
                <th style="background-color: #f0522f;border: 1px solid #ccc">Number of Deliveries</th>
                <th style="background-color: #f0522f;border: 1px solid #ccc">Audited Savings</th>
                <th style="background-color: #f0522f;border: 1px solid #ccc">Gross Amount Due</th>
                <th style="background-color: #f0522f;border: 1px solid #ccc">Monthly Total</th>
                <th style="background-color: #f0522f;border: 1px solid #ccc">Year-To-Date Total</th>
            </tr>
            <tr>
                <td style="width:120px;border: 1px solid #ccc">234</td>
                <td style="width:120px;border: 1px solid #ccc">100.00</td>
                <td style="width:120px;border: 1px solid #ccc">100.00</td>
                <td style="width:120px;border: 1px solid #ccc">100.00</td>
                <td style="width:120px;border: 1px solid #ccc">100.00</td>
            </tr>

        </table>
        <h4>Special Handling</h4>
        <table cellpadding="5" cellspacing="0" style="border: 1px solid #ccc;font-size: 9pt; width: 1000px">
            <tr>
                <th style="background-color: #f0522f;border: 1px solid #ccc">Number of Deliveries</th>
                <th style="background-color: #f0522f;border: 1px solid #ccc">Audited Savings</th>
                <th style="background-color: #f0522f;border: 1px solid #ccc">Gross Amount Due</th>
                <th style="background-color: #f0522f;border: 1px solid #ccc">Monthly Total</th>
                <th style="background-color: #f0522f;border: 1px solid #ccc">Year-To-Date Total</th>
            </tr>

            <tr>
                <td style="width:120px;border: 1px solid #ccc">234</td>
                <td style="width:120px;border: 1px solid #ccc">100.00</td>
                <td style="width:120px;border: 1px solid #ccc">100.00</td>
                <td style="width:120px;border: 1px solid #ccc">100.00</td>
                <td style="width:120px;border: 1px solid #ccc">100.00</td>
            </tr>

        </table>
    </div>
    <br />
    <br />

    @using (Html.BeginForm("Export", "REPORT", FormMethod.Post))
    {
        <input type="hidden" name="GridHtml" />
        <input type="submit" id="btnSubmit" value="Download" />
    }
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#btnSubmit").click(function () {
                $("input[name='GridHtml']").val($("#Grid").html());
            });
        });
    </script>
</body>
</html>
protected void btnExport_Click(object sender, EventArgs e)
    {
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=test.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        string imageFilePath = Server.MapPath(".") + "/images/GraphicNew.jpg";
        iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);
        Document pdfDoc = new Document(PageSize.A4, 50f, 50f, 50f, 50f);
        //Resize image depend upon your need
        //For give the size to image
        jpg.ScaleToFit(3000, 770);
        //If you want to choose image as background then,
        jpg.Alignment = iTextSharp.text.Image.UNDERLYING;
        //If you want to give absolute/specified fix position to image.
        jpg.SetAbsolutePosition(7, 69);

        StringWriter stringWriter = new StringWriter();
        HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
        employeelistDiv.RenderControl(htmlTextWriter);
      
        StringReader stringReader = new StringReader(stringWriter.ToString());
        
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        pdfDoc.NewPage();
        pdfDoc.Add(jpg);
        htmlparser.Parse(stringReader);
        pdfDoc.Close();
        Response.Write(pdfDoc);
        Response.End();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM