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 into templated Word.
MIT License
317 stars 107 forks source link

In-document Links with HTML anchor tag #115

Closed denizalpaslan closed 3 months ago

denizalpaslan commented 2 years ago

Issue Description

Anchor tags (with # links) are converted into links in word document, however they are not navigating to the specified place when clicked.

Environment

Using version 2.3.0 with .net 6.0 application.

Input

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
     <body>
          <a href="#bookmark1">Bookmark 1</a><br/>
          <a href="#bookmark2">Bookmark 2</a><br/>
          Looks how cool is <font size="x-large"><b>Open Xml</b></font>.
          Now with <font color="red"><u>HtmlToOpenXml</u></font>, it nevers been so easy to convert html.
          <p>
               If you like it, add me a rating on <a href="https://github.com/onizet/html2openxml">github</a>
          </p>
          <div id="bookmark1">
               some text here.some text here.some text here.some text here.some text here.some text here.some text here.some text here.
               some text here.some text here.some text here.some text here.some text here.some text here.some text here.some text here.
          </div>
          <div id="bookmark2">
               some more text...
          </div>
          <hr>
     </body>
</html>

Method to convert HTML to Docx

        private static void ConvertHtmlToWord(string outputFileName, string html)
        {
            using (MemoryStream generatedDocument = new MemoryStream())
            {
                using (WordprocessingDocument package = WordprocessingDocument.Create(generatedDocument, WordprocessingDocumentType.Document))
                {
                    MainDocumentPart mainPart = package.MainDocumentPart;
                    if (mainPart == null)
                    {
                        mainPart = package.AddMainDocumentPart();
                        new Document(new Body()).Save(mainPart);
                    }

                    HtmlConverter converter = new HtmlConverter(mainPart);
                    converter.ParseHtml(html);

                    mainPart.Document.Save();
                }

                File.WriteAllBytes(outputFileName, generatedDocument.ToArray());
            }
        }

Expected Behavior In the converted document it should navigate to the section specified in the link, when clicked.

Current Behavior Click on the link does not have any effect, it seems to be navigating to the top where it is already.

onizet commented 5 months ago

Hello, this has never really been implemented, but time to get this feature! Be sure to subscribe when v3 will be shipped

onizet commented 3 months ago

This is shipped in v3 now