Integrating Anchor Links in Web Documents with C# and .NET

Anchor links are a fundamental part of web content that allows users to navigate through a webpage effectively. In this article, we will explore how to insert anchor links inside an HTML page using C# and .NET with the help of library Sautinsoft.Document.

Anchor links enhance user experience by enabling seamless navigation within long web pages. Experiment with different styling options and customization features offered by the library Sautinsoft.Document to further optimize the anchor link insertion process.

Understanding Anchor Links:

  • Anchor links are HTML elements that define a hyperlink to a specific part of the same webpage.
  • They are commonly used in table of contents, navigation menus, and linking within long articles.
  • Anchor links consist of an anchor tag (<a>) with the href attribute pointing to the ID of the target element.
    1. Add SautinSoft.Document from Nuget.
    2. Load or create a new document.
    3. Add a new section.
    4. Add a new paragraph with a link binding to the end of the document.
    5. Add 100 paragraphs.
    6. Save the document in HTML format.
    Insert Anchor Links Inside The HTML Page

    Complete code

    using System.Text;
    using System.Linq;
    using SautinSoft.Document;
    
    namespace Sample
    {
        class Sample
        {
            static void Main(string[] args)
            {
                // Get your free 30-day key here:
                // https://sautinsoft.com/start-for-free/
    
                AnchorLinks();
            }
    
            /// <summary>
            /// Insert anchor links inside the HTML page using C# and .NET
            /// </summary>
            /// <remarks>
            /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/insert-anchor-links-inside-an-html-page-using-csharp-vb-net.php
            /// </remarks>
            public static void AnchorLinks()
            {
                // P.S. If you are using MS Word, to display bookmarks:
                // File -> Options -> Advanced -> On the "Show document content" check "Show bookmarks".
                string documentPath = @"AnchorLinks.html";
    
                // Let's create a new document.
                DocumentCore dc = new DocumentCore();
                dc.Sections.Add(new Section(dc));
    
                // Add an anchor links to the end of the document.
                dc.Sections[0].Blocks.Add(
                new Paragraph(dc,
                new Hyperlink(dc, "IdEnd", "Document End") { IsBookmarkLink = true }));
    
                // Add 100 paragraphs
                for (int i = 0; i < 100; i++)
                    dc.Sections[0].Blocks.Add(new Paragraph(dc, new Run(dc, $"Paragraph {i + 1}")));
    
                dc.Sections[0].Blocks.Add(
                    new Paragraph(dc,
                        new BookmarkStart(dc, "IdEnd"),
                        new Run(dc, "The document end."),
                        new BookmarkEnd(dc, "IdEnd")));
    
                // Let's save the document as HTML.
                dc.Save(documentPath, new HtmlFlowingSaveOptions());
    
                // Open the result for demonstration purposes.
                System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(documentPath) { UseShellExecute = true });
            }
    
        }
    }

    Download

    Imports System.Text
    Imports System.Linq
    Imports SautinSoft.Document
    
    Namespace Sample
    	Friend Class Sample
    		Shared Sub Main(ByVal args() As String)
    			' Get your free 30-day key here:   
    			' https://sautinsoft.com/start-for-free/
    
    			AnchorLinks()
    		End Sub
    
    		''' <summary>
    		''' Insert anchor links inside the HTML page using C# and .NET
    		''' </summary>
    		''' <remarks>
    		''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/insert-anchor-links-inside-an-html-page-using-csharp-vb-net.php
    		''' </remarks>		
    		Public Shared Sub AnchorLinks()
    			' P.S. If you are using MS Word, to display bookmarks:
    			' File -> Options -> Advanced -> On the "Show document content" check "Show bookmarks".
    			Dim documentPath As String = "AnchorLinks.html"
    
    			' Let's create a new document.
    			Dim dc As New DocumentCore()
    			dc.Sections.Add(New Section(dc))
    
    			' Add an anchor links to the end of the document.
    			dc.Sections(0).Blocks.Add(New Paragraph(dc, New Hyperlink(dc, "IdEnd", "Document End") With {.IsBookmarkLink = True}))
    
    			' Add 100 paragraphs
    			For i As Integer = 0 To 99
    				dc.Sections(0).Blocks.Add(New Paragraph(dc, New Run(dc, $"Paragraph {i + 1}")))
    			Next i
    
    			dc.Sections(0).Blocks.Add(New Paragraph(dc, New BookmarkStart(dc, "IdEnd"), New Run(dc, "The document end."), New BookmarkEnd(dc, "IdEnd")))
    
    			' Let's save the document as HTML.
    			dc.Save(documentPath, New HtmlFlowingSaveOptions())
    
    			' Open the result for demonstration purposes.
    			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(documentPath) With {.UseShellExecute = True})
    		End Sub
    
    	End Class
    End Namespace
    

    Download


    If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



    Questions and suggestions from you are always welcome!

    We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.