ASPOSE WORD C# FOOTNOTE
I AM WORKING ON WORD DOCUMENT TO FIND AND HYPERLINK TEXT USING REGEX EXPRESSION .I AM FACING ISSUE IN FOOTNOTE .I AM NOT ABLE TO FIND AND HYPERLINK FOOTNOTE TEXT USING DOC.Range.Replace(regExpression, ββ, findReplaceOptions);
PLEASE HELP
This Topic is created by awais.hafeez using Email to Topic tool.
@asheeshdwvd.ramy Could you please attach you input document and expected output here for testing? We will check the issue and provide you more information.
Also, please make sure FindReplaceOptions.IgnoreFootnotes flag is disabled in your findReplaceOptions
.
I am using below code and getting word document text based on my regex
Regex regExpression = new Regex(RegexText);
FindReplaceOptions findReplaceOptions = new FindReplaceOptions();
// findReplaceOptions.Direction = FindReplaceDirection.Backward;
MyReplaceEvaluator replacer = new MyReplaceEvaluator();
findReplaceOptions.ReplacingCallback = replacer;
DocumentText.Range.Replace(regExpression, "", findReplaceOptions);
ArrayList list = replacer.ListOfMatches;
foreach (string item in list)
filesList.Add(item);
Regex:[A-Z]{3}\.[\d]{3}\.[\d]{3}\.[\d]{4,6}|[A-Z]{3}\.[\d]{3,4}\.[\d]{3,4}\.[\d]{5,6}_[\d]{4}|[A-Z]{3}\.[\d]{3,4}\.[\d]{3,4}\.[\d]{5,6}|[A-Z]{3}\.[A-Z]{3}\.[\d]{3,4}\.[\d]{3,4}
Please find the attached word doc where i am able to create hyperlink on word doc body but i am not able to create on foot note
ABC.001.001.0001
ABC.001.001.0002
ABC.001.001.0003
I want to create link which points the document residing in directory based on regex file name search
as the respective document with same name is presentasdfcopy - Copy - Copy (2).docx (13.6 KB)
@asheeshdwvd.ramy Thank you for additional information. I cannot reproduce the problem with the latest 22.6 version of Aspose.Words and the following code:
Document doc = new Document(@"C:\Temp\in.docx");
Regex regex = new Regex(@"[A-Z]{3}\.[\d]{3}\.[\d]{3}\.[\d]{4,6}|[A-Z]{3}\.[\d]{3,4}\.[\d]{3,4}\.[\d]{5,6}_[\d]{4}|[A-Z]{3}\.[\d]{3,4}\.[\d]{3,4}\.[\d]{5,6}|[A-Z]{3}\.[A-Z]{3}\.[\d]{3,4}\.[\d]{3,4}");
FindReplaceOptions findReplaceOptions = new FindReplaceOptions();
MyReplaceEvaluator replacer = new MyReplaceEvaluator();
findReplaceOptions.ReplacingCallback = replacer;
doc.Range.Replace(regex, "", findReplaceOptions);
ArrayList list = replacer.ListOfMatches;
foreach (string item in list)
Console.WriteLine(item);
doc.Save(@"C:\Temp\out.docx");
private class MyReplaceEvaluator : IReplacingCallback
{
/// <summary>
/// This method is called by the Aspose.Words find and replace engine for each match.
/// This method replaces the match string with a hyperlink.
/// </summary>
ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
{
mListOfMatches.Add(e.Match.Value);
//return ReplaceAction.Replace;
// This is a Run node that contains either the beginning or the complete match.
Node currentNode = e.MatchNode;
// The first (and may be the only) run can contain text before the match,
// in this case it is necessary to split the run.
if (e.MatchOffset > 0)
currentNode = SplitRun((Run)currentNode, e.MatchOffset);
// This array is used to store all nodes of the match for further highlighting.
List<Run> runs = new List<Run>();
// Find all runs that contain parts of the match string.
int remainingLength = e.Match.Value.Length;
while (
remainingLength > 0 &&
currentNode != null &&
currentNode.GetText().Length <= remainingLength)
{
runs.Add((Run)currentNode);
remainingLength -= currentNode.GetText().Length;
// Select the next Run node.
// Have to loop because there could be other nodes such as BookmarkStart etc.
do
{
currentNode = currentNode.NextSibling;
} while (currentNode != null && currentNode.NodeType != NodeType.Run);
}
// Split the last run that contains the match if there is any text left.
if (currentNode != null && remainingLength > 0)
{
SplitRun((Run)currentNode, remainingLength);
runs.Add((Run)currentNode);
}
// Now insert a hyperlink and remove the matched runs.
DocumentBuilder builder = new DocumentBuilder((Document)e.MatchNode.Document);
builder.MoveTo(runs[runs.Count - 1]);
builder.Font.StyleIdentifier = StyleIdentifier.Hyperlink;
builder.InsertHyperlink(e.Match.Value, string.Format(@"documents\{0}.pdf", e.Match.Value), false);
// Now highlight all runs in the sequence.
foreach (Run run in runs)
run.Remove();
// Signal to the replace engine to do nothing because we have already done all what we wanted.
return ReplaceAction.Skip;
}
private static Run SplitRun(Run run, int position)
{
Run afterRun = (Run)run.Clone(true);
run.ParentNode.InsertAfter(afterRun, run);
afterRun.Text = run.Text.Substring(position);
run.Text = run.Text.Substring((0), (0) + (position));
return afterRun;
}
public ArrayList ListOfMatches { get { return mListOfMatches; } }
private readonly ArrayList mListOfMatches = new ArrayList();
}
Here are input and output documents: in.docx (16.9 KB) out.docx (14.9 KB)
Thanks alexey,
I was using 17.4 version aspose license .
I will purchase latest aspose license, its in process.Once I have latest license i will check with that.
Thanks have a nice day.
Can you please help me in removing hyperlink based on regex .As you have created hyperlink similarly removing it.
Thanks
@asheeshdwvd.ramy Sure, you can achieve this using the following simple code:
Document doc = new Document(@"C:\Temp\in.docx");
Regex regex = new Regex(@"[A-Z]{3}\.[\d]{3}\.[\d]{3}\.[\d]{4,6}|[A-Z]{3}\.[\d]{3,4}\.[\d]{3,4}\.[\d]{5,6}_[\d]{4}|[A-Z]{3}\.[\d]{3,4}\.[\d]{3,4}\.[\d]{5,6}|[A-Z]{3}\.[A-Z]{3}\.[\d]{3,4}\.[\d]{3,4}");
// Unlink the hyperlinks with displayed text matches the regular expression.
foreach (Field f in doc.Range.Fields)
{
if ((f.Type == FieldType.FieldHyperlink) && regex.IsMatch(f.DisplayResult))
f.Unlink();
}
doc.Save(@"C:\Temp\out.docx");
I also want to remove underline and make the unlink text black
@asheeshdwvd.ramy The provided code does exactly this. Here are input and output documents: in.docx (18.8 KB) out.docx (14.1 KB)
ok thanks I am using 17.4 license may be thatβs why I am facing this issue, will test it once I get the latest version .
Many thanks to you for quick support
1 Like
@asheeshdwvd.ramy While you are waiting, you can request a free temporary 30-days license to be able to test the latest version with your scenario.
DocumentText.Range.Fields.Cast<Field>().Where(f => f.Type == FieldType.FieldHyperlink && regex.IsMatch(f.Result)).ToList().ForEach(f => f.Unlink());
is more useful it unlinks all irrespective of repitition.
Once i removed the hyperlink i wanted to change the color of the text whose hyperlink I have removed .Please help how can we achieve it
@asheeshdwvd.ramy
Yes, this code does the same as the code I have provided.
You can easily achieve this using find/replace feature. Foe example see the following code:
Document doc = new Document(@"C:\Temp\in.docx");
Regex regex = new Regex(@"[A-Z]{3}\.[\d]{3}\.[\d]{3}\.[\d]{4,6}|[A-Z]{3}\.[\d]{3,4}\.[\d]{3,4}\.[\d]{5,6}_[\d]{4}|[A-Z]{3}\.[\d]{3,4}\.[\d]{3,4}\.[\d]{5,6}|[A-Z]{3}\.[A-Z]{3}\.[\d]{3,4}\.[\d]{3,4}");
// Unlink the matched hyperlinks
doc.Range.Fields.Cast<Field>().Where(f => f.Type == FieldType.FieldHyperlink && regex.IsMatch(f.Result)).ToList().ForEach(f => f.Unlink());
// Chenge color of the matched text in the document.
FindReplaceOptions opt = new FindReplaceOptions();
opt.ApplyFont.Color = Color.Red;
opt.UseSubstitutions = true;
doc.Range.Replace(regex, "$0", opt);
doc.Save(@"C:\Temp\out.docx");
Hi Alexey,
I am using aspose license and created a application in .net framework .
I published the application into single executable file.
Problem I am facing is I need to point the aspose license file path .
I want to integrate license within the code by providing key and license ID
Is there any option from where we can use aspose key and signature within our code and then there will be no need to provide license file path.
I saw XML stream option but i was not able to integrate it within my code.
My requirement is I want to use aspose library and while running the application I donβt want provide path of license file.
Can you please help in that.
@asheeshdwvd.ramy I think in your case you can simply include the license as an embedded resource into your application.
I tried using embedded resource .After publish I am getting attached error
"codebase is not supported on assemblies loaded from a single-file bundle "
image.png (2.9 KB)
@asheeshdwvd.ramy It looks like the same problem described here:
https://stackoverflow.com/questions/64818343/publishing-error-for-single-file-net-5-project-due-to-3rd-party-dependencies
If this does not help, try to get the embedded resource from your assembly in the code into a stream and pass the stream into License.SetLicense
method.
Hi alexey
I tried both option is not working Its asking license file path
I am using netcoreapp3.1
I am attaching my publish folder structure as I am creating single exe file.
After Publish It requires license file .Error attachedLicensePathError.png (6.3 KB)
PublishFolder.png (35.7 KB)
@asheeshdwvd.ramy Could you please share the code you use for applying the license?
Also, it would be great if you create a simple project that will allow us to reproduce the problem on our side.