The Tehie

The Tehie

  • NA
  • 56
  • 11.6k

How to add picture after text in word document using interop

Jan 7 2020 2:15 AM
On button click event picture is placed at the beginning of the document then text is written.
 
I want to write all the texts at the beginning of the document then picture/Image right below the texts. and also how to place picture beside the table? Are there any examples for placing/ Positioning word objects on a particular place in the word document?
 
please guide me to achieve it, thank you.
  1. using Word = Microsoft.Office.Interop.Word;namespace WindowsFormsApplication1    
  2. {    
  3. public partial class Form1 : Form   
  4. {   
  5. public Form1()    
  6. {    
  7. InitializeComponent();    
  8. }    
  9. private void button1_Click(object sender, EventArgs e)    
  10. {    
  11. object oMissing = System.Reflection.Missing.Value; object oEndOfDoc = "\\endofdoc";    
  12. /* \endofdoc is a predefined bookmark */    
  13. //Start Word and create a new document.    
  14. Word._Application oWord;    
  15. Word._Document oDoc = new Word.Document();    
  16. oWord = new Word.Application();    
  17. oWord.Visible = false;    
  18. oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);    
  19. //Insert a paragraph at the beginning of the document.    
  20. Word.Paragraph oPara1;    
  21. oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);oPara1.Range.Text = "Heading 1";    
  22. oPara1.Range.Font.Bold = 1;    
  23. oPara1.Format.SpaceAfter = 24;    
  24. //24 pt spacing after paragraph.    
  25. oPara1.Range.InsertParagraphAfter();    
  26. //Insert a paragraph at the end of the document.    
  27. Word.Paragraph oPara2;    
  28. object oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;    
  29. oPara2 = oDoc.Content.Paragraphs.Add(ref oRng);    
  30. oPara2.Range.Text = "Heading 2";    
  31. oPara2.Format.SpaceAfter = 6;oPara2.Range.InsertParagraphAfter();    
  32. //Insert Picture    
  33. Word.Range range = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;    
  34. range.Application.ActiveDocument.InlineShapes.AddPicture(@"D:\Image.jpg");    
  35. //Insert another paragraph.    
  36. Word.Paragraph oPara3;    
  37. oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;    
  38. oPara3 = oDoc.Content.Paragraphs.Add(ref oRng);    
  39. oPara3.Range.Text = "This is a sentence of normal text. Now here is a table:";    
  40. oPara3.Range.Font.Bold = 0;    
  41. oPara3.Format.SpaceAfter = 24;    
  42. oPara3.Range.InsertParagraphAfter();    
  43. //Insert Table    
  44. Word.Table oTable;    
  45. Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;    
  46. oTable = oDoc.Tables.Add(wrdRng, 3, 5, ref oMissing, ref oMissing);    
  47. oTable.Range.ParagraphFormat.SpaceAfter = 6;    
  48. int r, c;    
  49. string strText;    
  50. for (r = 1; r <= 3; r++)    
  51. for (c = 1; c <= 5; c++)    
  52. {    
  53. strText = "r" + r + "c" + c;oTable.Cell(r, c).Range.Text = strText;    
  54. }    
  55. oTable.Rows[1].Range.Font.Bold = 1;    
  56. oTable.Rows[1].Range.Font.Italic = 1;    
  57. string file_name = @"E:\Example.docx";    
  58. oDoc.SaveAs2(file_name);    
  59. oDoc.Close();    
  60. oWord.Quit();    
  61. }    
  62. }    
  63. }

Answers (3)