使用C#.net在Word文档的另一个图形之上绘制图形

时间:2018-11-02 09:50:13

标签: c#

我已经创建了两个形状rect1rect2。我想在rect2上方绘制rect1

Shape rect1 = wordDoc.Shapes.AddShape((int)Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle, 10, 10, 100, 40, tempRange);

// Put components in that paragraph
rect1.TextFrame.TextRange.Text = "Arun";
rect1.TextFrame.TextRange.Font.Size = 8;
rect1.ConvertToInlineShape();

Shape rect2 = wordDoc.Shapes.AddShape((int)Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle, 10, 10, 50, 20, tempRange);

// Put components in that paragraph
rect2.TextFrame.TextRange.Text = "InnerShape";
rect2.TextFrame.TextRange.Font.Size = 8;
rect2.ConvertToInlineShape();
rect2.ZOrder(Microsoft.Office.Core.MsoZOrderCmd.msoBringToFront);

wordDoc.SaveAs(archDocFile, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

1 个答案:

答案 0 :(得分:0)

将呼叫转移到ConvertToInlineShape()。这些调用将形状从“绘图”层“移动”到“文本”层,这使得它们无法自由定位(它们变得像字符/单词/段落一样定位)。

参考:
https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.word.shape.converttoinlineshape

要定位现在浮动的形状,请设置属性TopLeft(或者也许设置TopRelativeLeftRelative)。阅读文档,看看它们如何工作!