【问题标题】:How to add a "side" header or footer and align it vertically with word add-in vsto using c#?如何使用 c# 添加“侧面”页眉或页脚并将其与 word add-in vsto 垂直对齐?
【发布时间】:2017-05-26 14:10:23
【问题描述】:
我想通过 C# 使用 word addin vsto 插入页眉或页脚并将其垂直对齐到页面的一侧。以下是我希望看到的示例最终结果。我试图对此进行研究,但找不到有人以编程方式执行此操作的示例?有谁知道如何做到这一点?
所以现在我正在插入一个文本框,我可以像这样将它与页面的一侧对齐,但我不希望我的最终用户能够选择或删除它。所以我认为解决方案是将它放在“侧面”页眉或页脚中......但现在的问题是如何实现相同的对齐和定位?
【问题讨论】:
标签:
ms-word
vsto
office-interop
office-addins
【解决方案1】:
所以回答我自己的问题。您可以从当前活动文档和标题中获取“标题”,即使它看起来只是在顶部,它实际上跨越了可以容纳任何东西的整个文档(想想 adobe photo shop 中的图层)。无论如何,因此要插入此标题层并垂直对齐文本框,可以通过以下方式实现:
//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;
}