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.
- 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";
-
-
- 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);
-
- Word.Paragraph oPara1;
- oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);oPara1.Range.Text = "Heading 1";
- oPara1.Range.Font.Bold = 1;
- oPara1.Format.SpaceAfter = 24;
-
- oPara1.Range.InsertParagraphAfter();
-
- 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();
-
- Word.Range range = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
- range.Application.ActiveDocument.InlineShapes.AddPicture(@"D:\Image.jpg");
-
- 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();
-
- 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();
- }
- }
- }