【问题标题】:Html to PDF ITextSharp ImageHtml 到 PDF ITextSharp 图像
【发布时间】:2017-09-30 02:07:20
【问题描述】:

我正在使用 ITextSharp 将我的 Html 转换为 PDF,当我尝试将图像插入 HTML 页面时出现问题,但是当下载 PDF 时我看不到其中的图像。我没有看到任何错误,代码工作正常,但我在下载的 PDF 中看不到图像。 PDF 的路径是正确的,因为它在 HTML PAGE 中工作,我什至尝试使用 HTML 更改图像和画布的尺寸,但这并没有解决问题。

将 HTML 转换为 PDF 的编码是:

public void DownloadAsPDF()
    {
        try
        {
            string case_id = Request.Form["case_id"];
            string strHtml = string.Empty;
            string pdfFileName = Request.PhysicalApplicationPath + "\\files\\" + case_id + ".pdf";


            string template = System.IO.File.ReadAllText(Server.MapPath("~/Incomplete-Pdf-temp.html"));

插入图片的代码开始

            Base64ToImage().Save(Server.MapPath("\\files\\" + case_id + "stu.jpg"));

            Base64ToInsImage().Save(Server.MapPath("\\files\\" + case_id + "ins.jpg"));
            string facultysign = "/files/CS00022904stu.jpg";
            string stusign = "/files/CS00022904stu.jpg";
            template = template.Replace("[stusign]", facultysign);
            template = template.Replace("[facultysign]", stusign);

插入图片结尾的代码

            CreatePDFFromHTMLFile(template, pdfFileName);
 }
catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }



    public void CreatePDFFromHTMLFile(string HtmlStream, string FileName)
    {
        try
        {
            object TargetFile = FileName;

            string ModifiedFileName = string.Empty;
            string FinalFileName = string.Empty;

            GeneratePDF.HtmlToPdfBuilder builder = new GeneratePDF.HtmlToPdfBuilder(iTextSharp.text.PageSize.A4);
            GeneratePDF.HtmlPdfPage first = builder.AddPage();
            first.AppendHtml(HtmlStream);
            byte[] file = builder.RenderPdf();
            System.IO.File.WriteAllBytes(TargetFile.ToString(), file);

            iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(TargetFile.ToString());
            ModifiedFileName = TargetFile.ToString();
            ModifiedFileName = ModifiedFileName.Insert(ModifiedFileName.Length - 4, "1");

            iTextSharp.text.pdf.PdfEncryptor.Encrypt(reader, new FileStream(ModifiedFileName, FileMode.Append), iTextSharp.text.pdf.PdfWriter.STRENGTH128BITS, "", "", iTextSharp.text.pdf.PdfWriter.AllowPrinting);
            reader.Close();
            if (System.IO.File.Exists(TargetFile.ToString()))
                System.IO.File.Delete(TargetFile.ToString());
            FinalFileName = ModifiedFileName.Remove(ModifiedFileName.Length - 5, 1);
            System.IO.File.Copy(ModifiedFileName, FinalFileName);
            if (System.IO.File.Exists(ModifiedFileName))
                System.IO.File.Delete(ModifiedFileName);

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

*用于插入图像的 HMTL 编码 *

 <p><strong>Faculty Signature: </strong></p>
   <img src='[stusign]' />

 <p><strong>Faculty Signature: </strong></p>
   <img src='[facultysign]' />

【问题讨论】:

  • 您使用的是哪个版本的 iTextSharp?我还期待在您的代码中看到XMLWorker。看起来您正在使用此页面中的代码:codescratcher.com/asp-net/… 但该网站中的 GeneratePDF.cs 文件不是 iTextSharp 的一部分。
  • 如果您在弄清楚为什么某些内容存在(或不存在)在您的 pdf 文档中时遇到问题,请考虑使用 RUPS(可从 iText 存储库下载)。它允许您查看 pdf 文档的内部结构。
  • @AmedeeVanGasse 是的,我正在使用您提到的网站上的代码,那么我现在该怎么办?
  • @JorisSchellekens 我检查了 RUPS 中的文档。它向我展示了类似的东西。 7 0 obj>/ProcSet[/PDF/Text/ ImageB/ImageC/ImageI]>>/父 5 0 R/MediaBox[0 0 595 842]>>

标签: html asp.net image pdf itext


【解决方案1】:

调整图像大小为我解决了这个问题(即使调整画布大小也能解决问题)。

【讨论】:

    【解决方案2】:
    后面代码中的

    pdfptablepdfpcell 可用于定位 PDF 中的图像。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-14
      • 2015-05-30
      • 2011-02-18
      • 1970-01-01
      • 2013-07-11
      • 1970-01-01
      • 2012-02-26
      • 2015-10-26
      相关资源
      最近更新 更多