How to set MS Word page size via the automation API?

安稳与你 提交于 2020-01-06 06:42:08

问题


I need to change MS Word document's page size from Letter to A4 and found this automation class: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.document_members.aspx. Which property (possibly a nested one) do I need to set? I can't find anything related to page size.


回答1:


Based on the documentation you reference it is seen that a Document exposes a PageSetup property.

The PageSetup property has a PaperSize property which allow you to define the paper size of the document - the complete list of available paper sizes is specified by the WdPaperSize enum ( see its members here: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.wdpapersize.aspx ).

So basically, to set the paper size of a document you can do something like this:

document.PageSetup.PaperSize = WdPaperSize.wdPaperA4;

To show how this can be done in a "complete" context, I have included a complete sample in the following. The sample is implemented as a C# console application using .NET 4.5, Microsoft Office Object Library version 15.0, and Microsoft Word Object Library version 15.0 ( that is, the object libs. that ships with MS Office 2013 ).

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

namespace WordDocStats
{
    class Program
    {
        static void Main()
        {
            // Open a doc file
            var wordApplication = new Application();
            var document = wordApplication.Documents.Open(@"C:\Users\Username\Documents\document.docx");

            // Set paper size
            document.PageSetup.PaperSize = WdPaperSize.wdPaperA4;

            // Save settings
            document.Save();

            // Close word
            wordApplication.Quit();
            Console.ReadLine();
        }
    }
}


来源:https://stackoverflow.com/questions/12620003/how-to-set-ms-word-page-size-via-the-automation-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!