让形状永远保持在第一页

我正在开发Word的VSTO应用程序加载项,并希望在固定位置的第一页上保持形状。 有没有办法在没有主动监控形状状态的情况下做到这一点?

对于为什么解释而言,“它无法完成”的答案也是受欢迎的。

如果您将形状设置为标题并检查DifferentFirstPageHeaderFooter,则分页符不能满足您的需要,但形状将在背景上,并且页面布局>中断>下一页将形状复制到下一页。

private void ThisAddIn_Startup(object sender, System.EventArgs e) { AddFixedShapeOnFistPage(Application.Documents.Add(System.Type.Missing), MsoAutoShapeType.msoShapeRectangle, 160, 160, 30, 30); } public void AddFixedShapeOnFistPage(Microsoft.Office.Interop.Word.Document wordDocument, Microsoft.Office.Core.MsoAutoShapeType shapeType,int left, int top, int width, int height) { int wordTrueConst = -1; //https://social.msdn.microsoft.com/Forums/office/en-US/e9f963a9-18e4-459a-a588-17824bd3906d/differentfirstpageheaderfooter-bool-or-int?forum=worddev wordDocument.Sections[1].PageSetup.DifferentFirstPageHeaderFooter = wordTrueConst; wordDocument.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterFirstPage].Shapes.AddShape((int)shapeType, left, top, width, height); } 

形状将在背景上

是的,这是可以实现的。 代码如下:

 using Word = Microsoft.Office.Interop.Word; public void DrawShape() { try{ var wordApp = new Word.Application(); wordApp.Documents.Add(System.Type.Missing); Word.Document doc = wordApp.ActiveDocument; var shape = doc.Shapes.AddShape((int)Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle, 20, 20, 60, 20); } catch(Exception ex) { } } 

上述代码在文档第一页的左上角位置绘制宽度为60,高度为20的矩形(位置(20,20))。 请记住,(0,0)是Doc文件第一页左上角的起点。

在这里,Shapes.AddShape应该做到这一点。

 Shape AddShape(int Type, float Left, float Top, float Width, float Height, ref object Anchor = Type.Missing); 

有关SHapes.AddShape()的更多信息: https ://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.shapes.addshape.aspx

对于不同类型的形状,请参阅MsoAutoShapeType: https ://msdn.microsoft.com/en-us/library/microsoft.office.core.msoautoshapetype.aspx