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.

Fri Sep 12, 2014 10:53 am

Hi,

I have created a new topic though I did start a topic several months ago with the title 'How to get index of a paragraph' which did give me the answers. But now im getting an issue where i cannot get rid of the text where the picture is inserting to.

I have created a word doc please see attached file, but i cannot get rid of the text highlighted in blue after the picture, how do i get rid of this text?

This is my code so far:
Dim selections As TextSelection() = ActiveDocument.FindAllString(textSelections(0).SelectedText, True, True)
Dim imageData() As Byte = Nothing
Dim image As Image = Nothing
imageData = GetImageData(ImageId)
image = clsPictureManipulator.CreateImageFromByteArray(imageData)

Dim picture As New DocPicture(ActiveDocument)
picture.LoadImage(image)

For Each selection As TextSelection In selections
Dim range As TextRange = selection.GetAsOneRange()
Dim paragraph As Paragraph = range.OwnerParagraph
Dim index As Integer = paragraph.ChildObjects.IndexOf(range)
paragraph.ChildObjects.Insert(index, picture.Clone())
'If you also want to remove "|Image|" text, you can try this line.
'paragraph.ChildObjects.RemoveAt(index + 1)
Next

Where im doing the paragraph.ChildObjects.RemoveAt(index + 1) it is not removing the whole text selection it is leaving <156|Imae|> any ideas how to fix this?

stevenheggie
 
Posts: 60
Joined: Tue Jun 24, 2014 10:32 am

Mon Sep 15, 2014 2:51 am

Hello,

Thanks for your feedback.
We have modified the document you provided and tested it, we can't replicate the issue you mentioned, following codes are ours same as yours, and the template is attached.
Please provide us more detailed information or a simple project can reproduce the issue.
Code: Select all
Dim doc As New Document()
doc.LoadFromFile("spire-doc-test.docx")
Dim selects As TextSelection() = doc.FindAllString("<156|Image|>", True, True)
Dim image As Image = Image.FromFile("3497.jpg")
Dim picture As New DocPicture(doc)
picture.LoadImage(image)
For Each select As TextSelection In selects
   Dim range As TextRange = select.GetAsOneRange()
   Dim paragraph As Paragraph = range.OwnerParagraph
   Dim index As Integer = paragraph.ChildObjects.IndexOf(range)
   paragraph.ChildObjects.Insert(index, picture)
   paragraph.ChildObjects.RemoveAt(index + 1)
Next
doc.SaveToFile("result.docx", FileFormat.Docx)

Thanks,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Mon Sep 15, 2014 10:21 am

hi,

I have been going through the code again, and i can now get it to work, the reason was due to another piece of code causing the text to stay there!
many thanks for you help!

stevenheggie
 
Posts: 60
Joined: Tue Jun 24, 2014 10:32 am

Tue Sep 16, 2014 1:12 am

Hello,

Thanks for your response. Glad to hear that .
If there are any questions, welcome to get it back to us.
Sincerely,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Return to Spire.Doc