Установить поля документа Word 2010 из C#

Я хочу установить поля в документе Word, который я создаю с использованием автоматизации из кода на C#.

Я начал процесс, используя ActiveDocument.TopMargin = но я не могу найти код C# похож на VB Word.InchesToPoint(.5) Любая помощь будет принята с благодарностью

3 ответа

Иногда самый простой способ работает. Эта строка кода решила проблему

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

Вы должны получить экземпляр приложения Word:

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

См. Ссылку: http://msdn.microsoft.com/en-us/library/ff197549.aspx

Вы можете использовать метод InchesToPoints объекта Word Application следующим образом:

        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 точки на дюйм в своем интервале.

Другие вопросы по тегам