c# 使用word 标签 插入多图片 文字

c#操作word 通过替换标签 达到 使用word 模板

  1. 创建模板

c# 使用word 标签 插入多图片 文字_第1张图片

保存为模板

c# 使用word 标签 插入多图片 文字_第2张图片

添加引用

c# 使用word 标签 插入多图片 文字_第3张图片

c# 使用word 标签 插入多图片 文字_第4张图片

using msword=Microsoft.Office.Interop.Word;
using System.IO;
using System.Reflection;
using Microsoft.Office.Interop.Word;
//定义公共  

       public msword.Application wordApp = new msword.Application();
       public msword.Document wordDoc;
       object oMissing = System.Reflection.Missing.Value;
 public msword.Application wordApp = new msword.Application();
       public msword.Document wordDoc;
       object oMissing = System.Reflection.Missing.Value;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            object filepath1;
            
            object oMissing = System.Reflection.Missing.Value;

            filepath1 = Environment.CurrentDirectory;
            filepath1 = filepath1 + "\\123.dotx";                 //文件名

             wordApp.Visible = false;
             wordDoc = wordApp.Documents.Open(ref filepath1, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);//打开文件
 private void button1_Click(object sender, EventArgs e)
        {
            object[] oBookMark = new object[2];
            oBookMark[0] = "bq1";
           
            oBookMark[1] = "bq2";



            wordDoc.Bookmarks.get_Item(ref oBookMark[0]).Range.Text = "aa";   //改文字
           


           //循环添加图片示例,标签改图片

            int i = 0;
            int i1 =4;
            wordDoc.Bookmarks.get_Item(ref oBookMark[1]).Select();
            object linkToFile = true;      
            string filepath2 = Environment.CurrentDirectory;
            filepath2 = filepath2 + "\\img\\1.jpg";     //图片路径 ,改自己的          
           
             for (i = 0; i < i1 + 1; i++)
            {
                wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
                InlineShape inlineShape = wordApp.Selection.InlineShapes.AddPicture(filepath2, ref linkToFile, ref oMissing, ref oMissing);
                inlineShape.Width = 40;
                inlineShape.Height = 40;

            }
 wordApp.ActivePrinter = "HPRT N41BT";     //我这是静默打印,直接默认了打印机名称,控制面板里面看

          wordDoc.PrintOut(); //打印命令
          wordDoc.Saved = false;//放弃存盘
          wordDoc.Close(ref oMissing, ref oMissing, ref oMissing);//关了文件
          
        }

你可能感兴趣的:(c#入门,从0开始,c#)