Render Selected Pages

Introduction

In this tutorial, we’ll delve into how to utilize Groupdocs.Viewer for .NET to render selected pages from a document. Whether you’re a seasoned developer or just starting out, this step-by-step guide will walk you through the process with ease.

Prerequisites

Before we begin, make sure you have the following prerequisites in place:

1. Installation

Ensure that you have Groupdocs.Viewer for .NET installed in your development environment. If not, you can download it from the Download link.

Importing Namespaces

In your C# code file, import the necessary namespaces to access the required classes and methods. You can do this using the using directive:

using System;
using System.IO;
using GroupDocs.Viewer.Options;

Now let’s break down the example code provided into multiple steps:

Step 1: Set Output Directory

Define the directory where you want the rendered pages to be saved. Replace "Your Document Directory" with the desired directory path.

string outputDirectory = "Your Document Directory";

Step 2: Define Page File Path Format

Specify the format for the file paths of the rendered pages. This will be used to save each page as an HTML file in the output directory.

string pageFilePathFormat = Path.Combine(outputDirectory, "page_{0}.html");

Step 3: Instantiate Viewer Object

Create an instance of the Viewer class, passing the path of the document you want to render as an argument.

using (Viewer viewer = new Viewer(TestFiles.SAMPLE_DOCX))

Step 4: Configure HTML View Options

Set up the HTML view options for rendering. In this example, we’re configuring options to embed resources in the HTML output.

HtmlViewOptions options = HtmlViewOptions.ForEmbeddedResources(pageFilePathFormat);

Step 5: Render Selected Pages

Specify the page numbers you want to render. In this case, we’re rendering pages 1 to 3. Then, call the View method on the Viewer object, passing the options and page numbers as arguments.

viewer.View(options, 1, 3);

Step 6: Output Result

Finally, display a message indicating the successful rendering of the document and the location where the output files are saved.

Console.WriteLine($"\nSource document rendered successfully.\nCheck output in {outputDirectory}.");

Conclusion

Congratulations! You’ve successfully learned how to render selected pages from a document using Groupdocs.Viewer for .NET. With this knowledge, you can now integrate document rendering capabilities into your .NET applications with ease.

FAQ’s

Q: Can I render pages from different types of documents, such as PDFs or images?

A: Yes, Groupdocs.Viewer for .NET supports rendering pages from various document formats, including PDFs, Microsoft Office documents, and image files.

Q: Is there a trial version available for testing before purchasing?

A: Yes, you can access a free trial version of Groupdocs.Viewer for .NET from the website.

Q: Can I customize the output format other than HTML?

A: Absolutely, Groupdocs.Viewer for .NET provides options to render pages as images, PDFs, and more, in addition to HTML.

Q: How can I obtain temporary licenses for testing purposes?

A: Temporary licenses can be acquired from the temporary license page on the Groupdocs website.

Q: Where can I seek assistance or get help with any issues I encounter?

A: You can visit the Groupdocs.Viewer forum for support and guidance from the community and developers.