This example demonstrates how to loop through all hyperlinks in a document and display simple summary
If you need to check like how many hyperlinks are there in your word document you need to first find all the hyperlinks and then categorised them depending upon types
First we choose a word file using file browser which has hyperlinks
We have several version of this representation in words ,using dropdown you can make a choice within the following
After clicking generate button it will launch a word file depending upon the above made choices you will see a word file and a message box telling about the number of hyperlinks
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using OfficeComponent.Word;
namespace OfficeComponent.Samples
{
class HyperlinkSummaryExample : WordExampleBase
#if WEB
, IUIExample
#endif
{
public string FileName;
public HyperlinkSummaryExample(string commonDataPath, string outputDir)
: base(commonDataPath, outputDir)
{
}
public HyperlinkSummaryExample(string commonDataPath, string outputDir, string xmlFile) : base(commonDataPath, outputDir, xmlFile)
{
}
public override string Execute()
{
#if WEB
ProcessForm();
#endif
if (string.IsNullOrWhiteSpace(FileName))
{
ShowError("Please specify the Word file name");
return null;
}
// Create a new instance of PdfDocument class.
WordDocument document = new WordDocument(FileName);
List webLinks = new List();
List emailLinks = new List();
List fileLinks = new List();
List bookmarks = new List();
GetHyperLinks(document, webLinks, emailLinks, fileLinks, bookmarks);
ShowInfo(string.Format("The document has {0} Web Link(s), {1} E-mail Link(s), {2} File Link(s), and {3} Bookmark(s).", webLinks.Count,
emailLinks.Count,
fileLinks.Count,
bookmarks.Count));
// Close the document.
document.Close();
return null;
}
private static void GetHyperLinks(WordDocument document, List webLinks, List emailLinks, List fileLinks, List bookmarks)
{
foreach (Section section in document.Sections)
{
foreach (Paragraph paragraph in section.Body.Paragraphs)
{
foreach (ParagraphItem item in paragraph.Items)
{
Field field = item as Field;
if (field != null && field.FieldType == FieldType.FieldHyperlink)
{
Hyperlink hlink = new Hyperlink(field);
switch (hlink.Type)
{
case HyperlinkType.WebLink:
if (hlink.PictureToDisplay == null)
webLinks.Add(hlink);
break;
case HyperlinkType.EMailLink:
emailLinks.Add(hlink);
break;
case HyperlinkType.FileLink:
fileLinks.Add(hlink);
break;
case HyperlinkType.Bookmark:
bookmarks.Add(hlink);
break;
}
}
}
}
}
}
public override string ActionTitle
{
get
{
return "Summarize";
}
}
#if WEB
void ProcessForm()
{
FileName = GetPostFile("SourceFile");
}
#endif
}
}
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Text
Imports OfficeComponent.Word
Namespace OfficeComponent.Samples
#If WEB Then
Friend Class HyperlinkSummaryExample
Inherits WordExampleBase
Implements IUIExample
#Else
Friend Class HyperlinkSummaryExample
Inherits WordExampleBase
#End If
Public FileName As String
Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String)
MyBase.New(commonDataPath, outputDir)
End Sub
Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String, ByVal xmlFile As String)
MyBase.New(commonDataPath, outputDir, xmlFile)
End Sub
Public Overrides Function Execute() As String
#If WEB Then
ProcessForm()
#End If
If String.IsNullOrWhiteSpace(FileName) Then
ShowError("Please specify the Word file name")
Return Nothing
End If
' Create a new instance of PdfDocument class.
Dim document As New WordDocument(FileName)
Dim webLinks As New List(Of Hyperlink)()
Dim emailLinks As New List(Of Hyperlink)()
Dim fileLinks As New List(Of Hyperlink)()
Dim bookmarks As New List(Of Hyperlink)()
GetHyperLinks(document, webLinks, emailLinks, fileLinks, bookmarks)
ShowInfo(String.Format("The document has {0} Web Link(s), {1} E-mail Link(s), {2} File Link(s), and {3} Bookmark(s).", webLinks.Count, emailLinks.Count, fileLinks.Count, bookmarks.Count))
' Close the document.
document.Close()
Return Nothing
End Function
Private Shared Sub GetHyperLinks(ByVal document As WordDocument, ByVal webLinks As List(Of Hyperlink), ByVal emailLinks As List(Of Hyperlink), ByVal fileLinks As List(Of Hyperlink), ByVal bookmarks As List(Of Hyperlink))
For Each section As Section In document.Sections
For Each paragraph As Paragraph In section.Body.Paragraphs
For Each item As ParagraphItem In paragraph.Items
Dim field As Field = TryCast(item, Field)
If field IsNot Nothing AndAlso field.FieldType = FieldType.FieldHyperlink Then
Dim hlink As New Hyperlink(field)
Select Case hlink.Type
Case HyperlinkType.WebLink
If hlink.PictureToDisplay Is Nothing Then
webLinks.Add(hlink)
End If
Case HyperlinkType.EMailLink
emailLinks.Add(hlink)
Case HyperlinkType.FileLink
fileLinks.Add(hlink)
Case HyperlinkType.Bookmark
bookmarks.Add(hlink)
End Select
End If
Next item
Next paragraph
Next section
End Sub
Public Overrides ReadOnly Property ActionTitle() As String
Get
Return "Summarize"
End Get
End Property
#If WEB Then
Private Sub ProcessForm()
FileName = GetPostFile("SourceFile")
End Sub
#End If
End Class
End Namespace