Microsoft.Office.Interop.Word: 在 C# 中为图像添加标题

Microsoft.Office.Interop.Word: add caption to image in C#

我使用 Microsoft.Office.Interop.Word 在 C# 中创建 Word 文件。 我插入图片:

Word.InlineShape pictureShape = paragraph.Range.InlineShapes.AddPicture(img.Url, ref missing, ref missing, ref missing);

但我无法为其插入标题。

我试过使用:

 pictureShape.Range.InsertCaption("caption");

但它不起作用。谁能帮帮我?

为了让您的代码正常工作 "as is",您需要在标题 collection 中添加一个名为 "caption" 的项目。尝试 "Figure",这是 Word 的默认标签之一,看看是否有效。

要向标题 collection 添加自定义标题标签,请使用 Word.CaptionLabels.Add 函数。而且,在尝试在 InsertCaption 方法中使用自定义标签之前,您将首先执行此操作。

如果您想要阅读标题,"Figure # unique caption" 那么您可以在 InsertCaption 方法中使用标题 属性 并在其中插入您独特的标题。

这是一个来自 MSDN post 的添加您自己的自定义标签的示例:

oWordApp.CaptionLabels.Add("Picture");
object caption = oWordApp.CaptionLabels["Picture"];
oWordApp.Selection.InsertCaption(ref caption, ref oLabel, ref missing, ref missing, ref missing);