Knowledgebase (2345)
Children categories
In MS Word, a hyperlink is a clickable link that allows you to jump to a web page, a file, an email address, or even another location in the same document. It is undeniable that adding hyperlinks in Word documents is one of the most common operations in daily work, but there are times when you may also need to change the address or update the display text of an existing hyperlink. This article will demonstrate how to programmatically edit a hyperlink in a Word document using Spire.Doc for .NET.
Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.Doc
Edit a Hyperlink in a Word Document in C# and VB.NET
A hyperlink consists of two basic parts: the hyperlink address (URL) and its display text. With Spire.Doc for .NET, you are allowed to modify both the address and display text of an existing hyperlink using Field.Code and Field.FieldText properties. The detailed steps are as follows.
- Create a Document object.
- Load a sample Word document using Document.LoadFromFile() method.
- Create an object of List<Field>.
- Traverse through all body child objects of sections in the sample document to find all hyperlinks.
- Modify the address (URL) of a specified hyperlink using Field.Code property.
- Modify the display text of a specified hyperlink using Field.FieldText property.
- Save the document to another file using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Collections.Generic;
namespace ModifyHyperlink
{
class Program
{
static void Main(string[] args)
{
//Create a Document object
Document doc = new Document();
//Load a sample Word document
doc.LoadFromFile("Hyperlink.docx");
//Create an object of List
List<Field> hyperlinks = new List<Field>();
//Loop through the items in the sections to find all hyperlinks
foreach (Section section in doc.Sections)
{
foreach (DocumentObject sec in section.Body.ChildObjects)
{
if (sec.DocumentObjectType == DocumentObjectType.Paragraph)
{
//Loop through all paragraphs in the sections
foreach (DocumentObject para in (sec as Paragraph).ChildObjects)
{
if (para.DocumentObjectType == DocumentObjectType.Field)
{
Field field = para as Field;
if (field.Type == FieldType.FieldHyperlink)
{
hyperlinks.Add(field);
}
}
}
}
}
}
//Modify the address (URL) of the first hyperlink
hyperlinks[0].Code = "HYPERLINK \"" + "https://www.e-iceblue.com/Introduce/word-for-net-introduce.html" + "\"";
//Modify the display text of the first hyperlink
hyperlinks[0].FieldText = "Spire.Doc for .NET";
//Save the result document
doc.SaveToFile("EditHyperlinks.docx", FileFormat.Docx);
}
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Hyperlinks can point to files, emails, websites, imagers or video when readers click on it. Hyperlinks are widely used in word document for it is very convenient to direct readers to related, useful content. By using Spire.Doc, developers can add hyperlinks, finding hyperlinks and modify hyperlinks. This article will show you how to find all the existing hyperlinks in a word document in C#.
Download and install Spire.Doc for .NET and then add Spire.Doc.dll as reference in the downloaded Bin folder though the below path: "..\Spire.Doc\Bin\NET4.0\ Spire.Doc.dll". Here comes to the details of how to finding hyperlinks in C#.
Firstly, view the word document which contains many hyperlinks:

Please check the code of how to find all the hyperlinks in word document:
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Collections.Generic;
using System.Drawing;
namespace FindHyperlink
{
class Program
{
static void Main(string[] args)
{
Document doc = new Document();
doc.LoadFromFile("Spire.docx");
List hyperlinks = new List();
foreach (Section section in doc.Sections)
{
foreach (DocumentObject sec in section.Body.ChildObjects)
{
if (sec.DocumentObjectType == DocumentObjectType.Paragraph)
{
foreach (DocumentObject para in (sec as Paragraph).ChildObjects)
{
if (para.DocumentObjectType == DocumentObjectType.Field)
{
Field field = para as Field;
if (field.Type == FieldType.FieldHyperlink)
{
hyperlinks.Add(field);
}
}
}
}
}
}
}
The effective screenshot of the finding hyperlinks:


PDF documents are ubiquitous in modern applications, and efficient handling of these files is crucial for developers. In C#, working with PDF files using streams offers memory efficiency and flexibility, especially in web applications, cloud services, or when handling dynamic content.
Spire.PDF, a robust PDF processing library, simplifies these operations with its intuitive API. This article explores how to create, load, and save PDF documents using streams in C#, covering the following aspects.
- Why Use Streams for PDF Processing?
- Getting Started with Spire.PDF
- Create a PDF File and Save it to Stream in C#
- Load a PDF from Stream in C#
- Advanced Example: Merge Multiple PDFs by Streams
Why Use Streams for PDF Processing?
Stream-based PDF handling in C# offers significant advantages for modern applications:
- Memory Efficiency: Process large PDFs without loading entire files into memory.
- Scalability: Ideal for web applications and cloud environments.
- Reduced I/O Operations: Minimize disk reads/writes for faster performance.
- Flexibility: Integrate with APIs, databases, and cloud storage seamlessly.
Getting Started with Spire.PDF
Spire.PDF is a feature-rich library that simplifies PDF manipulation in .NET applications. Here’s how to install it:
- Via NuGet Package Manager (Recommended):
In Package Manager Console, run the following command to install:
PM> Install-Package Spire.PDF - 2Manual Installation:
Download the DLL files from the official website and add a reference to your project.
Create a PDF File and Save it to Stream in C#
This task can be divided into three main parts. Follow the steps below to dynamically create a PDF file and save it to the stream.
- Create a PDF document.
- Initialize an instance of the PdfDocument class to represent a PDF file.
- Add a page to the PDF file.
- Add content to the PDF (in this case, a simple text string).
- Use the PdfPageBase.Canvas.DrawString() method to draw a simple text string on the page. (Comprehensive guide: Create a PDF Document in C#)
- Spire.PDF also allows to add various elements to PDF, such as images, tables, form fields, digital signatures, etc.
- 3. Save to a Stream
- Initialize an instance of the FileStream or the MemoryStream class.
- Call the PdfDocument.SaveToStream(Stream stream) method to save the PDF file to stream.
Code Example:
- C#
using Spire.Pdf;
using System.IO;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace SavePdfToStream
{
class Program
{
static void Main(string[] args)
{
// Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
// Add a page
PdfPageBase page = pdf.Pages.Add();
// Draw text on the page
page.Canvas.DrawString("Hello, World!",
new PdfFont(PdfFontFamily.Helvetica, 30f),
new PdfSolidBrush(Color.Blue),
100, 100);
// Save the PDF file to Stream
FileStream toStream = new FileStream("SavePdfToStream.pdf", FileMode.OpenOrCreate);
pdf.SaveToStream(toStream);
toStream.Close();
pdf.Close();
}
}
}
A screenshot of the generated PDF:

Load a PDF from Stream in C#
Streams provide a flexible way to read PDFs from different sources, such as:
- FileStream: represents a stream of data stored in a physical file.
- MemoryStream: represents a stream of data stored in memory (RAM).
Their differences lie in:
| Feature | FileStream | MemoryStream |
| Storage | Disk | Memory (RAM) |
| Performance | Slower (disk I/O) | Faster (no disk access) |
| Use Case | Persistent files, large data | Temporary data, in-memory processing |
| Size Limit | Limited by disk space | Limited by available RAM |
The PdfDocument.LoadFromStream(Stream stream) method of Spire.PDF library supports loading an existing PDF from a stream directly. The code example is shown below:
- C#
using Spire.Pdf;
using System.IO;
namespace LoadPdfFromStream
{
class Program
{
static void Main(string[] args)
{
// Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
// Load PDF file from memory stream
MemoryStream stream = new MemoryStream();
File.OpenRead("sample.pdf").CopyTo(stream);
pdf.LoadFromStream(stream);
// Load PDF file from file stream
//FileStream stream = File.OpenRead("sample.pdf");
//pdf.LoadFromStream(stream);
// Save the PDF file to disk
pdf.SaveToFile("LoadPdfFromStream.pdf", FileFormat.PDF);
pdf.Close();
}
}
}
A screenshot of the loaded PDF file:

Advanced Example: Merge Multiple PDFs by Streams
By mastering the skills of loading and saving PDFs using streams, you can combine it with other PDF processing functions to perform advanced PDF stream operations.
Below is an example on how to combine multiple PDFs into a single document using streams:
- C#
using System.IO;
using Spire.Pdf;
namespace MergePDFsByStream
{
class Program
{
static void Main(string[] args)
{
// Specify the files to be merged
string[] pdfFiles = {
"MergePdfsTemplate_1.pdf",
"MergePdfsTemplate_2.pdf",
"MergePdfsTemplate_3.pdf"
};
// Initialize FileStreams for input PDF files
FileStream[] streams = new FileStream[pdfFiles.Length];
for (int i = 0; i < pdfFiles.Length; i++)
{
streams[i] = File.OpenRead(pdfFiles[i]);
}
// Merge the PDF files using the streams
PdfDocumentBase pdf = PdfDocument.MergeFiles(streams);
// Save the merged PDF file
pdf.Save("MergePDFByStream.pdf", FileFormat.PDF);
}
}
}
Conclusion
Working with PDF files in streams using C# and Spire.PDF provides a flexible and efficient way to handle document processing tasks. Whether you’re loading, modifying existing PDF documents, or saving PDF files, streams offer a memory-efficient alternative to traditional file-based approaches.
To learn more advanced PDF processing features, explore Spire.PDF’s official documentation.
Get a Free License
To fully experience the capabilities of Spire.PDF for .NET without any evaluation limitations, you can request a free 30-day trial license.