Использование DocumentBuilder для вставки Гиперссылки на C# и .NET

  1. Добавьте SautinSoft.Document из Nuget.
  2. Создайте новый документ.
  3. Создайте Builder на основе документа.
  4. Запустите метод InsertHyperlink.

   Используйте метод DocumentBuilderInsertHyperlink для вставки гиперссылки в документ.

Скачать полученный файл в формате PDF: Result-insert hyperlink.pdf

Полный код

using System;
using SautinSoft.Document;
using System.Text;
using SautinSoft.Document.Drawing;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get your free 30-day key here:   
            // https://sautinsoft.com/start-for-free/
            InsertingHyperlink();
        }
        /// <summary>
        /// Insert a hyperlink into a document using DocumentBuilder.
        /// </summary>
        /// <remarks>
        /// Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/documentbuilder-inserting-hyperlink.php
        /// </remarks>

        static void InsertingHyperlink()
        {
            DocumentCore dc = new DocumentCore();
            DocumentBuilder db = new DocumentBuilder(dc);

            // Insert the formatted text into the document.
            db.CharacterFormat.FontName = "Verdana";
            db.CharacterFormat.Size = 16;
            db.Writeln("Insert a hyperlink into a document using DocumentBuilder.");

            // Inserts a Word field into a document.
            db.CharacterFormat.Size = 26;
            db.CharacterFormat.FontColor = Color.Brown;
            db.InsertField("DATE");
            db.InsertSpecialCharacter(SpecialCharacterType.LineBreak);

            // Insert URL hyperlink.
            db.CharacterFormat.FontColor = Color.Blue;
            db.CharacterFormat.UnderlineStyle = UnderlineType.Dashed;
            db.InsertHyperlink("Welcome to SautinSoft!", "https://sautinsoft.com", false);

            db.InsertSpecialCharacter(SpecialCharacterType.PageBreak);

            // Insert a hyperlink inside a document as a bookmark.
            db.CharacterFormat.FontColor = Color.Brown;
            db.CharacterFormat.UnderlineStyle = UnderlineType.DotDotDash;
            db.InsertHyperlink("back to the field {DATE}", "DATE", true);

            // Save our document into DOCX format.
            string resultPath = @"Result.docx";
            dc.Save(resultPath, new DocxSaveOptions());

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(resultPath) { UseShellExecute = true });
        }
    }
}

Download

Imports System
Imports SautinSoft.Document
Imports System.Text
Imports SautinSoft.Document.Drawing

Namespace Example
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			InsertingHyperlink()
		End Sub
                ''' Get your free 30-day key here:   
                ''' https://sautinsoft.com/start-for-free/
		''' <summary>
		''' Insert a hyperlink into a document using DocumentBuilder.
		''' </summary>
		''' <remarks>
		''' Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/documentbuilder-inserting-hyperlink.php
		''' </remarks>

		Private Shared Sub InsertingHyperlink()
			Dim dc As New DocumentCore()
			Dim db As New DocumentBuilder(dc)

			Dim resultPath As String = "result.docx"

			' Insert the formatted text into the document.
			db.CharacterFormat.FontName = "Verdana"
			db.CharacterFormat.Size = 16
			db.Writeln("Insert a hyperlink into a document using DocumentBuilder.")

			' Inserts a Word field into a document.
			db.CharacterFormat.Size = 26
			db.CharacterFormat.FontColor = Color.Brown
			db.InsertField("DATE")
			db.InsertSpecialCharacter(SpecialCharacterType.LineBreak)

			' Insert URL hyperlink.
			db.CharacterFormat.FontColor = Color.Blue
			db.CharacterFormat.UnderlineStyle = UnderlineType.Dashed
			db.InsertHyperlink("Welcome to SautinSoft!", "https://sautinsoft.com", False)

			db.InsertSpecialCharacter(SpecialCharacterType.PageBreak)

			' Insert a hyperlink inside a document as a bookmark.
			db.CharacterFormat.FontColor = Color.Brown
			db.CharacterFormat.UnderlineStyle = UnderlineType.DotDotDash
			db.InsertHyperlink("back to the field {DATE}", "DATE", True)

			' Save our document into DOCX format.
			dc.Save(resultPath, New DocxSaveOptions())

			' Open the result for demonstration purposes.
			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(resultPath) With {.UseShellExecute = True})
		End Sub
	End Class
End Namespace

Download


Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу [email protected] или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже:



Вопросы и предложения всегда приветствуются!

Мы разрабатываем компоненты .Net с 2002 года. Мы знаем форматы PDF, DOCX, RTF, HTML, XLSX и Images. Если вам нужна помощь в создании, изменении или преобразовании документов в различных форматах, мы можем вам помочь. Мы напишем для вас любой пример кода абсолютно бесплатно.