VSTO Word 2013 - 如何对齐页脚中的字符串

VSTO Word 2013 - How to align string in footer

我创建了一个应用程序,当按下一个按钮时,它会在文档的页脚中放置一段指定的文本。

问题是我希望这个字符串总是出现在页脚的顶行,与右手边对齐。

有没有办法对齐页脚中的一段文字,使其始终显示在页脚的右上角?

好吧,我要说的是,这将需要更多研究如何获得正确的位置,并且可能需要进行一些硬编码调整。以此代码为起点

你需要解决很多问题,比如 您真的只想使用第 1 部分吗? 您真的只想使用主页脚吗? PageSetup 是否足以获得正确的位置?

也行

shp.RelativeVerticalPosition = Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionBottomMarginArea;

似乎没有任何影响,但可以通过 Word UI 进行设置。这将有助于更详细地探索,因为它会为您节省大量计算

using System;
using Word = Microsoft.Office.Interop.Word;

namespace WordAddIn1
{
    public class Class1
    {
        public void InsertShape(Word.Document doc)
        {
            try
            {
                Word.Section sec = doc.Sections[1];
                Word.HeaderFooter foo = sec.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];

                Word.Range rng = foo.Range;

                float leftPos = doc.PageSetup.PageWidth - doc.PageSetup.RightMargin;
                float topPos = doc.PageSetup.PageHeight - doc.PageSetup.BottomMargin;

                Word.Shape shp = doc.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal,
                                                       leftPos, topPos, 50, 20, rng);

                shp.TextFrame.TextRange.Text = "Text";
                shp.RelativeVerticalPosition = Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionBottomMarginArea;

            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}