【C#】NPOI 操作 word 文档插入图片

使用NPOI操作,word文档必须为docx格式。
NPOI对文档插入图片的操作实际分为:向段落中插入图片、向表格中插入图片。

添加NPOI包

image

实际操作

文档模板

insertpic为插入图片需要的自定义定位标签
image

//添加引用
using NPOI.XWPF.UserModel;

namespace ConsoleApp_Archive
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string Templatepath = "E:\\Sample.docx";
            string Picpath1 = "E:\\SamplePic1.png";
            string Picpath2 = "E:\\SamplePic2.png";
            string Picpath3 = "E:\\SamplePic3.png";

            using (FileStream stream = File.OpenRead(Templatepath))
            {
                XWPFDocument doc = new XWPFDocument(stream);

                //向第一个表格中插入图片
                XWPFTable table1 = doc.Tables[0];
                FileStream imgstream1 = new FileStream(Picpath1, FileMode.Open, FileAccess.Read);
                //拆分表格,划分为每行每格每段落循环查找
                for (int i = 0; i < table1.Rows.Count; i++)
                {
                    XWPFTableRow tableRow = table1.Rows[i];
                    for (int j = 0; j < tableRow.GetTableCells().Count; j++)
                    {
                        XWPFTableCell tableRowCell = tableRow.GetTableCells()[j];
                        for (int k = 0; k < tableRowCell.Paragraphs.Count; k++)
                        {
                            XWPFParagraph par = tableRowCell.Paragraphs[k];
                            var runs = par.Runs;
                            if (par.ParagraphText.Contains("insertpic"))
                            {
                                for (int l = 0; l < runs.Count; l++)
                                {
                                    if (Convert.ToString(runs[l]) == "insertpic")
                                    {
                                        //插入图片,图片格式参数与图片实际一致,添加文字任意无影响,宽高自定义,需要 * 10000
                                        par.InsertNewRun(l).AddPicture(imgstream1, (int)PictureType.PNG, "img", 100 * 10000, 50 * 10000);
                                        //替换标签文字为空
                                        par.ReplaceText("insertpic", null);
                                    }
                                }
                            }
                        }
                    }
                }

                //向第二个表格中插入图片
                XWPFTable table2 = doc.Tables[1];
                FileStream imgstream2 = new FileStream(Picpath2, FileMode.Open, FileAccess.Read);
                for (int i = 0; i < table2.Rows.Count; i++)
                {
                    XWPFTableRow tableRow = table2.Rows[i];
                    for (int j = 0; j < tableRow.GetTableCells().Count; j++)
                    {
                        XWPFTableCell tableRowCell = tableRow.GetTableCells()[j];
                        for (int k = 0; k < tableRowCell.Paragraphs.Count; k++)
                        {
                            XWPFParagraph par = tableRowCell.Paragraphs[k];
                            var runs = par.Runs;
                            if (par.ParagraphText.Contains("insertpic"))
                            {
                                for (int l = 0; l < runs.Count; l++)
                                {
                                    if (Convert.ToString(runs[l]) == "insertpic")
                                    {
                                        par.InsertNewRun(l).AddPicture(imgstream2, (int)PictureType.PNG, "img", 100 * 10000, 50 * 10000);
                                        par.ReplaceText("insertpic", null);
                                    }
                                }
                            }
                        }
                    }
                }

                //向段落中插入图片
                FileStream imgstream3 = new FileStream(Picpath3, FileMode.Open, FileAccess.Read);
                for (int i = 0; i < doc.Paragraphs.Count; i++)
                {
                    if (doc.Paragraphs[i].ParagraphText.Contains("insertpic"))
                    {
                        XWPFParagraph par = doc.Paragraphs[i];
                        var runs = par.Runs;
                        for (int j = 0; j < runs.Count; j++)
                        {
                            if (Convert.ToString(runs[j]) == "insertpic")
                            {
                                par.InsertNewRun(j).AddPicture(imgstream3, (int)PictureType.PNG, "img", 100 * 10000, 50 * 10000);
                                doc.Paragraphs[i].ReplaceText("insertpic", null);
                            }
                        }
                    }
                }

                FileStream file = new FileStream("E:\\outSample.docx", FileMode.Create, FileAccess.Write);
                doc.Write(file);
                file.Close();
            }

            Console.WriteLine("Hello, World!");
        }
    }
}

运行得到outSample.docx

image

posted @ 2024-03-14 10:24  NovNz  阅读(1476)  评论(3编辑  收藏  举报