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
297 stars 106 forks source link

Compatibilities Issues with .Net 7.0 #133

Closed vibhor-github closed 6 months ago

vibhor-github commented 1 year ago

We are migrating existing console app based on .net framework 4.6.2 to .Net 7.0. Existing app is using HtmlToOpenXml 1.6.0 version without any issues. We updated the HtmlToOpenXml version in latest .Net 7.0 app but getting compatibility issues. Please advise how to use HtmlToOpenXml dll (either 1.6.0 or latest 2.3.0 version) with .Net 7.0 console app without any compatibility issues.

Refer below the Function where we are using HtmlToOpenXml library. private static void FindBookmarksAndReplaceWithHtml(WordprocessingDocument document, string bookmarkName, string html, DocumentPart partType, MainDocumentPart part, RunProperties runProperties = null, ParagraphProperties paragraphProp = null, bool persistParagraphStyle = true, bool resetEnd = false, bool removeOnlyBookMark = false) {

        RunProperties runProp = runProperties == null ? (RunProperties)null : (RunProperties)(runProperties.CloneNode(true));
        var bmRefs = GetBookmarkReferences(bookmarkName, partType, part);
        if (bmRefs == null)
        {
            return;
        }
        if (!bmRefs.IsValid)
        {
            return;
        }
        //setting default border to "1" for html table
        if (!string.IsNullOrEmpty(html))
        {
            html = html.Replace("<table", "<table border='1' cellpadding='4' style='border-collapse:separate;' desiredWidth='100%'");
            //Replace del tag with s
            html = html.Replace("<del", "<s");
            html = html.Replace("</del>", "</s>");
        }

        BookmarkStart bmstart = bmRefs.BmStart;
        BookmarkEnd bmend = bmRefs.BmEnd;

        if (bmend.Parent != bmstart.Parent && resetEnd)
        {
            BookmarkEnd bmEndTemp = (BookmarkEnd)bmend.Clone();
            OpenXmlElement currentSibling = null;
            OpenXmlElement nextSibling = bmstart.NextSibling();
            while (nextSibling != null)
            {
                currentSibling = nextSibling;
                nextSibling = nextSibling.NextSibling();
            }
            currentSibling = currentSibling ?? bmstart;
            bmstart.Parent.InsertAfter(bmEndTemp, currentSibling);
            bmend.Remove();
            bmend = bmEndTemp;
        }

        if (runProp != null)
        {
            var children = runProp.GetFirstChild<Underline>();
            if (children == null)
            {
                var hasUnderline = BookmarkRunHasUnderline(partType, part, bookmarkName);
                UpdateRunProperties(runProp, underline: hasUnderline);
            }
        }

        //Check, if new html should be append
        OpenXmlElement siblingElement = bmstart.Parent.PreviousSibling() ?? bmstart.Parent.Parent;
        RunProperties rProp = runProp == null ? (from rp in bmstart.Parent.Descendants<Run>() where rp.RunProperties != null select rp.RunProperties).FirstOrDefault() : (RunProperties)runProp.Clone();

        //New run with the style of the first run, which was removed
        var newRun = new Run();
        if (runProp != null)
        {
            newRun.RunProperties = (RunProperties)runProp.Clone();
        }
        else if (rProp != null)
        {
            newRun.RunProperties = (RunProperties)rProp.Clone();
        }

        var rs = from bm in part.Document.Body.Descendants<BookmarkStart>() where bm.Name == bookmarkName select bm;
        if (!string.IsNullOrEmpty(html))
        {
            html = string.Format(@"<html><head/><body>{0}</body></html>", html);
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(html));

            **var converter = new NotesFor.HtmlToOpenXml.HtmlConverter(part);
            var openXmlParsed = converter.Parse(html);**

            OpenXmlElement processingElement = bmstart;
            var isInTableCell = false;
            do
            {
                processingElement = processingElement.Parent;
                if (processingElement is TableCell)
                {
                    isInTableCell = true;
                    break;
                }
            } while (!(processingElement is Body));

            var parent = bmstart.Parent;
            if (parent is Paragraph)
            {
                var sibling = parent.PreviousSibling();
                bmstart.Remove();
                bmend.Remove();
                parent.Remove();
                if (isInTableCell && !(openXmlParsed[openXmlParsed.Count - 1] is Paragraph))
                {
                    sibling.InsertAfterSelf(new Paragraph());
                }
                for (var loop = openXmlParsed.Count - 1; loop >= 0; loop--)
                {
                    sibling.InsertAfterSelf(openXmlParsed[loop]);
                }
            }
        }
    }
code-tourist commented 6 months ago

I'm also having issues when calling .Parse(string html) The error I receive is: 'System.InvalidProgramException' Common Language Runtime detected an invalid program.'

This started to occur when updating my app from .NETCore3.1 to .NET8.0

onizet commented 6 months ago

Hi, I have published a new version 2.4. It supports natively .NET 8.0 now

code-tourist commented 6 months ago

Hello.

I've updated my project with the latest package, it working again.

Thx alot man!