从C#设置Word 2010文档边距

时间:2012-01-17 02:39:46

标签: c# office-interop word-automation

我想在C#中使用自动化创建的Word文档上设置边距。

我已使用ActiveDocument.TopMargin =启动了该过程,但我找不到类似于vb的C#代码Word.InchesToPoint(.5)任何帮助都将非常感谢

3 个答案:

答案 0 :(得分:7)

有时最简单的方法有效。这行代码解决了这个问题

oWord.ActiveDocument.PageSetup.TopMargin = (float)50;

答案 1 :(得分:3)

您必须获取Word应用程序的实例:

Word.Application oWord = new Word.Application();
oWord.InchesToPoint((float)0.5);

参见参考资料: http://msdn.microsoft.com/en-us/library/ff197549.aspx

答案 2 :(得分:1)

您可以使用Word Application对象的InchesToPoints方法,如下所示:

        Word.Application wrdApplication = new Word.Application();
        Word.Document wrdDocument;
        wrdApplication.Visible = true;
        wrdDocument = wrdApplication.Documents.Add();
        wrdDocument.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape;
        wrdDocument.PageSetup.TopMargin = wrdApplication.InchesToPoints(0.5f);
        wrdDocument.PageSetup.BottomMargin = wrdApplication.InchesToPoints(0.5f);
        wrdDocument.PageSetup.LeftMargin = wrdApplication.InchesToPoints(0.5f);
        wrdDocument.PageSetup.RightMargin = wrdApplication.InchesToPoints(0.5f);

或者如果你愿意,你可以自己制作......

    private float InchesToPoints(float fInches)
    {
        return fInches * 72.0f;
    }

它可以用在这样的东西上:

        Word.Application wrdApplication = new Word.Application();
        Word.Document wrdDocument;
        wrdDocument = wrdApplication.Documents.Add();
        wrdDocument.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape;
        wrdDocument.PageSetup.TopMargin = InchesToPoints(0.5f); //half an inch in points
        wrdDocument.PageSetup.BottomMargin = InchesToPoints(0.5f);
        wrdDocument.PageSetup.LeftMargin = InchesToPoints(0.5f);
        wrdDocument.PageSetup.RightMargin = InchesToPoints(0.5f);
        wrdApplication.Visible = true;

Word在其间距中每英寸使用72个点。