Specify File Type When Loading Documents

Introduction

GroupDocs.Viewer for .NET is a versatile document rendering API that supports a wide range of file formats, including DOCX, PDF, PPTX, and more. By specifying the file type when loading documents, you can ensure accurate rendering and smooth viewing experience for your users.

Prerequisites

Before you begin, make sure you have the following prerequisites:

  • Basic knowledge of C# and .NET framework.
  • Visual Studio installed on your system.
  • GroupDocs.Viewer for .NET installed in your project. You can download it from here.

Import Namespaces

Firstly, you need to import the necessary namespaces into your C# code. These namespaces provide access to the classes and methods required for document rendering.

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

Step 1: Set up Output Directory

Define the directory where you want to save the rendered document pages.

string outputDirectory = "Your Document Directory";

Step 2: Define Page File Path Format

Specify the format for naming the output HTML files for each page of the document.

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

Step 3: Specify Load Options

Create an instance of the LoadOptions class and set the desired file type.

LoadOptions loadOptions = new LoadOptions
{
    FileType = FileType.DOCX
};

Step 4: Load Document and Render

Use the Viewer class to load the document and render it into HTML format.

using (Viewer viewer = new Viewer("YourDocument.docx", loadOptions))
{
    HtmlViewOptions options = HtmlViewOptions.ForEmbeddedResources(pageFilePathFormat);
    viewer.View(options);
}

Step 5: Display Success Message

Inform the user that the document has been rendered successfully and specify the location of the output files.

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

Conclusion

In this tutorial, we’ve learned how to use GroupDocs.Viewer for .NET to specify the file type when loading documents. By following these simple steps, you can ensure accurate rendering of various document formats in your .NET applications.

FAQ’s

Can I render documents other than DOCX using GroupDocs.Viewer for .NET?

Yes, GroupDocs.Viewer supports a wide range of file formats, including PDF, PPTX, XLSX, and more.

Is GroupDocs.Viewer for .NET compatible with .NET Core?

Yes, GroupDocs.Viewer for .NET is compatible with both .NET Framework and .NET Core.

Can I customize the output HTML files generated by GroupDocs.Viewer?

Yes, you can customize the HTML output using various options provided by the API.

Does GroupDocs.Viewer for .NET require any external dependencies?

No, GroupDocs.Viewer for .NET is a standalone library and does not require any external dependencies.

Is there a trial version available for GroupDocs.Viewer for .NET?

Yes, you can download a free trial version from here.