Exporting indexed documents to HTML is a vital feature for applications that require sharing or viewing document content in a browser-friendly format. This process is especially useful in scenarios where documents are stored in an indexed format and need to be rendered dynamically in HTML for easier access and interaction. This article explores how to export indexed documents to HTML using C#. Using following steps, developers can efficiently implement this functionality with minimal effort. Moreover, an example will be provided to help you get started with C# export indexed documents to HTML.
Steps to Export Indexed Documents to HTML using C#
- Add the GroupDocs.Search for .NET library to your development environment to facilitate exporting indexed documents to HTML
- Set up an IndexSettings instance to define how documents are indexed, allowing you to configure properties such as text storage settings
- Initialize the Index by specifying the folder where documents will be stored and indexed
- Add documents from the specified folder using Index.Add method to prepare them for searching and exporting
- After indexing, you can retrieve the list of all indexed documents using Index.GetIndexedDocuments
- If there are any indexed documents, you can extract the content of a specific document and export it to HTML format
To begin, documents must first be indexed for efficient retrieval and manipulation. Using the code snippet provided, an Index object is created with settings that enable text storage with high compression. Once the documents are indexed, developers can extract their content and convert indexed documents to HTML C#. This involves retrieving the desired document from the index and using the FileOutputAdapter class to specify the output format as HTML. The content is then written to a file, making it accessible for further use or integration into web-based applications. This approach is both flexible and efficient, handling various document types seamlessly.
Code to Export Indexed Documents to HTML using C#
using GroupDocs.Search; | |
using GroupDocs.Search.Common; | |
using GroupDocs.Search.Options; | |
using GroupDocs.Search.Results; | |
namespace ExportIndexedDocumentstoHTMLUsingCSharp | |
{ | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// Apply the license to remove the restrictions | |
// imposed by the Search library | |
License lic = new License(); | |
lic.SetLicense(@"GroupDocs.Search.lic"); | |
string indexFolder = @"d:\MyIndex\"; | |
string documentsFolder = @"d:\MyDocuments\"; | |
// Creating an index settings instance | |
IndexSettings settings = new IndexSettings(); | |
// Enabling storage of extracted text in the index | |
settings.TextStorageSettings = new TextStorageSettings(Compression.High); | |
// Creating an index in the specified folder | |
Index index = new Index(indexFolder, settings); | |
// Indexing documents from the specified folder | |
index.Add(documentsFolder); | |
// Getting list of indexed documents | |
DocumentInfo[] documents = index.GetIndexedDocuments(); | |
// Getting a document text | |
if (documents.Length > 0) | |
{ | |
DocumentInfo document = documents[0]; | |
// Output to a file | |
FileOutputAdapter fileOutputAdapter = new FileOutputAdapter( | |
OutputFormat.Html, @"d:\Text.html"); | |
index.GetDocumentText(document, fileOutputAdapter); | |
} | |
} | |
} | |
} |
One of the main benefits of this solution is its platform flexibility. It works seamlessly whether you’re building a desktop application, a web-based solution, or deploying to the cloud, ensuring compatibility across all environments. This versatility allows you to save indexed documents as HTML in C# and deliver solutions that are robust and adaptable to various platforms. By leveraging this capability, developers can create user-friendly and efficient document management systems tailored to their specific needs. Whether it’s for financial reports, research documents, or other data-driven content, exporting documents to HTML ensures that content is easily accessible and can be viewed across multiple devices without the need for specialized software.
Previously, we provided a comprehensive guide on numeric range search in C#. For a complete step-by-step tutorial, be sure to read our detailed article on numeric range search in C#.