Insert image in word document

  • NPOI
  • Spire.Doc
  • Download Sample Code

using NPOI.XWPF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NPOI
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create document
            XWPFDocument doc = new XWPFDocument();
            XWPFParagraph para = doc.CreateParagraph();
            XWPFRun run = para.CreateRun();

            //Insert image and set its size
            using (FileStream picFile = new FileStream("../../../Data/image.jpg", FileMode.Open,FileAccess.Read))
            {
                run.AddPicture(picFile, (int)PictureType.PNG, "image", 300 * 12857, 168 * 12857);
            }

            //Save the file
            using (FileStream file = File.Create("Image.docx"))
            {
                doc.Write(file);
            }

            //Launch
            System.Diagnostics.Process.Start("Image.docx");
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace Spire.Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create Document
            Document document = new Document();
            Section s = document.AddSection();
            Paragraph p = s.AddParagraph();

            //Insert image and set its size
            DocPicture Pic = p.AppendPicture(Image.FromFile("../../../Data/image.jpg"));
            Pic.Width = 400;
            Pic.Height = 200;

            //Save and Launch
            document.SaveToFile("Image.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Image.docx");

        }
    }
}