【问题标题】:PDFizer: how to inset picture in generated pdf document?PFizer:如何在生成的 pdf 文档中插入图片?
【发布时间】:2012-02-14 18:43:44
【问题描述】:

我从这里使用 .NET 的 PDFizer 库 - PDFizer 我需要帮助...如何使用这个库将所有 html 文档(包括存储在其中的图片)转换为 PDF?现在我只能生成没有图片的pdf...

【问题讨论】:

    标签: asp.net pdf-generation


    【解决方案1】:

    经过一些测试,这是你需要做的:

    • 创建一个文件夹,其中包含所有图像。
    • 如果您已有Pdfizer.HtmlToPdfConverter 的实例,请将ImagePath 属性更改为指向您的图像所在的文件夹。
    • 在您的 html 代码中包含 <img> 标记。
    • 确保图片在文件夹中。

    注意:我尝试添加 Png 文件,但出现转换错误。这是我从您提供的网站中获取的示例,以及我的修改:

    System.Text.StringBuilder sbHtml = new System.Text.StringBuilder();
            sbHtml.Append("<html>");
            sbHtml.Append("<body>");
            sbHtml.Append("<font size='14'>My Document Title Line</font>");
            sbHtml.Append("<img src='trollface.jpg' />");
            sbHtml.Append("<br />");
            sbHtml.Append("This is my document text");
            sbHtml.Append("</body>");
            sbHtml.Append("</html>");
    
    
    
    
            //create file stream to PDF file to write to
            using (System.IO.Stream stream = new System.IO.FileStream
    
            (sPathToWritePdfTo, System.IO.FileMode.OpenOrCreate))
            {
                // create new instance of Pdfizer
                Pdfizer.HtmlToPdfConverter htmlToPdf = new Pdfizer.HtmlToPdfConverter();
                // open stream to write Pdf to to
                htmlToPdf.Open(stream);
                htmlToPdf.ImagePath = Server.MapPath(ResolveUrl("~/Images"));
    
                // write the HTML to the component
                htmlToPdf.Run(sbHtml.ToString());
                // close the write operation and complete the PDF file
                htmlToPdf.Close();
            }
        }
    

    祝你好运!

    【讨论】:

    • 没问题。如果这有助于您解决问题,请标记为答案。
    • 我还有一个问题要问您...例如,我将图片存储在 html 代码中,例如 - 我需要在 ImagePath 中写什么?因为我需要所有路径,例如 - site.com/images/logo.png
    • 记住我告诉过你的有关 png 图像的内容。他们可能会或不会工作。在您的 html 代码中,您只需编写图像的文件名,ImagePath 属性应该具有所有图片所在的完整路径。
    • 好的,谢谢 =) 但在我的情况下 png 图片效果很好 :)
    • 好!我尝试的图像一定有问题。我很高兴一切顺利。您是否能够在 pdf 中插入彩色文本?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    相关资源
    最近更新 更多