onizet / html2openxml

Html2OpenXml is a small .Net library that convert simple or advanced HTML to plain OpenXml components. This program has started in 2009, initially to convert user's comments from SharePoint to Word.
MIT License
306 stars 107 forks source link

Import html images into a docx created from template not working #55

Closed blau1981 closed 2 months ago

blau1981 commented 5 years ago

Hi, We recently updated from 2.0.1 to 2.0.2 and after the update the import of the images from html file to docx is not longer working when the document is created from a template. The document is created from a blank template.

        var templatePath = "Empty.dotx";
        var html = File.ReadAllText(path + "\\ImagePage.html");
        var doc = WordprocessingDocument.CreateFromTemplate(templatePath);
        var mainPart = doc.MainDocumentPart;
        HtmlConverter converter = new HtmlConverter(mainPart)
        {
            ImageProcessing = ImageProcessing.ManualProvisioning,
            BaseImageUrl = new Uri(path)
        };
        converter.ProvisionImage += OnProvisionImage;
        converter.ParseHtml(html);
        doc.SaveAs(@"D:\temp\doc.docx");
        doc.Close();

Thanks

onizet commented 5 years ago

Could you share with me the content of OnProvisionImage? I don't know what is behind doc.SaveAs but ensure you have called mainPart.Document.Save() to persist the parsed chunks into the document.

blau1981 commented 5 years ago

This is the the code: static void OnProvisionImage(object sender, ProvisionImageEventArgs e) { var documentPath = (sender as HtmlConverter).BaseImageUrl.OriginalString; string fileName = e.ImageUrl.OriginalString; e.Provision(File.ReadAllBytes(fileName)); } I need to use SaveAs because it comes from a template and I it does not have an option on create for filename. Html is like this:

<body>
    <div>test message</div>
    <div>
        <table>
            <tr>
                <td>
                    <img src="image.png" />
                </td>
            </tr>
        </table>
        <br />
    </div>
</body>
onizet commented 2 months ago

The code is exposing a IWebRequest interface to allow custom implementation.