Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find the Range of particular Text and Mark that text as hyperlink #78

Open
arunln opened this issue Nov 10, 2016 · 2 comments
Open

Find the Range of particular Text and Mark that text as hyperlink #78

arunln opened this issue Nov 10, 2016 · 2 comments

Comments

@arunln
Copy link

arunln commented Nov 10, 2016

Hi Team,

Below mentioned text will be present in our TEST.docx file.

sample example paragraph:
To make your document look professionally produced, Word provides header, footer, cover page, and text box designs that complement each other.
For example, you can add a matching cover page, header, and sidebar. Click Insert and then choose the elements you want from the different galleries.

We want Implement following requirements using DocX API

  1. Load "TEST.docx" File using DocX API.
  2. Identify the following text "Click Insert" in document and find its start and end index
  3. Mark "Click Insert" as hyperlink element and save the document.
    Please let us know is there any way to achieve this using DocX API.

Regards,
Arun

@zimmermanw84
Copy link

@arunln This is a 2 year old comment so I assume this problem is far in your rear view, so I am commenting for anyone else who may find this useful.

I found a workaround to inserting Hyperlinks into a document where an arbitrary text string is located. Obviously there's performance implications to this, but it works in the interim until the maintainers add this functionality.

// create a hyperlink
Hyperlink linkedTitle = document.AddHyperlink(content, new Uri(href));
// FindAll returns a List of starting indexes where your string was found
List<int> textMatchIndexes = document.FindAll(stringToMatch, RegexOptions.IgnoreCase);
// hack to insert (insert a paragraph because you can insert at an index)
foreach (int index in textMatchIndexes)
{
  // insert p at index
   var p = document.InsertParagraph(index, "", false);
   // add hyper link to it
   p.AppendHyperlink(linkedTitle).Color(Color.Blue).UnderlineStyle(UnderlineStyle.singleLine);
} 
// replace placeholder text with empty string
document.ReplaceText(stringToMatch, "", false, RegexOptions.IgnoreCase);

Obviously not the most straight forward approach.

@XceedBoucherS
Copy link
Collaborator

Hi,
in the next release, v1.6, a new method will be available to replace a string by an hyperlink :
ReplaceTextWithObject( string searchValue, DocumentElement objectToAdd )
Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants