Rachel
Rachel

Reputation: 31

get height and width of docx file without using office

I get count of pages in next code:

using DocumentFormat.OpenXml.Packaging;

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

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

can I get in this way height and width of file? or in another way but without using office.

Upvotes: 0

Views: 1590

Answers (2)

Remy
Remy

Reputation: 12713

Aspose.Word (which is not free) has these things built in: 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;

Someone had a similar problem and this is the discussion on SO (but with OpenXML):
Change Page size of Wor Document using Open Xml SDK 2.0
Maybe you can deduct your answer from this.

Upvotes: 1

Tahir Manzoor
Tahir Manzoor

Reputation: 597

Please use PageSetup.PageHeight and PageSetup.PageWidth properties to get the page's height and width. Hope this helps you.

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

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

I work with Aspose as Developer Evangelist.

Upvotes: 0

Related Questions