ITextSharp解析带有图像的HTML:它正确解析但不会显示图像

saz*_*azr 9 c# asp.net itextsharp

我正在尝试使用库ITextSharp从html生成.pdf.我能够创建pdf与html文本转换为pdf文本/段落

我的问题: pdf没有显示我的图像(我的img元素来自html).我的html中的所有img html元素都不会显示在pdf中?ITextSharp是否可以解析HTML和显示图像.我真的希望如此,否则我被塞满了:(

我链接到图像所在的正确目录(使用IMG_BASURL)但它们只是没有显示

我的代码:

// mainContents variable is a string containing my HTML
var document = new Document(PageSize.A4, 50, 50, 80, 100);
var output = new MemoryStream();
var writer = PdfWriter.GetInstance(document, output);
document.open();

Hashtable providers = new Hashtable();
providers.Add("img_baseurl","C:/users/xx/VisualStudio/Projects/myproject/");
var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(mainContents), null, providers);
foreach (var htmlElement in parsedHtmlElements)
   document.Add(htmlElement as IElement);

document.Close();
Run Code Online (Sandbox Code Playgroud)

Chr*_*aas 11

每次我遇到这个问题时,图片对于画布来说太大了.更具体地说,即使是IMG内部的裸标签也将包裹在一个Chunk将被包裹在a中Paragraph,并且我认为图像溢出了段落,但我不是百分之百确定.

这两个简单的修复方法是放大画布或在HTML IMG标记上指定图像尺寸.第三个更复杂的路线是使用其他提供商IMG_PROVIDER.为此,您需要实现该IImageProvider接口.下面是一个非常简单的版本

    public class ImageThing : IImageProvider {
        //Store a reference to the main document so that we can access the page size and margins
        private Document MainDoc;
        //Constructor
        public  ImageThing(Document doc) {
            this.MainDoc = doc;
        }
        Image IImageProvider.GetImage(string src, IDictionary<string, string> attrs, ChainedProperties chain, IDocListener doc) {
            //Prepend the src tag with our path. NOTE, when using HTMLWorker.IMG_PROVIDER, HTMLWorker.IMG_BASEURL gets ignored unless you choose to implement it on your own
            src = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\" + src;
            //Get the image. NOTE, this will attempt to download/copy the image, you'd really want to sanity check here
            Image img = Image.GetInstance(src);
            //Make sure we got something
            if (img == null) return null;
            //Determine the usable area of the canvas. NOTE, this doesn't take into account the current "cursor" position so this might create a new blank page just for the image
            float usableW = this.MainDoc.PageSize.Width - (this.MainDoc.LeftMargin + this.MainDoc.RightMargin);
            float usableH = this.MainDoc.PageSize.Height - (this.MainDoc.TopMargin + this.MainDoc.BottomMargin);
            //If the downloaded image is bigger than either width and/or height then shrink it
            if (img.Width > usableW || img.Height > usableH) {
                img.ScaleToFit(usableW, usableH);
            }
            //return our image
            return img;
        }
    }
Run Code Online (Sandbox Code Playgroud)

要使用此提供程序,只需将其添加到提供程序集合中,就像您一样HTMLWorker.IMG_BASEURL:

providers.Add(HTMLWorker.IMG_PROVIDER, new ImageThing(doc));
Run Code Online (Sandbox Code Playgroud)

应该注意的是,如果你使用HTMLWorker.IMG_PROVIDER它,你有责任弄清楚图像的一切.上面的代码假设所有图像路径都需要添加一个常量字符串,您可能希望更新它并HTTP在开始时检查.此外,因为我们说我们想要完全处理图像处理管道,HTMLWorker.IMG_BASEURL所以不再需要提供程序.

主代码循环现在看起来像这样:

        string html = @"<img src=""Untitled-1.png"" />";
        string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "HtmlTest.pdf");
        using (FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
            using (Document doc = new Document(PageSize.A4, 50, 50, 80, 100)) {
                using (PdfWriter writer = PdfWriter.GetInstance(doc, fs)) {
                    doc.Open();
                    using (StringReader sr = new StringReader(html)) {
                        System.Collections.Generic.Dictionary<string, object> providers = new System.Collections.Generic.Dictionary<string, object>();
                        providers.Add(HTMLWorker.IMG_PROVIDER, new ImageThing(doc));

                        var parsedHtmlElements = HTMLWorker.ParseToList(sr, null,  providers);
                        foreach (var htmlElement in parsedHtmlElements) {
                            doc.Add(htmlElement as IElement);
                        }
                    }
                    doc.Close();
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

最后一件事,确保在此处发布时指定您要定位的iTextSharp版本.上面的代码针对iTextSharp 5.1.2.0但我认为您可能正在使用4.X系列.