【发布时间】:2019-11-12 21:36:23
【问题描述】:
我有一个 MS word 文件,里面有一些句子,我需要在两行之间插入一些图像。当我在Microsoft.Office.Interop.Word 中使用AddPicture 方法时,我可以插入图像,但不能插入特定位置。
我没有找到除AddPicture 之外的任何插入方法来将图像插入现有的word 文件。 我正在尝试在苹果之后的特定行之后插入一张图片,那里应该有一张苹果的图片。
在这里,我正在创建一个段落并尝试添加图像。这是我的初始文件:
这包含包含苹果、芒果和葡萄等词的段落。
这是我的代码的输出(下)
图片应该插在苹果线之后 所需输出:
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Metadata;
using Word =Microsoft.Office.Interop.Word;
using System.IO;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
Word.Application ap = new Word.Application();
Word.Document document = ap.Documents.Open(@"C:\Users\ermcnnj\Desktop\Doc1.docx");
//document.InlineShapes.AddPicture(@"C:\Users\ermcnnj\Desktop\apple.png");
String read = string.Empty;
List<string> data = new List<string>();
for (int i = 0; i < document.Paragraphs.Count; i++)
{
string temp = document.Paragraphs[i + 1].Range.Text.Trim();
if (temp != string.Empty && temp.Contains("Apple"))
{
var pPicture = document.Paragraphs.Add();
pPicture.Format.SpaceAfter = 10f;
document.InlineShapes.AddPicture(@"C:\Users\ermcnnj\Desktop\apple.png", Range: pPicture.Range);
}
}
}
}
}
以上是我正在使用的代码。
【问题讨论】:
标签: c# image ms-word insertion