In situations like downloading a huge Word document, we sometimes want to preview one or more pages of the document before downloading it. You can easily convert pages of a Word document into images to preview them without the help of MS Word.
The Syncfusion .NET Core Word Library (DocIO) can programmatically convert any Word document into images with just a few lines of code in C#. This library does not need Microsoft Word or any interop dependencies. With this functionality, you can easily preview Word documents in applications and share the images for printing.
This Word-to-image conversion is available in multiple environments such as:
- Windows
- Linux
- macOS
- Mobile devices
- Docker
- Cloud computing environments
This conversion function supports all the elements of a typical Word document: text, formatting, images, tables, hyperlinks, fields, table of contents, shapes, headers, footers, etc. You can convert commonly used Word file formats such as DOC, DOCX, and RTF into images and save them in different image formats like PNG, JPEG, and BMP.
Note: For more details, refer to our documentation on Converting Word documents to images in C#.
In this blog, we are going to discuss these options available in our Syncfusion .NET Core Word Library for Word-to-image conversion with code examples:
- Convert entire pages in a Word document to images.
- Convert the first page of a Word document to an image.
- Convert a specific range of pages to images.
Let’s get started with the implementation! Streamline your Word document workflow effortlessly with Syncfusion’s robust Word Library.
Getting started
Step 1: First, create a new C# .NET Core Console App in Visual Studio.
Step 2: Then, install the Syncfusion.DocIORenderer.Net.Core NuGet package as a reference to the app from NuGet Gallery.
Step 3: Now, include the following namespaces in the Program.cs file.
using Syncfusion.DocIO; using Syncfusion.DocIO.DLS; using Syncfusion.DocIORenderer;
The project is ready! Let’s write the code to convert a Word document into an image. The Syncfusion Word Library’s comprehensive APIs need only a few lines of code to get started with and are well-documented.
Convert all the pages in a Word document into images
With our .NET Core Word Library, you can easily convert all the pages of a Word document into images using the RenderAsImages method of the WordDocument instance.
Refer to the following code example.
//Open the file as Stream. //Open the file as Stream. using (FileStream docStream = new FileStream("Template.docx", FileMode.Open, FileAccess.Read)) { //Load an existing Word document. using (WordDocument wordDocument = new WordDocument(docStream, FormatType.Automatic)) { //Create a new instance of DocIORenderer. using (DocIORenderer render = new DocIORenderer()) { //Convert an entire Word document to images. Stream[] imageStreams = wordDocument.RenderAsImages(); for (int i = 0; i < imageStreams.Length; i++) { //Save the stream as file. using (FileStream fileStreamOutput = File.Create("WordToImage_" + i + ".jpeg")) { imageStreams[i].CopyTo(fileStreamOutput); } } } } }
By executing the previous code example, we will get output like in the following screenshot.
Experience the interactive demos to see the vast functionality of Syncfusion’s Word Library for yourself.
Convert the first page of a Word document to an image
If you’re dealing with a large document, you might decide to just export the first page or any single page of it as an image for previewing. You can use the RenderAsImage method of the WordDocument instance with an overload to provide the index of the page to be exported as image. To convert the first page of the Word document into an image, use the index value 0.
Refer to the following code example.
//Open the file as Stream. using (FileStream docStream = new FileStream("Template.docx", FileMode.Open, FileAccess.Read)) { //Load an existing Word document. using (WordDocument wordDocument = new WordDocument(docStream, FormatType.Automatic)) { //Create a new instance of DocIORenderer. using (DocIORenderer render = new DocIORenderer()) { //Convert the first page of the Word document into an image. Stream imageStream = wordDocument.RenderAsImages(0, ExportImageFormat.Jpeg); //Save the stream as file. using (FileStream fileStreamOutput = File.Create("WordToImage.jpeg")) { imageStream.CopyTo(fileStreamOutput); } } } }
By executing this code example, we will get output like in the following screenshot.
Convert a specific range of pages to images
You may wish to export a range of pages as images in a Word document. For this, use the RenderAsImages method of the WordDocument instance with an overload that accepts two-page indices. The first parameter is the start-page index, and the second parameter is the end-page index. The pages that fall in this range will be converted into images.
Refer to the following code example.
//Open the file as Stream. using (FileStream docStream = new FileStream("Template.docx", FileMode.Open, FileAccess.Read)) { //Load an existing Word document. using (WordDocument wordDocument = new WordDocument(docStream, FormatType.Automatic)) { //Create a new instance of DocIORenderer. using (DocIORenderer render = new DocIORenderer()) { //Convert a specific range of pages in Word document to images. Stream[] imageStreams = wordDocument.RenderAsImages(1, 2); for (int i = 0; i < imageStreams.Length; i++) { //Save the stream as file. using (FileStream fileStreamOutput = File.Create("WordToImage_" + i + ".jpeg")) { imageStreams[i].CopyTo(fileStreamOutput); } } } } }
GitHub reference
You can find all the examples for converting Word documents to images using Syncfusion .NET Core Word Library in the GitHub repository. The Syncfusion Word Library provides cross-platform compatibility and can be tailored to your unique needs.
Conclusion
Thanks for reading this blog. We have seen how to easily convert Word documents into images easily using the Syncfusion .NET Core Word Library. Take a moment to peruse the documentation, where you’ll find other options and features, all with accompanying code examples.
Apart from this Word-to-image conversion functionality, our Syncfusion .NET Word Library (DocIO) has the following significant functionalities and many more:
- Create, read, and edit Word documents programmatically.
- Create complex reports by merging data into a Word template from various data sources through mail merge.
- Merge, split, and organize Word documents.
- Convert Word documents into PDF, image, HTML, RTF, and other formats.
You can find more Word Library examples at this GitHub location.
Are you already a Syncfusion user? You can download the product setup here. If you’re not yet a Syncfusion user, you can download a 30-day free trial.
If you have questions, contact us through our support forum, support portal, or feedback portal. We are always happy to assist you!