获取docx文件的高度和宽度而不使用office

时间:2017-12-11 09:03:22

标签: c# ms-word

我在下一段代码中得到了页数:

using DocumentFormat.OpenXml.Packaging;

WordprocessingDocument doc = WordprocessingDocument.Open( @"D:\2pages.docx", false );

Console.WriteLine(
    doc.ExtendedFilePropertiesPart.Properties.Pages.InnerText.ToString()
);

我能以这种方式获得文件的高度和宽度吗? 或以其他方式但不使用办公室。

2 个答案:

答案 0 :(得分:1)

Aspose.Word(不是免费的)内置了这些东西: https://docs.aspose.com/display/wordsnet/Changing+Page+Setup+for+Whole+Document+using+Aspose.Words

Document doc = new Document();

// This truly makes the document empty. No sections (not possible in Microsoft Word).
doc.RemoveAllChildren();

// Create a new section node. 
// Note that the section has not yet been added to the document, 
// but we have to specify the parent document.
Section section = new Section(doc);

// Append the section to the document.
doc.AppendChild(section);

// Lets set some properties for the section.
section.PageSetup.SectionStart = SectionStart.NewPage;
section.PageSetup.PaperSize = PaperSize.Letter;

有人有类似的问题,这是关于SO的讨论(但使用OpenXML):
Change Page size of Wor Document using Open Xml SDK 2.0
也许你可以从中扣除你的答案。

答案 1 :(得分:0)

请使用PageSetup.PageHeight和PageSetup.PageWidth属性来获取页面的高度和宽度。希望这对你有所帮助。

Document doc = new Document(MyDir + "input.docx");

Console.WriteLine(doc.FirstSection.PageSetup.PageHeight);
Console.WriteLine(doc.FirstSection.PageSetup.PageWidth);

我与Aspose一起担任开发者布道者。