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

标签 c# image ms-word office-interop

我使用 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");

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

最佳答案

为了让您的代码“按原样”工作,您需要在标题集合中添加一个名为“caption”的项目。试试“图”,这是 Word 的默认标签之一,看看它是否有效。

要将自定义题注标签添加到题注集合中,您可以使用 Word.CaptionLabels.Add 函数。并且,您将首先执行此操作,然后再尝试在 InsertCaption 方法中使用自定义标签。

如果您希望标题显示为“Figure # unique caption”,那么您可以使用 InsertCaption 方法的 Title 属性并在其中插入您的唯一标题。

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

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

关于c# - Microsoft.Office.Interop.Word : add caption to image in C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56443612/

相关文章:

c# - System.Net.Sockets.Tcpclient.Connect 错误 <String hostname, Int32 port>

c# - 为什么 DateTime.Now.ToString ("u") 不起作用?

MS word 的 VBA

c# - 将 2D 仿射变换矩阵转换为 3D 仿射变换矩阵

c# - 如何手动禁用/启用 mrtk v2 空间感知设置

C++:将整数文本文件转换为BMP格式的位图图像文件

image - 如何更改图像的整体色调,类似于 Photoshop 的色调调整?

html - 在不调整大小的情况下将大图像放置在较小的屏幕分辨率中

html - 无需安装即可将 HTML 或 Word 转换为 CHM

ms-word - 如何在Word文档中的文本上插入或放置一行?