using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

// Open the Add References dialog, open the Assemblies branch,
// select the the Extensions tab, and check:
//      Microsoft.Office.Interop.Word
//      office
// Be sure to use matching versions of the libraries.
using Word = Microsoft.Office.Interop.Word;
using Core = Microsoft.Office.Core;

namespace AddWordPicture
{
    public partial class frm20AddWordPicture : Form
    {
        public frm20AddWordPicture()
        {
            InitializeComponent();
        }

        // Set initial file names.
        private void frm20AddWordPicture_Load(object sender, EventArgs e)
        {
            fileTextBox.Text = Application.StartupPath + "\\test.docx";
            pictureTextBox.Text = Application.StartupPath + "\\test.png";
        }

        // Add the picture to the Word document.
        private void insertButton_Click(object sender, EventArgs e)
        {
            // Get the Word application object.
            Word.Application word = new Word.Application();

            // Uncomment to make Word visible.
            //word.Visible = true;

            // Open the existing Word document.
            object filename = Application.StartupPath + "\\test.docx";
            Word.Document doc = word.Documents.Open(ref filename);

            // Find the beginning of the document bookmark.
            object start = "\\startofdoc";

            // Get a Range at the start of the document.
            Word.Range startRange = doc.Bookmarks.get_Item(ref start).Range;

            // Add the picture to the Range's InlineShapes.
            Word.InlineShape inlineShape =
                startRange.InlineShapes.AddPicture(pictureTextBox.Text);

            // Format the picture.
            Word.Shape shape = inlineShape.ConvertToShape();

            // Scale uniformly by 50%.
            shape.LockAspectRatio = Core.MsoTriState.msoTrue;
            shape.ScaleHeight(0.5f, Core.MsoTriState.msoTrue,
                Core.MsoScaleFrom.msoScaleFromTopLeft);

            // Wrap text around the picture's square.
            shape.WrapFormat.Type = Word.WdWrapType.wdWrapSquare;

            // Align the picture on the upper right.
            shape.Left = (float)Word.WdShapePosition.wdShapeOutside;
            shape.Top = (float)Word.WdShapePosition.wdShapeTop;
            
            // Save the document.
            doc.Save();

            // Close.
            doc.Close();
            word.Quit();

            MessageBox.Show("Done");
        }


        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.fileTextBox = new System.Windows.Forms.TextBox();
            this.pictureTextBox = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.insertButton = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(12, 15);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(26, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "File:";
            // 
            // fileTextBox
            // 
            this.fileTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.fileTextBox.Location = new System.Drawing.Point(61, 12);
            this.fileTextBox.Name = "fileTextBox";
            this.fileTextBox.Size = new System.Drawing.Size(311, 20);
            this.fileTextBox.TabIndex = 1;
            // 
            // pictureTextBox
            // 
            this.pictureTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.pictureTextBox.Location = new System.Drawing.Point(61, 38);
            this.pictureTextBox.Name = "pictureTextBox";
            this.pictureTextBox.Size = new System.Drawing.Size(311, 20);
            this.pictureTextBox.TabIndex = 3;
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(12, 41);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(43, 13);
            this.label2.TabIndex = 2;
            this.label2.Text = "Picture:";
            // 
            // insertButton
            // 
            this.insertButton.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.insertButton.Location = new System.Drawing.Point(155, 76);
            this.insertButton.Name = "insertButton";
            this.insertButton.Size = new System.Drawing.Size(75, 23);
            this.insertButton.TabIndex = 4;
            this.insertButton.Text = "Insert";
            this.insertButton.UseVisualStyleBackColor = true;
            this.insertButton.Click += new System.EventHandler(this.insertButton_Click);
            // 
            // frm20AddWordPicture
            // 
            this.AcceptButton = this.insertButton;
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(384, 111);
            this.Controls.Add(this.insertButton);
            this.Controls.Add(this.pictureTextBox);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.fileTextBox);
            this.Controls.Add(this.label1);
            this.Name = "frm20AddWordPicture";
            this.Text = "AddWordPicture";
            this.Load += new System.EventHandler(this.frm20AddWordPicture_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox fileTextBox;
        private System.Windows.Forms.TextBox pictureTextBox;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Button insertButton;
    }
}