当我尝试向文档中添加图片时,我会添加1张图片的副本。 我的代码如下:
public static void InsertPicture(string document, string fileName)
{
List<KeyValuePair<string, string>> fieldValues = new List<KeyValuePair<string, string>>();
fieldValues.Add(new KeyValuePair<string, string>("x" , "https://some-picture-page.net/picture-1.jpeg"));
fieldValues.Add(new KeyValuePair<string, string>("x" , "https://some-picture-page.net/picture-2.jpeg"));
fieldValues.Add(new KeyValuePair<string, string>("y" , "https://some-picture-page.net/picture-3.jpeg"));
fieldValues.Add(new KeyValuePair<string, string>("y" , "https://some-picture-page.net/picture-4.jpeg"));
fieldValues.Add(new KeyValuePair<string, string>("y" , "https://some-picture-page.net/picture-5.jpeg"));
fieldValues.Add(new KeyValuePair<string, string>("x" , "https://some-picture-page.net/picture-6.jpeg"));
int i = 0;
foreach (var fieldValue in fieldValues)
{
i += 1;
using (WebClient webClient = new WebClient())
{
webClient.DownloadFile(fieldValue.Value, $"Picture {i}");
}
}
using (WordprocessingDocument wordProcessingDocument =
WordprocessingDocument.Open(document, true))
{
MainDocumentPart mainPart = wordProcessingDocument.MainDocumentPart;
ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg);
using (FileStream stream = new FileStream(fileName, FileMode.Open))
{
imagePart.FeedData(stream);
}
AddImageToBody(wordProcessingDocument, mainPart.GetIdOfPart(imagePart));
}
}
如果可能的话,我最好放弃整个下载部分。
AddImageToBody来自docs,位于页面底部,其中显示了示例代码。
现在,我不知道从哪里开始,因为Microsoft除了使用它之外没有提供对AddImageToBody代码的任何解释。