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();
}
}
}