How to set MS Word page size using automation API?

I need to change the page size of a Word Word document from Letter to A4 and find this automation class: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.document_members.aspx . Which property (possibly nested) needs to be set? I can not find anything related to page size.

0
source share
1 answer

Based on the documentation that references it , it is clear that Document provides PageSetup .

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

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

 document.PageSetup.PaperSize = WdPaperSize.wdPaperA4; 

To show how this can be done in a β€œfull” context, I 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, object libraries that come 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(); } } } 
+1
source

All Articles