在iTextSharp中创建包含图像(文件中的数据流)的PDF

时间:2019-03-22 12:23:44

标签: vb.net pdf itext

我正在尝试按照此示例创建带有图像的PDF Example PDF with an Image

我正在VB.NET(ASP.NET 3.5)中使用VS2013进行开发。

我快疯了,我不明白两件事:

  1. 我必须在IMG标签中传递的名称是什么。 src属性不包含http-Url。而是使用前缀data:imagestream标识图像的源类型。在以下斜杠后列出.NET库清单中的资源名称。

  2. 在CustomImageTagProcessor类中执行END ovveride函数时

我已经在项目中嵌入了一张图片,清单中包含了

...
}
.mresource public Test1.phone.jpg
{
  // Offset: 0x00000000 Length: 0x00003E0D
}
.mresource public Test1.Resources.resources
{
  // Offset: 0x00003E11 Length: 0x0000406B
}

我正在逐步调试,但从未执行过ovverride函数中的代码。

这是产生PDF的功能

 Public Function CreateFromHtml(ByVal html As String) As Stream

        Dim stream = New MemoryStream()

        Using doc = New Document(PageSize.A4)

            Using ms = New MemoryStream()

                Using writer = PdfWriter.GetInstance(doc, ms)
                    writer.CloseStream = False
                    doc.Open()
                    Dim tagProcessors = CType(Tags.GetHtmlTagProcessorFactory(), DefaultTagProcessorFactory)
                    tagProcessors.RemoveProcessor(iTextSharp.tool.xml.html.HTML.Tag.IMG)
                    tagProcessors.AddProcessor(iTextSharp.tool.xml.html.HTML.Tag.IMG, New CustomImageTagProcessor())
                    Dim cssFiles = New CssFilesImpl()
                    cssFiles.Add(XMLWorkerHelper.GetInstance().GetDefaultCSS())
                    Dim cssResolver = New StyleAttrCSSResolver(cssFiles)
                    Dim charset = Encoding.UTF8
                    Dim context = New HtmlPipelineContext(New CssAppliersImpl(New XMLWorkerFontProvider()))
                    context.SetAcceptUnknown(True).AutoBookmark(True).SetTagFactory(tagProcessors)
                    Dim htmlPipeline = New HtmlPipeline(context, New PdfWriterPipeline(doc, writer))
                    Dim cssPipeline = New CssResolverPipeline(cssResolver, htmlPipeline)
                    Dim worker = New XMLWorker(cssPipeline, True)
                    Dim xmlParser = New XMLParser(True, worker, charset)

                    Using sr = New StringReader(html)
                        xmlParser.Parse(sr)
                        doc.Close()
                        ms.Position = 0
                        ms.CopyTo(stream)
                        stream.Position = 0
                    End Using
                End Using
            End Using
        End Using

        Return stream
    End Function

这是CustomImageTagProcessor的类

Imports iTextSharp.tool.xml
Imports System.Reflection
Imports iTextSharp.text

Public Class CustomImageTagProcessor
    Inherits iTextSharp.tool.xml.html.Image

    Public Overrides Function [End](ByVal ctx As IWorkerContext, ByVal tag As Tag, ByVal currentContent As IList(Of IElement)) As IList(Of IElement)

        Dim src = String.Empty
        If Not tag.Attributes.TryGetValue(iTextSharp.tool.xml.html.HTML.Attribute.SRC, src) Then Return New List(Of IElement)(1)
        If String.IsNullOrEmpty(src) Then Return New List(Of IElement)(1)

        If src.StartsWith("data:imagestream/", StringComparison.InvariantCultureIgnoreCase) Then
            Dim name = src.Substring(src.IndexOf("/", StringComparison.InvariantCultureIgnoreCase) + 1)

            Using stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name)
                Return CreateElementList(ctx, tag, Image.GetInstance(stream))
            End Using
        End If

        Return MyBase.[End](ctx, tag, currentContent)
    End Function

    Protected Function CreateElementList(ByVal ctx As IWorkerContext, ByVal tag As Tag, ByVal image As Image) As IList(Of IElement)
        Dim htmlPipelineContext = GetHtmlPipelineContext(ctx)
        Dim result = New List(Of IElement)()
        Dim element = GetCssAppliers().Apply(New Chunk(CType(GetCssAppliers().Apply(image, tag, htmlPipelineContext), Image), 0, 0, True), tag, htmlPipelineContext)
        result.Add(element)
        Return result
    End Function
End Class

非常感谢您的帮助。 我希望你们。

1 个答案:

答案 0 :(得分:0)

问题是丢失的

library(dplyr)

df %>%
  group_by(Group) %>%
  mutate(orig = !duplicated(Value)) %>%
  group_by(Group, Date) %>%
  summarise(new_values = sum(orig))

#  Group     Date     new_values
#   <fct> <fct>         <int>
#1   A     01-01-19          3
#2   A     02-01-19          1
#3   B     01-01-19          3
#4   B     02-01-19          1

标签

自闭标签无效。

当我放置标签时,覆盖函数中的代码也已执行。

非常感谢。