TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
The Tehie
NA
56
12.4k
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.
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();
}
}
}
Attachment:
WindowsFormsApplication1.rar
Reply
Answers (
3
)
how to bind multiple child textbox to one master textbox
How to insert the records into DB(MYSQL)