如何通过自动化API设置MS Word页面大小? [英] How to set MS Word page size via the automation API?
问题描述
我需要将MS Word文档的页面大小从Letter更改为A4,并找到以下自动化类: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.document_members.aspx 。我需要设置哪个属性(可能是嵌套属性)?我找不到与页面大小有关的任何内容。
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.
推荐答案
基于您引用的文档中可以看到 Document
公开了 PageSetup
属性。
Based on the documentation you reference it is seen that a Document
exposes a PageSetup
property.
PageSetup
属性具有 PaperSize
属性,可让您定义文档的纸张尺寸-可用纸张尺寸的完整列表由 WdPaperSize
枚举指定(请参阅其成员)此处: http://msdn.microsoft.com / zh-CN / library / m icrosoft.office.interop.word.wdpapersize.aspx )。
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;
为了显示如何在完整的上下文中完成此操作,我在其中提供了完整的示例下列。该示例使用.NET 4.5,Microsoft Office对象库15.0版和Microsoft Word对象库15.0版(即MS Office 2013附带的对象库)作为C#控制台应用程序实现。
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();
}
}
}
这篇关于如何通过自动化API设置MS Word页面大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!