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

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

提前致谢.

解决方法:

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

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

创建一个名为< 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);
上一篇:c# – 使用iTextSharp和PDFStamper在PDF中使用不同的字体


下一篇:c# – 使用itextsharp根据大小将pdf拆分为较小的pdf