Generating page preview for WordProcessing document
Generating page preview for WordProcessing document
Leave feedback
On this page
Starting from the version 23.5 the GroupDocs.Editor for .NET allows to generate a preview for arbitrary page in the loaded WordProcessing document, like DOC, DOCX, DOCM, RTF, ODT etc. The preview is generated in the SVG format, which is a vector graphics format and is supported in the numerous image viewers and also by any modern browser.
Using this feature the users can view and inspect any document of the imported WordProcessing document without actually editing it. This preview cannot be edited by the GroupDocs.Editor itself, it can be only obtained in SVG format and saved as usual - to the byte stream or file.
This feature is working regardless of the licensing mode of the GroupDocs.Editor for .NET: it works the same for both trial and licensed mode, there are no trial limitations for this feature. While generating the pages preview, the GroupDocs.Editor doesn’t write off the consumed bytes or credits.
With this feature was introduced, the GroupDocs.Editor for .NET finally supports generating a preview for all three major office format families: WordProcessing, Spreadsheet and Presentation.
For generating the pages preview for a particular WordProcessing document the user must perform the next steps:
Load a desired WordProcesing file to the Editor class.
Call the GetDocumentInfo() method and specify a password for the loaded document in case if this document is protected with a password.
In the obtained struct WordProcessingDocumentInfo invoke a new instance method GeneratePreview(int pageIndex) and in this method specify a zero-based index (do not confuse with the page numbers, which are 1-based) of the desired page. If the specified index is lesser than 0 or exceeds the number of pages within a given document, then the System.ArgumentOutOfRangeException exception will be thrown.
The GeneratePreview(int pageIndex) method returns a page preview as an SVG vector image, that is encapsulated in the SvgImage public class from the GroupDocs.Editor.HtmlCss.Resources.Images.Vector namespace. This class has all necessary methods and properties to obtain the content of an SVG image in any desired form, save it to disk, stream and so on.
The code sample below shows opening an unprotected WordProcessing DOCX file, obtaining a number of all pages inside this DOCX file, and then generating the previews for every page in a loop. Then these previews are saved to the disk. Please note that the GroupDocs.Editor is working in the trial mode, but page preview is generated for all the pages, and no trial copyright marks are present on these previews.
// Obtain a valid full path to the WordProcessing filestringinputPath="SampleDoc.docx";// Load WordProcessing file to the Editor constructorusing(GroupDocs.Editor.Editoreditor=newEditor(inputPath)){// Get document info for this fileIDocumentInfoinfoUncasted=editor.GetDocumentInfo(null);//Make sure it is really a WordProcessing of DOCX formatAssert.AreEqual(Formats.WordProcessingFormats.Docx,infoUncasted.Format);// Cast this document info to the WordProcessingDocumentInfo typeWordProcessingDocumentInfoinfoWordProcessing=(WordProcessingDocumentInfo)infoUncasted;// Get the number of all pagesintpagesCount=infoWordProcessing.PageCount;//Iterate through all pages and generate the preview on every iterationfor(intpageIndex=0;pageIndex<pagesCount;pageIndex++){// Generate one preview as SVG image by page indexSvgImageoneSvgPreview=infoWordProcessing.GeneratePreview(pageIndex);// Save SVG preview to the fileoneSvgPreview.Save(Path.Combine(outputFolder,oneSvgPreview.FilenameWithExtension));}}
All WordProcessing formats are able to store the raster images. So when the specific page of some loaded document has one or more raster images, and for this page the SVG preview is generated, this/these raster image(s) will be embedded inside SVG in base64 format using the data URI scheme.
Concluding, the new page preview feature is by its essence a new public method in the existing WordProcessingDocumentInfo struct, that obtains a page index and returns an instance of SvgImage class. If the end-user needs to obtain a preview of the page in a raster format, but not in the vector, he can use the SaveToPng method in the SvgImage class — this method converts the SVG content of the current SvgImage to the PNG format and saves it to the specified System.IO.Stream.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.