C# word Interop 制作可点击的超链接,包括搜索和替换后的替代文本

问题描述 投票:0回答:1

免责声明:我试图在这里寻找答案,但我似乎找不到符合我情况的答案。

目前我有一个功能可以在原始位置搜索和替换文档中的文本。这是基于我创建的称为翻译词典的对象类,其中包含以下属性:

  1. 字符串搜索文本
  2. 字符串替换文本
  3. 字符串替代文本

第一个属性是我正在搜索的文本,第二个属性是将搜索文本替换为的文本。第三个属性是替代文本。

确实发生的情况之一是我正在搜索的文本被链接替换。 这效果很好,但遗憾的是链接本身在最终文档中不可点击。我确实有一个函数可以检查 SearchText 是否是有效的 URL。但据我所知。另外,我真的不知道如何将该 URL 更改为替代文本而不丢失链接本身。理论上,这应该只是另一次搜索和替换。

但考虑到该链接不可点击。它只是将该 URL 替换为我定义的替代文本,使我失去了链接。

public void ConvertDocumentWords(string inputlocation, List<TranslationInformationCarrier> inputList)
{
    try
    {
        string FileName = Path.GetFileName(inputlocation);
        Application WordApp = new Application();
        Word.Document doc = WordApp.Documents.Open($@"{inputlocation}");
        doc.Activate();
        foreach (var item in inputList)
        {
            string ReplacementText = item.ReplacementText;
            string SearchText = item.SearchText;
            string AlternativeText = item.AlternativeText;
            FindAndReplace(WordApp, SearchText, ReplacementText);
            DataLogger.LogInformation($"Replaced text {item.SearchText} in file {FileName}");
            if (IsValidUrl(ReplacementText))
            {
                //Stuck here.
            }
        }
        doc.Close(true);
        WordApp.Quit(true);
        GarbageSystemCleanUp();
    }

    catch (Exception ex)
    {
        DataLogger.LogError($"An exception occurred in the find and replace process: {ex.StackTrace}");
        throw;
    }
}
public static bool IsValidUrl(string url)
{
    return Uri.IsWellFormedUriString(url, UriKind.Absolute);
}
/// <summary>
/// Function to find and replace a text.
/// </summary>
/// <param name="fileOpen">Word Application that is open.</param>
/// <param name="findText">Text to find the document.</param>
/// <param name="replaceWithText">Text to replace the find text to.</param>
static void FindAndReplace(Microsoft.Office.Interop.Word.Application fileOpen, object findText, object replaceWithText)
{
    object matchCase = false;
    object matchWholeWord = true;
    object matchWildCards = false;
    object matchSoundsLike = false;
    object matchAllWordForms = false;
    object forward = true;
    object format = false;
    object matchKashida = false;
    object matchDiacritics = false;
    object matchAlefHamza = false;
    object matchControl = false;
    object read_only = false;
    object visible = true;
    object replace = 2;
    object wrap = 1;
    //execute find and replace
    fileOpen.Selection.Find.Execute(ref findText, ref matchCase, ref matchWholeWord,
        ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, ref replaceWithText, ref replace,
        ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl);
}
c# office-interop
1个回答
0
投票

经过一段时间的搜索和实验,我找到了自己问题的答案。重要的是像这样添加超链接,首先将范围设置为目标对象并使用。可以使用我的搜索文本找到该范围。然后我可以使用我的替换文本(包括我的替代文本和链接)设置超链接。

if (IsValidUrl(ReplacementText))
{
Hyperlinks myLinks = doc.Hyperlinks;
Range range = doc.Content;
range.Find.Execute(SearchText);
Hyperlink myLink = myLinks.Add(range, ReplacementText, null, ReplacementText, AlternativeText);
}
© www.soinside.com 2019 - 2024. All rights reserved.