Skip to content

Commit 0ea0762

Browse files
923754_Added sample
1 parent 6f510a1 commit 0ea0762

4 files changed

Lines changed: 102 additions & 0 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Solution>
2+
<Project Path="Add-docvariable-in-hyperlink-field/Add-docvariable-in-hyperlink-field.csproj" />
3+
</Solution>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Add_docvariable_in_hyperlink_field</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIORenderer.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Output\.gitkeep">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
</ItemGroup>
20+
</Project>

‎Fields/Add-docvariable-in-hyperlink-field/.NET/Add-docvariable-in-hyperlink-field/Output/.gitkeep‎

Whitespace-only changes.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
namespace Add_docvariable_in_hyperlink_field
4+
{
5+
internal class Program
6+
{
7+
8+
static void Main(string[] args)
9+
{
10+
// Creates a new instance of WordDocument (empty Word document).
11+
using (WordDocument wordDocument = new WordDocument())
12+
{
13+
// Adds a section to the document.
14+
IWSection section = wordDocument.AddSection();
15+
// Adds a paragraph to the section.
16+
IWParagraph paragraph = section.AddParagraph();
17+
// Adds a document variable named "Author" with a value of "https://www.syncfusion.com".
18+
wordDocument.Variables.Add("Author", "https://www.syncfusion.com");
19+
// Appends a hyperlink to the paragraph.
20+
WField hyperlinkField = paragraph.AppendHyperlink("https://www.google.com", "google", HyperlinkType.WebLink) as WField;
21+
// Embeds the document variable within the hyperlink field.
22+
AddDocVariableToHyperlink(paragraph, hyperlinkField);
23+
// Updates all document fields to reflect changes.
24+
wordDocument.UpdateDocumentFields();
25+
// Saves the output Word document to a specified file stream in DOCX format.
26+
using (FileStream stream = new FileStream(Path.GetFullPath(@"../../../Output/output.docx"), FileMode.OpenOrCreate))
27+
{
28+
wordDocument.Save(stream, FormatType.Docx);
29+
}
30+
}
31+
}
32+
33+
/// <summary>
34+
/// Modifies a hyperlink field to include a document variable.
35+
/// </summary>
36+
private static void AddDocVariableToHyperlink(IWParagraph paragraph, WField? field)
37+
{
38+
// Get the index of the field in the paragraph to insert the field code.
39+
int fieldIndex = paragraph.Items.IndexOf(field) + 1;
40+
// Add the field code for the hyperlink.
41+
field.FieldCode = "HYPERLINK \"";
42+
// Increment field index for further insertion.
43+
fieldIndex++;
44+
// Insert document variables into the field code at the specified index.
45+
InsertDocVariables("Author", ref fieldIndex, paragraph);
46+
// Close the hyperlink field with the ending quote.
47+
InsertText("\"", ref fieldIndex, paragraph as WParagraph);
48+
}
49+
50+
/// <summary>
51+
/// Inserts text such as operators or identifiers into the paragraph.
52+
/// </summary>
53+
private static void InsertText(string text, ref int fieldIndex, WParagraph paragraph)
54+
{
55+
// Create a new text range with the specified text.
56+
WTextRange textRange = new WTextRange(paragraph.Document);
57+
textRange.Text = text;
58+
// Insert the text range as a field code item at the specified index.
59+
paragraph.Items.Insert(fieldIndex, textRange);
60+
// Increment the field index for subsequent insertions.
61+
fieldIndex++;
62+
}
63+
/// <summary>
64+
/// Inserts document variables at the given index in the specified paragraph.
65+
/// </summary>
66+
private static void InsertDocVariables(string fieldName, ref int fieldIndex, IWParagraph paragraph)
67+
{
68+
// Create a new paragraph to hold the document variable field.
69+
WParagraph para = new WParagraph(paragraph.Document);
70+
para.AppendField(fieldName, FieldType.FieldDocVariable);
71+
// Get the count of child entities in the paragraph (should be 1 for the field).
72+
int count = para.ChildEntities.Count;
73+
// Insert the field into the original paragraph, ensuring the complete field structure is added.
74+
paragraph.ChildEntities.Insert(fieldIndex, para.ChildEntities[0]);
75+
// Increment the field index based on the count of inserted entities.
76+
fieldIndex += count;
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)