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.

Tue Sep 16, 2014 4:23 pm

Hi,

I am trying to insert a picture that i have saved in my c drive ie c:\pictureCake into my word document but i cannot get it to work.

When i insert this picture i need it to remove the text as this is where the picture is meant to go.

I have attached a zip file that contains the document of how it should look and the document of what it currently looks like.

So what i am trying to achieve is when i drop into a routine that is calling the file path of the picture is to then insert that picture over the text which in this instance is [Student.picBirthdayPicture/1].

Also the piece of text could be anywhere in the document.

How is this done?

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

Wed Sep 17, 2014 2:27 am

Hello,

We have tested the situation you mentioned, but there is no any issues, Following codes are for your reference.
Code: Select all
Dim doc As New Document()
Dim sec As Section = doc.AddSection()
'add  paragraph
Dim para As Paragraph = sec.AddParagraph()
para.Format.AfterSpacing = 10F
Dim tr As New TextRange(doc)
tr = para.AppendText("BDAY picture")
tr.CharacterFormat.Font = New Font(New FontFamily("Calibri"), 11F)
tr.CharacterFormat.UnderlineStyle = UnderlineStyle.Single
'add table
Dim table As Table = sec.AddTable(True)
table.ResetCells(2, 2)
tr = table.Rows(0).Cells(0).AddParagraph().AppendText("Student age")
tr.CharacterFormat.Font = New Font(New FontFamily("Calibri"), 11F)
tr = table.Rows(0).Cells(1).AddParagraph().AppendText("35")
tr.CharacterFormat.Font = New Font(New FontFamily("Calibri"), 11F)
tr.CharacterFormat.UnderlineStyle = UnderlineStyle.[Single]
tr = table.Rows(1).Cells(0).AddParagraph().AppendText("Birthday picture")
tr.CharacterFormat.Font = New Font(New FontFamily("Calibri"), 11F)
tr = table.Rows(1).Cells(1).AddParagraph().AppendText("[Student.picBirthdayPicture/1] ")
tr.CharacterFormat.Font = New Font(New FontFamily("Calibri"), 11F)
Dim selects As TextSelection() = doc.FindAllString("[Student.picBirthdayPicture/1]", True, True)
Dim image As Image = Image.FromFile("C:\pictureCake\1.gif")
Dim picture As New DocPicture(doc)
picture.LoadImage(image)
For Each select As var 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(ResultPath + "result.docx", FileFormat.Docx)

Sincerely,
Gary
E-iceblue support team
User avatar

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

Wed Sep 17, 2014 8:45 am

Thats great with the code you referenced i have been able to get this to work.

many thanks

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

Return to Spire.Doc