ITextSharp:如何获取图像嵌入资源

时间:2013-01-02 17:00:26

标签: c# itextsharp embedded-resource

我正在解析带有一些图片的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);
            }

提前致谢。

1 个答案:

答案 0 :(得分:3)

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

但是,这是问题的一个解决方案:

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

以下是实施:

/// <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);