4

我正在解析其中包含一些图像的 HTML。

这些图像存储为嵌入式资源,而不是文件系统。

据我所知,我需要在 HtmlPipelineContext 中设置自定义图像提供程序,并且该提供程序需要检索图像路径或 itextsharp 图像。

问题是,有人知道我需要实现哪种抽象图像提供程序方法?如何?

这是我的代码:

            var list = new List<string> { text };
            byte[] renderedBuffer;
            using (var outputMemoryStream = new MemoryStream())
            {
                using (
                    var pdfDocument = new Document(PageSize.A4, 30, 30, 30, 30))
                {
                    var pdfWriter = PdfWriter.GetInstance(pdfDocument, outputMemoryStream);
                    pdfWriter.CloseStream = false;
                    pdfDocument.Open();
                    HtmlPipelineContext htmlContext = new HtmlPipelineContext(new CssAppliersImpl());
                    htmlContext.SetImageProvider(new MyImageProvider());
                    htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
                    ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(true);
                    CssResolverPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(pdfDocument, pdfWriter)));
                    XMLWorker worker = new XMLWorker(pipeline, true);
                    XMLParser p = new XMLParser(worker);
                    foreach (var htmlText in list)
                    {
                        using (var htmlViewReader = new StringReader(htmlText))
                        {
                            p.Parse(htmlViewReader);
                        }
                    }
                }

                renderedBuffer = new byte[outputMemoryStream.Position];
                outputMemoryStream.Position = 0;
                outputMemoryStream.Read(renderedBuffer, 0, renderedBuffer.Length);
            }

提前致谢。

4

1 回答 1

3

使用自定义图像提供程序似乎不受支持。它唯一真正支持的是更改根路径。

但是,这是解决该问题的一种方法:

创建一个名为 的新 html 标记,<resimg src="{resource name}"/>并为其编写自定义标记处理器。

这是实现:

/// <summary>
/// Our custom HTML Tag to add an IElement.
/// </summary>
public class ResourceImageHtmlTagProcessor : AbstractTagProcessor
{
    public override IList<IElement> End(IWorkerContext ctx, Tag tag, IList<IElement> currentContent)
    {
        var src = tag.Attributes["src"];
        var bitmap = (Bitmap)Resources.ResourceManager.GetObject(src);

        if (bitmap == null)
            throw new RuntimeWorkerException("No resource with the name: " + src);

        var converter = new ImageConverter();
        var image = Image.GetInstance((byte[])converter.ConvertTo(bitmap, typeof(byte[])));
        HtmlPipelineContext htmlPipelineContext = this.GetHtmlPipelineContext(ctx);
        return new List<IElement>(1)
            {
                this.GetCssAppliers().Apply(
                    new Chunk((Image)this.GetCssAppliers().Apply(image, tag, htmlPipelineContext), 0f, 0f, true),
                    tag,
                    htmlPipelineContext)
            };
    }
}

要配置您的新处理器,请将您指定 TagFactory 的行替换为以下内容:

var tagProcessorFactory = Tags.GetHtmlTagProcessorFactory();
tagProcessorFactory.AddProcessor(new ResourceImageHtmlTagProcessor(), new[] { "resimg" });
htmlContext.SetTagFactory(tagProcessorFactory);
于 2013-01-02T22:19:27.927 回答