Sommario
Installato tramite NuGet
PM> Install-Package Spire.Doc
Link correlati
Un collegamento ipertestuale all'interno di un documento Word consente ai lettori di passare dalla sua posizione a un punto diverso all'interno del documento, a un file o sito Web diverso o a un nuovo messaggio di posta elettronica. I collegamenti ipertestuali rendono semplice e veloce per i lettori la navigazione verso le informazioni correlate. Questo articolo illustra come aggiungere collegamenti ipertestuali a testo o immagini in C# e VB.NET utilizzando Spire.Doc for .NET.
- Inserisci collegamenti ipertestuali quando aggiungi paragrafi a Word
- Aggiungi collegamenti ipertestuali al testo esistente in Word
Installa Spire.Doc for .NET
Per cominciare, devi aggiungere i file DLL inclusi nel pacchetto Spire.Doc for.NET come riferimenti nel tuo progetto .NET. I file DLL possono essere scaricati da questo link o installato tramite NuGet.
PM> Install-Package Spire.Doc
Inserisci collegamenti ipertestuali quando aggiungi paragrafi a Word
Spire.Doc offre il metodo Paragraph.AppendHyperlink() per aggiungere un collegamento Web, un collegamento e-mail, un collegamento a un file o un collegamento a un segnalibro a una parte di testo o un'immagine all'interno di un paragrafo. Di seguito sono riportati i passaggi dettagliati.
- Creare un oggetto Documento.
- Aggiungi una sezione e un paragrafo.
- Inserisci un collegamento ipertestuale basato sul testo utilizzando il metodo Paragraph.AppendHyerplink(string link, string text, HyperlinkType type).
- Aggiungi un'immagine al paragrafo utilizzando il metodo Paragraph.AppendPicture().
- Inserisci un collegamento ipertestuale basato sull'immagine utilizzando il metodo Paragraph.AppendHyerplink(string link, Spire.Doc.Fields.DocPicture picture, HyperlinkType type).
- Salva il documento utilizzando il metodo Document.SaveToFile().
- C#
- VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
namespace InsertHyperlinks
{
class Program
{
static void Main(string[] args)
{
//Create a Word document
Document doc = new Document();
//Add a section
Section section = doc.AddSection();
//Add a paragraph
Paragraph paragraph = section.AddParagraph();
paragraph.AppendHyperlink("https://www-iceblue.com/", "Home Page", HyperlinkType.WebLink);
//Append line breaks
paragraph.AppendBreak(BreakType.LineBreak);
paragraph.AppendBreak(BreakType.LineBreak);
//Add an email link
paragraph.AppendHyperlink("mailto:support@e-iceblue.com", "Mail Us", HyperlinkType.EMailLink);
//Append line breaks
paragraph.AppendBreak(BreakType.LineBreak);
paragraph.AppendBreak(BreakType.LineBreak);
//Add a file link
string filePath = @"C:\Users\Administrator\Desktop\report.xlsx";
paragraph.AppendHyperlink(filePath, "Click to open the report", HyperlinkType.FileLink);
//Append line breaks
paragraph.AppendBreak(BreakType.LineBreak);
paragraph.AppendBreak(BreakType.LineBreak);
//Add another section and create a bookmark
Section section2 = doc.AddSection();
Paragraph bookmarkParagrapg = section2.AddParagraph();
bookmarkParagrapg.AppendText("Here is a bookmark");
BookmarkStart start = bookmarkParagrapg.AppendBookmarkStart("myBookmark");
bookmarkParagrapg.Items.Insert(0, start);
bookmarkParagrapg.AppendBookmarkEnd("myBookmark");
//Link to the bookmark
paragraph.AppendHyperlink("myBookmark", "Jump to a location inside this document", HyperlinkType.Bookmark);
//Append line breaks
paragraph.AppendBreak(BreakType.LineBreak);
paragraph.AppendBreak(BreakType.LineBreak);
//Add an image link
Image image = Image.FromFile(@"C:\Users\Administrator\Desktop\logo.png");
Spire.Doc.Fields.DocPicture picture = paragraph.AppendPicture(image);
paragraph.AppendHyperlink("https://docs.microsoft.com/en-us/dotnet/", picture, HyperlinkType.WebLink);
//Save to file
doc.SaveToFile("InsertHyperlinks.docx", FileFormat.Docx2013);
}
}
}

Aggiungi collegamenti ipertestuali al testo esistente in Word
Aggiungere collegamenti ipertestuali al testo esistente in un documento è un po' più complicato. Dovrai prima trovare la stringa di destinazione, quindi sostituirla nel paragrafo con un campo di collegamento ipertestuale. Di seguito sono riportati i passaggi.
- Creare un oggetto Documento.
- Carica un file Word utilizzando il metodo Document.LoadFromFile().
- Trova tutte le occorrenze della stringa di destinazione nel documento utilizzando il metodo Document.FindAllString() e ottieni quella specifica in base al suo indice dalla raccolta.
- Ottieni il paragrafo della stringa e la sua posizione al suo interno.
- Rimuovere la stringa dal paragrafo.
- Crea un campo collegamento ipertestuale e inseriscilo nella posizione in cui si trova la stringa.
- Salva il documento in un altro file utilizzando il metodo Document.SaveToFle().
- C#
- VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using Spire.Doc.Interface;
namespace AddHyperlinksToExistingText
{
class Program
{
static void Main(string[] args)
{
//Create a Document object
Document document = new Document();
//Load a Word file
document.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.docx");
//Find all the occurrences of the string ".NET Framework" in the document
TextSelection[] selections = document.FindAllString(".NET Framework", true, true);
//Get the second occurrence
TextRange range = selections[1].GetAsOneRange();
//Get its owner paragraph
Paragraph parapgraph = range.OwnerParagraph;
//Get its position in the paragraph
int index = parapgraph.Items.IndexOf(range);
//Remove it from the paragraph
parapgraph.Items.Remove(range);
//Create a hyperlink field
Spire.Doc.Fields.Field field = new Spire.Doc.Fields.Field(document);
field.Type = Spire.Doc.FieldType.FieldHyperlink;
Hyperlink hyperlink = new Hyperlink(field);
hyperlink.Type = HyperlinkType.WebLink;
hyperlink.Uri = "https://en.wikipedia.org/wiki/.NET_Framework";
parapgraph.Items.Insert(index, field);
//Insert a field mark "start" to the paragraph
IParagraphBase start = document.CreateParagraphItem(ParagraphItemType.FieldMark);
(start as FieldMark).Type = FieldMarkType.FieldSeparator;
parapgraph.Items.Insert(index + 1, start);
//Insert a text range between two field marks
ITextRange textRange = new Spire.Doc.Fields.TextRange(document);
textRange.Text = ".NET Framework";
textRange.CharacterFormat.Font = range.CharacterFormat.Font;
textRange.CharacterFormat.TextColor = System.Drawing.Color.Blue;
textRange.CharacterFormat.UnderlineStyle = UnderlineStyle.Single;
parapgraph.Items.Insert(index + 2, textRange);
//Insert a field mark "end" to the paragraph
IParagraphBase end = document.CreateParagraphItem(ParagraphItemType.FieldMark);
(end as FieldMark).Type = FieldMarkType.FieldEnd;
parapgraph.Items.Insert(index + 3, end);
//Save to file
document.SaveToFile("AddHyperlink.docx", Spire.Doc.FileFormat.Docx);
}
}
}

Richiedi una licenza temporanea
Se desideri rimuovere il messaggio di valutazione dai documenti generati o eliminare le limitazioni della funzione, per favore richiedere una licenza di prova di 30 giorni per te.