如何使用 c# 添加 "side" 页眉或页脚并将其与 word 加载项 vsto 垂直对齐?

How to add a "side" header or footer and align it vertically with word add-in vsto using c#?

我想插入一个页眉或页脚,并通过 C# 使用 word addin vsto 将其垂直对齐到页面的一侧。以下是我希望看到的示例最终结果。我试图对此进行研究,但找不到有人以编程方式执行此操作的示例?有谁知道如何做到这一点?

所以现在我正在插入一个文本框,我可以像这样将它对齐到页面的一侧,但我不希望我的最终用户能够 select 或删除它。所以我认为解决方案是将它放在 "side" 页眉或页脚中...但现在的问题是如何实现相同的对齐和定位?

所以回答我自己的问题。您可以从当前活动文档和 header 中获取 "header",即使它看起来只在顶部,但它实际上跨越了可以容纳任何内容的整个文档(想想 adobe photo shop 中的图层) .无论如何,要插入此 header 层并垂直对齐文本框,可以通过以下方式实现:

//note: we are in ThisAddin.cs (word addin) and this.Application = word addin

private void drawTdCycleTextBox(String cycleCode)
        {
            int length = 1000;

            // get the current cursor location
            Range cursorRange = this.Application.Selection.Range;

            // get the primary header on the current page
            HeaderFooter header = this.Application.ActiveDocument.Sections.First.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];
            // header.Range.Text = "THIS IS A TEST";

            // create the textbox inside the header and at the current cursor location
            Microsoft.Office.Interop.Word.Shape textBox = header.Shapes.AddTextbox(Office.MsoTextOrientation.msoTextOrientationUpward, 0, 0, 35, length, cursorRange);
            textBox.TextFrame.TextRange.Font.Size = 20;
            textBox.TextFrame.TextRange.Text = Util.repeatedTextOutput(cycleCode, 25, length);
            textBox.Fill.ForeColor.RGB = ColorTranslator.ToOle(Color.Turquoise);
            textBox.Fill.Transparency = .35F;
        }