How do I programatically change the layout of a Word 2010 document?
We have an application that reads Word documents and imports them into our file format.
A recent开发者_StackOverflow bug was found that the page count is only available in Page Layout View, however Word 2010 defaults to Web Layout.
Using .NET c# how can we change the view to give us back the page count?
I believe the property you are looking for is Document.ActiveWindow.View.Type = wdPrintView;
You can read more in this on MSDN.
//Here is my code snipped based on 'DocumentFormat.OpenXml' nuget package.
//Open doc file
using (var wordDocument = WordprocessingDocument.Open(strInputFile,true))
{
SectionProperties sectionProps = new SectionProperties();
//Set page margins
PageMargin margin = new PageMargin() { Top = 1008,
Right = (UInt32Value)1008U,
Bottom = 1008,
Left = (UInt32Value)1008U,
Header = (UInt32Value)720U,
Footer = (UInt32Value)720U, `enter code here`
Gutter = (UInt32Value)0U };
sectionProps.Append(margin);
//Apply margin
wordDocument.MainDocumentPart.Document.Body.Append(sectionProps);
//Save changes
wordDocument.Save();
}
精彩评论