Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
blau1981 opened this issue May 28, 2019 · 2 comments
Open

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

blau1981 opened this issue May 28, 2019 · 2 comments

Comments

@blau1981
Copy link

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
Copy link
Owner

onizet commented Jun 19, 2019

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
Copy link
Author

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>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants