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.

Thu Jun 26, 2014 11:49 am

Hi,

I am trying to insert an image, which i have got to work. However i cannot get the image to display in the correct paragraph. For example i have 4 paragraphs and i need the image to be displayed in paragraph 3. This is the snippet of code. also how do i now i have the correct section?

Dim sec As Section = ActiveDocument.Sections(0)
Dim Imageparagraph As Paragraph '= sec.AddParagraph()


Imageparagraph = ActiveDocument.Sections(0).Paragraphs(?????????)
Dim picture As DocPicture = Imageparagraph.AppendPicture(image)

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

Fri Jun 27, 2014 3:06 am

Hello,

Thanks for yoru inquiry.
Please refer to the below code snippet.
Code: Select all
//Get the first section of document.
Dim section As Section = document.Sections(0)
//Get the fourth paragraph of the section
Dim paragrph As Paragraph = section.Paragraphs(3)
paragrph.AppendPicture(image)


Welcome to write to us again if you have further problems.

Best wishes,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Fri Jun 27, 2014 8:06 am

Hi,

I cannot have it hard coded as the program will not now what paragraph the image will be on as the user will design a specific document for what they need, so I need to know when producing the word doc where the user has put the image, I couldn't find any property to do this.

is it possible to get the paragraph number from the properties?

And how would i get the section number incase it is section 2, 3 or 7 for example, again this is something the program will not know till the word document is being produced.

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

Fri Jun 27, 2014 8:52 am

Hello,

Sorry that I misunderstand your requirement.
Please refer to the following code snippet to get the paragraph number and the section number of the picture in an existed document.

Code: Select all
     static void Main(string[] args)
        {
            Document document = new Document();
            document.LoadFromFile("..\\..\\Test.docx");
            //Get all pictures of document.
            List<DocPicture> pictures = GetAllPictures(document);
            //Get the paragraph of the first picture
            Paragraph paragraph = pictures[0].OwnerParagraph;
            //Get the second of the first picture
            Section section = paragraph.Owner.Owner as Section;
            //Get the paragraph number of the first picture
            int indexP = section.Body.ChildObjects.IndexOf(paragraph);
           //Get the section number of the first picture
            int indexS = document.ChildObjects.IndexOf(section);
        }
        static List<DocPicture> GetAllPictures(Document document)
        {
            List<DocPicture> pictures=new List<DocPicture>();
            foreach (Section section in document.Sections)
            {
                foreach (Paragraph paragraph in section.Paragraphs)
                {
                    foreach (DocumentObject obj in paragraph.ChildObjects)
                    {
                        if (obj.DocumentObjectType == DocumentObjectType.Picture)
                        {
                            DocPicture image = obj as DocPicture;
                           
                            pictures.Add(image);
                        }
                    }
                }
            }
            return pictures;
        }


Please feel free to contact us if you have any problems.

Best wishes,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Fri Jun 27, 2014 11:44 am

Hi,

I have tried this code and got it to work with an actual image inserted into the document. The issue is the way that we merge documents we use text for example
<[ImageID]|Image|> and then from this can tell its an image and run code to find the image by its image id. Is there a way of doing this?
And I can see that DocumentObjectType for this is text range so cannot add this to the DocPicture type.

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

Mon Jun 30, 2014 7:16 am

Hello,

To help us to know your need well, would you please share a sample document here?

Thanks.

Best wishes,
Amy
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Mon Jun 30, 2014 9:07 am

Hi,

I cannot attach a document as the extension is not allowed. I have tried attaching a word.doc, word.docx and word.docm and neither extension allow me to attach.

The basis of a document could look like this

Spire Test Doc Title
Forname: [txtForename]
Surname: [txtSurname]
Image: <[Picture]|Image|>
Town: [txtTown]
Country: [txtCountry]

So what I am trying to do is when creating the document, when the keyword "|Image" is found is to insert the picture at that position on that paragraph. And I could have text above and below the image or even have the image in a table.

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

Mon Jun 30, 2014 9:52 am

Hi,

Please attach a .zip file.

Thanks.

Best wishes,
Amy
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Mon Jun 30, 2014 9:57 am

Hi,

I have attached the file.

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

Tue Jul 01, 2014 3:23 am

Hi,

Please try the following code to find the position of the text"|Image|" and then insert a picture at the position.
Code: Select all
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports System.Drawing

         Dim document As New Document()
        document.LoadFromFile("..\..\Spire Doc Adding Images.docm")
        Dim selections As TextSelection() = document.FindAllString("|Image|", True, True)

        Dim image As Image = image.FromFile("..\..\image1.jpg")
        Dim picture As New DocPicture(document)
        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
        Dim output As String = "sample.docm"
        document.SaveToFile(output, FileFormat.Docm)
        System.Diagnostics.Process.Start(output)


Please feel free to contact us if you have any problems.

Best wishes,
Amy
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Fri Jul 04, 2014 9:11 am

Hi,

Have you tried the code? Did you resolve your issue?
Thanks for your feedback.

Best wishes,
Amy
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Fri Jul 04, 2014 9:18 am

HI,

Its almost there, the image inserts into correct position, but the text still remains.
Is there a way of clearing the text?

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

Fri Jul 04, 2014 9:36 am

Hi,

Please check the previous code, there are the below lines in it.
Code: Select all
'   If you also want to remove "|Image|" text, you can try this line.
'paragraph.ChildObjects.RemoveAt(index + 1)


Best wishes,
Amy
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.Doc