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";
- /* \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();
- }
- }
- }
Marvin ReidPosted Jun 27, 2023, 2:54 PM
An alternative solution that you can try which bypasses the need for having Microsoft Office installed is adding the picture using the Leadtools.Document.Editor namespace. It exposes the ImageEngine object which allows you to insert an image directly using the insertImage method. Here is some sample code which demonstrates this:
https://www.leadtools.com/help/sdk/v22/dh/javascript/de/imageengine.html
https://www.leadtools.com/help/sdk/tutorials/html5-js-import-edit-save-documents-with-the-document-editor.html
The TehiePosted Jan 8, 2020, 12:03 AM
Leon DPosted Jan 7, 2020, 3:27 AM