[]
GcWord allows you to add, modify, and delete hyperlinks in a document. In GcWord, hyperlink element is represented by the Hyperlink class. You can add a hyperlink in a document using Add method of the HyperlinkCollection class. It can also be modified using the Hyperlink class properties, and deleted using Delete method of the ContentObject class.
To add a hyperlink in a document:
Access a section in a document where the hyperlink is to be added.
Add a paragraph to the section using Add method of the ParagraphCollection class.
Add a hyperlink to the paragraph using Add method of the HyperlinkCollection class.
var section = doc.Body.Sections.First;
//Add the first paragraph
var p = section.GetRange().Paragraphs.Add(
"Among other things, fields allow to insert hyperlinks into documents." +
" Following is a hyperlink to a web address. ");
//Add a hyperlink to it
Hyperlink link1 = p.GetRange().Hyperlinks.Add(new Uri("http://www.google.com"),
"", "Click to go to www.google.com.");
//Save the document
doc.Save("AddHyperlink.docx");
To modify a hyperlink:
Access a hyperlink from the hyperlink collection using Hyperlinks property of the RangeBase class. For example, access the first hyperlink of the collection.
Modify the address for the specified link using Address property of the Hyperlink class.
Modify the text content value for the link using Value property of the Text class.
//Load the existing word document in GcWord instance
doc.Load("AddHyperlink.docx");
//Modify the hyperlink code
Hyperlink link1 =
doc.Body.Sections.First.GetRange().Paragraphs.First.GetRange().Hyperlinks.First;
link1.Address = new Uri("http://www.grapecity.com");
link1.GetRange().Texts[0].Value = "Click to visit Grapecity website";
//Save the document
doc.Save("ModifyHyperlink.docx");
To delete a hyperlink:
Access a hyperlink from the hyperlink collection using Hyperlinks property of the RangeBase class. For example, access the first hyperlink of the collection.
Delete the field using the Delete method of the ContentObject class.
//Load the existing word document in GcWord instance
doc.Load("AddHyperlink.docx");
//Delete hyperlink to bookmark
doc.Body.Sections.First.GetRange().Paragraphs.First.GetRange().Hyperlinks.First.Delete();
//Save the document
doc.Save("DeleteHyperlink.docx");
To load a document containing malformed URI in GcWord, the URI needs to be rewritten in a valid manner. GcWord provides MalformedUriRewriter property in GcWordDocument class which encapsulates two delegates namely, OnLoadInvalidUriHandler and OnSaveInvalidUriHandler which are called on loading malformed URI event or on saving any URI from the document, respectively.
The DefaultMalformedUriRewriter class implements the IMalformedUriRewriter interface and provides the default rewriting strategy for handling invalid URIs.
You can also define any custom URI rewriter implementation which overrides the delegates and rewrites malformed URI as defined in the custom implementation.
The below template document contains malformed URI and is loaded in GcWord after using MalformedUriRewriter property to handle it. Refer to Report Templates to know more more about templates.
const string templateDocFileName = @"example_malformed_mailto.docx";
public void DemoDefaultRewriter()
{
var doc = new GcWordDocument();
try
{
Console.WriteLine("Loading file...");
doc.Load(templateDocFileName);
}
catch (UriFormatException)
{
Console.WriteLine("File contains malformed urls and cannot be loaded without handling such urls.");
}
//apply malformed rewriter(default implementation).
Console.WriteLine("Apply malformed rewriter (default implementation)...");
doc.MalformedUriRewriter = new DefaultMalformedUriRewriter();
Console.WriteLine("Loading file...");
doc.Load(templateDocFileName);
var contacts = new[]
{
new{ name = "x y",company = "A company", email = @"xy@a_company.com"},
new{ name = "a b",company = "B company", email = @"ab@b.com"},
new{ name = "c d",company = "C company", email = @"cd@C_company.com"},
};
doc.DataTemplate.DataSources.Add("ds", contacts);
Console.WriteLine("Filling template...");
int i = 1;
doc.DataTemplate.BatchProcess(() =>
{
var fileName = String.Format("gen{0}.docx", i++);
Console.WriteLine(string.Format("Generated template file {0}", fileName));
doc.Save(Path.Combine(fileName));
});
}
For more information on how to implement hyperlinks using GcWord, see GcWord sample browser.