Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Wed Jan 05, 2011 3:40 pm

I like replace a Regex (text) by a image, is possible?

private void FindAndReplace(string s, string physicalPath)
{
string img = string.Empty;

Regex reg = new Regex(s);
TextSelection regExText = document.FindPattern(reg);

if (regExText != null)
{
if (regExText.Count != 0)
{
IeCapt ie = new IeCapt();
img = ChooseImage(s, physicalPath);
}


}
}

afetter
 
Posts: 2
Joined: Wed Jan 05, 2011 9:27 am

Thu Jan 06, 2011 3:15 pm


24houres wainting surport!
VERY GOOD!




private void FindAndReplace(string s, string physicalPath)
{
string img = string.Empty;

Regex reg = new Regex(s);
TextSelection[] regExText = document.FindAllPattern(reg);

if (regExText != null)
{
List<Paragraph> listParagraph = new List<Paragraph>();

for (int i = 0; i < regExText.Length; i++)
{

if (regExText[i].Count != 0)
{
img = ChooseImage(s, physicalPath);
}

Paragraph paragraph = GetParagraph(regExText[i].SelectedText);
paragraph.Text = " ";
paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Left;
paragraph.AppendPicture(Image.FromFile(img));
listParagraph.Add(paragraph);
//document.Replace(reg,
// new TextSelection(paragraph, 0, paragraph.Text.Length));

//document.Replace(regExText[i].SelectedText,string.Empty,true,true);
}

foreach (Paragraph item in listParagraph)
{
document.Replace(reg, new TextSelection(item, 0, item.Text.Length));
}
}
}

private Paragraph GetParagraph(string p)
{
foreach (Paragraph item in document.Sections[0].Paragraphs)
{
if (item.Text.Trim().Equals(p))
return item;
}
return document.Sections[0].AddParagraph();
}

afetter
 
Posts: 2
Joined: Wed Jan 05, 2011 9:27 am

Mon Jan 10, 2011 2:38 am

Dear afetter,

We are sorry for the delay of the answer of your question about replacing text by image.
Please try:

TextSelection[] selections = document.FindAllPattern(regex);
foreach (TextSelection selection in selections)
{
TextRange textRange = selection.GetAsOneRange();
Paragraph paragraph = textRange.OwnerParagraph;
int index = paragraph.ChildObjects.IndexOf(textRange);
if (index != -1)
{
paragraph.ChildObjects.RemoveAt(index);
String imagePath = ChooseImage(regexPattern, physicalPath);
DocPicture picture = paragraph.AppendPicture(Image.FromFile(imagePath));
paragraph.ChildObjects.Remove(picture);
paragraph.ChildObjects.Insert(index, picture);
}
}


A full demo is attached, please check.
If you have any other question, please let us know.

Thanks and Regards
Harry
Technical Support / Developer,
e-iceblue Support Team
User avatar

harry.support
 
Posts: 180
Joined: Mon Nov 08, 2010 3:11 pm

Return to Spire.Doc