none
How to place picture after some text in word document using interop (C#)? RRS feed

  • Question

  • On button click event picture is placed at the beginning of the document then text is written. I want to write all the text at the beginning of the document then picture/image right below the text. Iam using word Interop.dll. Please guide me to achieve it, thank you.

    using Word = Microsoft.Office.Interop.Word;
    
    namespace WindowsFormsApplication1
    {
     public partial class Form1 : Form
      {
       public Form1()
       {  
            InitializeComponent();
       }
    
       private void button1_Click(object sender, EventArgs e)
       {
         object oMissing = System.Reflection.Missing.Value;
         object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */
         //Start Word and create a new document.
         Word._Application oWord;
         Word._Document oDoc = new Word.Document();
         oWord = new Word.Application();
         oWord.Visible = false;
         oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
         ref oMissing, ref oMissing);
         //Insert a paragraph at the beginning of the document.
         Word.Paragraph oPara1;
         oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
         oPara1.Range.Text = "Heading 1";
         oPara1.Range.Font.Bold = 1;
         oPara1.Format.SpaceAfter = 24; //24 pt spacing after paragraph.
         oPara1.Range.InsertParagraphAfter();
         //Insert a paragraph at the end of the document.
         Word.Paragraph oPara2;
         object oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
         oPara2 = oDoc.Content.Paragraphs.Add(ref oRng);
         oPara2.Range.Text = "Heading 2";
         oPara2.Format.SpaceAfter = 6;
         oPara2.Range.InsertParagraphAfter();
    
         //Insert Picture
         Word.Range range = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
         range.Application.ActiveDocument.InlineShapes.AddPicture(@"D:\Image.jpg");
    
         //Insert another paragraph.
         Word.Paragraph oPara3;
         oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
         oPara3 = oDoc.Content.Paragraphs.Add(ref oRng);
         oPara3.Range.Text = "This is a sentence of normal text. Now here is a table:";
         oPara3.Range.Font.Bold = 0;
         oPara3.Format.SpaceAfter = 24;
         oPara3.Range.InsertParagraphAfter();
    
         //Insert Table
         Word.Table oTable;
         Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
         oTable = oDoc.Tables.Add(wrdRng, 3, 5, ref oMissing, ref oMissing);
         oTable.Range.ParagraphFormat.SpaceAfter = 6;
         int r, c;
         string strText;
         for (r = 1; r <= 3; r++)
             for (c = 1; c <= 5; c++)
             {
                 strText = "r" + r + "c" + c;
                 oTable.Cell(r, c).Range.Text = strText;
             }
         oTable.Rows[1].Range.Font.Bold = 1;
         oTable.Rows[1].Range.Font.Italic = 1;
    
         string file_name = @"E:\Example.docx";
         oDoc.SaveAs2(file_name);
         oDoc.Close();
         oWord.Quit();
       }
      }
    }


    Sunday, January 12, 2020 5:20 PM

Answers

  • Hi,

    Thank you for posting here.

    You can replace the code that inserts the picture with these sentences:

                object o_CollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
                Word.Range imgrng = oDoc.Content;
                imgrng.Collapse(ref o_CollapseEnd);
                imgrng.InlineShapes.AddPicture(@"D:\test\picture\3.jpg", oMissing, oMissing, imgrng);

    Then move it blow.

    Result:

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, January 13, 2020 8:29 AM

All replies

  • Hi,

    Thank you for posting here.

    You can replace the code that inserts the picture with these sentences:

                object o_CollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
                Word.Range imgrng = oDoc.Content;
                imgrng.Collapse(ref o_CollapseEnd);
                imgrng.InlineShapes.AddPicture(@"D:\test\picture\3.jpg", oMissing, oMissing, imgrng);

    Then move it blow.

    Result:

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, January 13, 2020 8:29 AM
  • Hi,

    Has your issue been resolved?

    If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, January 17, 2020 8:18 AM