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

时间:2016-08-04 15:32:14

标签: c# vsto add-in word-addins

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

麻烦的是,我希望这个字符串始终显示在页脚的顶行,与右侧对齐。

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

1 个答案:

答案 0 :(得分:0)

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

你需要解决很多问题,比如 你真的只想使用第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;
            }
        }
    }
}