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:
<a>
) with the href attribute pointing to the ID of the target element.Complete code
using System.Text;
using System.Linq;
using SautinSoft.Document;
namespace Sample
{
class Sample
{
static void Main(string[] args)
{
// Get your free 100-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 });
}
}
}
Imports System.Text
Imports System.Linq
Imports SautinSoft.Document
Namespace Sample
Friend Class Sample
Shared Sub Main(ByVal args() As String)
' Get your free 100-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
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: