Document loads lazily into memory using .NET

Hi,


I noticed that Aspose.Words.Document is loading lazily into memory, not during creation of object but after accessing/calling properties/methods, like PageCount, GetPageInfo().
My question is: can I access some part of document (for example first page) without loading whole Document structure into memory?

My goal is to get snapshot from the first page, I use GetPageInfo() for this.

Thanks,
Roman

Hi Roman,

Thanks for your query. Please read Document Loading Overview from following documentation link.

Please use the following code snippet to convert each document page to image file.

// Open document.

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


ImageSaveOptions options = new ImageSaveOptions(SaveFormat.Png);

options.PageCount = 1;

for (int pageIndex = 0; pageIndex < doc.PageCount; pageIndex++)

{

string outputFileName = MyDir + string.Format("{0}_{1}.png", "Test", pageIndex + 1);

options.PageIndex = pageIndex;

doc.Save(outputFileName, options);

}

Please let us know if you have any more queries.

Thanks for reply,


Example upper forces whole document to be loaded in memory thru the call to doc.PageCount.
In my case, I need only image from the first page.
So, how can I get image from the first page without loading whole document into memory ?

Thanks,
Roman

Hi Roman,

I like to share with you that you can not get any information about document without loading it. Aspose.Words mimics the same as MS Words do. Please use the following code snippet to convert first page of document to image.

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


ImageSaveOptions options = new ImageSaveOptions(SaveFormat.Png);

options.PageCount = 1;

string outputFileName = MyDir + string.Format("{0}_{1}.png", "Test", pageIndex + 1);

options.PageIndex = 0;

doc.Save(outputFileName, options);

Hi there,

Thanks for your inquiry.

When you invoke one of the members such as PageCount or UpdatePageLayout the document is rendered in memory. Most likely this operation can only be performed fully (all pages) however you might have some luck using the RenderToScale method and only passing the first page (zero index).

This might render faster than using Save although I’m not 100% sure, please let us know if it does help.