Images not displaying when using ConvertHtmlToWml and writing to memory stream

Home Forums Open-Xml-PowerTools Images not displaying when using ConvertHtmlToWml and writing to memory stream

This topic contains 0 replies, has 1 voice, and was last updated by  gmogollon 1 year ago.

Viewing 1 post (of 1 total)
  • Author
  • #4357


    Hi and thanks Eric for all the support you give to this forum.

    We have word documents that are used as a template with fields that get replaced with HTML data that comes from a database. My approach was to retrieve the word document template and convert it to HTML using WmlToHtmlConverter.ConvertToHtml then I do all the required replacements. Once that is done I convert the HTML back to a wmlDocument using HtmlToWmlConverter.ConvertHtmlToWml.
    Everything seems to be working fine but I can’t get the images to display on the final wmlDocument.

    Can someone help me? I think the issue is I am not saving the file to the file system instead I am working with memorystreams. My code is the following:

    Dim template As Byte() = System.IO.File.ReadAllBytes(strFullyQualifiedTemplateFile)
        Dim ms2 As New memorystream
        Dim wmlDocument As WmlDocument
        Dim convertedHtml As XElement
        Dim finalconvertedhtml As string
        Dim imageDirectoryName As String
        Dim strWebUNCPath As String = ""
        Dim strUploadPath As String = ""
        Dim imageCounter As Integer = 0       
          strWebUNCPath = GlobalFunctions.GetAppSetting("Path_Web")
        Catch ex As Exception
          strWebUNCPath = ""
        End Try
          strUploadPath = GlobalFunctions.GetAppSetting("Path_Upload")
        Catch ex As Exception
          strUploadPath = ""
        End Try
        imageDirectoryName= strWebUNCPath & strUploadPath
        Using ms As New MemoryStream()
          ms.Write(template, 0, template.Length)
          Using doc As WordprocessingDocument = WordprocessingDocument.Open(ms, True)
            Dim pageTitle = "Document"
            Dim settings As New WmlToHtmlConverterSettings() With { 
    	       .PageTitle = pageTitle, 
    	       .FabricateCssClasses = True, 
    	       .CssClassPrefix = "pt-", 
    	       .RestrictToSupportedLanguages = False, 
    	       .RestrictToSupportedNumberingFormats = False,
             .ImageHandler = Function(imageInfo)
                              Dim localDirInfo As New DirectoryInfo(imageDirectoryName)
                              If Not localDirInfo.Exists Then
                              End If
                              imageCounter += 1
                              Dim extension As String = imageInfo.ContentType.Split("/"C)(1).ToLower()
                              Dim imageFormat__1 As ImageFormat = Nothing
                              If extension = "png" Then
                                ' Convert png to jpeg.
                                extension = "gif"
                                imageFormat__1 = ImageFormat.Gif
                              ElseIf extension = "gif" Then
                                imageFormat__1 = ImageFormat.Gif
                              ElseIf extension = "bmp" Then
                                imageFormat__1 = ImageFormat.Bmp
                              ElseIf extension = "jpeg" Then
                                imageFormat__1 = ImageFormat.Jpeg
                              ElseIf extension = "tiff" Then
                                ' Convert tiff to gif.
                                extension = "gif"
                                imageFormat__1 = ImageFormat.Gif
                              ElseIf extension = "x-wmf" Then
                                extension = "wmf"
                                imageFormat__1 = ImageFormat.Wmf
                              End If
                              'If the image format isn't one that we expect, ignore it,
                              'and don't return markup for the link.
                              If imageFormat__1 Is Nothing Then
                                Return Nothing
                              End If
                              Dim imageFileName As String = Convert.ToString(imageDirectoryName + "image" + imageCounter.ToString() + ".") & extension
                                imageInfo.Bitmap.Save(imageFileName, imageFormat__1)
                              Catch generatedExceptionName As System.Runtime.InteropServices.ExternalException
                                Return Nothing
                              End Try
                              Dim img As New XElement(Xhtml.img, New XAttribute(NoNamespace.src, imageFileName), imageInfo.ImgStyleAttribute, If(imageInfo.AltText IsNot Nothing, New XAttribute(NoNamespace.alt, imageInfo.AltText), Nothing))
                              Return img
                          End Function 
           convertedHtml = WmlToHtmlConverter.ConvertToHtml(doc, settings)
            Dim usedAuthorCss As String = HtmlToWmlConverter.CleanUpCss(Cstr(convertedHtml.Descendants().FirstOrDefault(Function(d) d.Name.LocalName.ToLower() = "style")))
            finalconvertedhtml = convertedHtml.ToString        
            For Each field As KeyValuePair(Of String, string) In lstFieldValues
              Dim identifier As String = "{" & field.Key & "}"
              If finalconvertedhtml.Contains(identifier) Then
                finalconvertedhtml = finalconvertedhtml.Replace(identifier, field.Value)
              End If
            convertedHtml = XElement.Parse(finalconvertedhtml)
           Dim  settings1 as HtmlToWmlConverterSettings = HtmlToWmlConverter.GetDefaultSettings()
              settings1.BaseUriForImages = imageDirectoryName
            wmlDocument =HtmlToWmlConverter.ConvertHtmlToWml(string.Empty, usedAuthorCss, string.Empty, convertedHtml, settings1)
          End Using
          Return ms
        End Using
Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.