Convert TXT to Multiple Formats with GroupDocs.Viewer .NET
Introduction
Looking to convert text documents into various formats such as HTML, JPG, PNG, or PDF effortlessly? Managing document conversions can be challenging, especially when dealing with multiple pages or specific format requirements. GroupDocs.Viewer for .NET simplifies the process of rendering TXT files into diverse output formats, ensuring your data is accessible and visually appealing.
In this guide, we’ll explore how to use GroupDocs.Viewer for .NET to transform TXT documents into multi-page HTML, single-page HTML, JPG, PNG, and PDF. By the end, you’ll master:
- Converting TXT files using C# with GroupDocs.Viewer
- Implementing different rendering options for your needs
- Optimizing performance during conversions
Let’s dive in to solve your document conversion challenges.
Prerequisites
Before we begin, ensure you have the following ready:
- Development Environment: Visual Studio 2019 or later.
- .NET Framework: Version 4.6.1 or higher.
- GroupDocs.Viewer for .NET Library:
- Via NuGet Package Manager Console:
Install-Package GroupDocs.Viewer -Version 25.3.0
- Using .NET CLI:
dotnet add package GroupDocs.Viewer --version 25.3.0
- Via NuGet Package Manager Console:
Familiarity with C# programming and basic file operations in .NET is recommended to follow along easily.
Setting Up GroupDocs.Viewer for .NET
Installation
To begin, install the GroupDocs.Viewer library using your preferred package manager:
NuGet Package Manager Console
Install-Package GroupDocs.Viewer -Version 25.3.0
.NET CLI
dotnet add package GroupDocs.Viewer --version 25.3.0
Licensing
You can start with a free trial or obtain a temporary license to explore the full capabilities of GroupDocs.Viewer for .NET without evaluation limitations:
- Free Trial: Download here
- Temporary License: Request here
For ongoing use, consider purchasing a license directly from GroupDocs.
Basic Initialization
To set up GroupDocs.Viewer in your project:
using System.IO;
using GroupDocs.Viewer;
string outputDirectory = Path.Combine("YOUR_DOCUMENT_DIRECTORY", "Output");
// Initialize the Viewer object with a TXT file path.
using (Viewer viewer = new Viewer(Path.Combine("YOUR_DOCUMENT_DIRECTORY", "Sample.txt")))
{
// Your rendering code will go here.
}
Implementation Guide
Now, let’s delve into each feature and see how you can implement them.
Render TXT Document to Multi-page HTML
Overview
This feature demonstrates converting a TXT document into multi-page HTML format. Each page of the text file is rendered as an individual HTML file with embedded resources.
Step 1: Setup the Viewer
Create a Viewer
object for your source TXT file:
using System.IO;
using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
string outputDirectory = Path.Combine("YOUR_DOCUMENT_DIRECTORY", "Output");
string pageFileFullPath = Path.Combine(outputDirectory, "Txt_result.html");
// Initialize the Viewer with a sample text file.
using (Viewer viewer = new Viewer(Path.Combine("YOUR_DOCUMENT_DIRECTORY", "Sample.txt")))
{
// Continue to step 2...
Step 2: Configure HTML View Options
Set up HtmlViewOptions
to render each page separately:
// Set up HTML view options for multi-page rendering.
HtmlViewOptions options = HtmlViewOptions.ForEmbeddedResources(pageFileFullPath);
// Render the document as multi-page HTML.
viewer.View(options);
}
Explanation: The ForEmbeddedResources()
method ensures that resources like images and styles are embedded directly within the HTML file, facilitating easy sharing.
Render TXT Document to Single Page HTML
Overview
Convert a TXT document into a single HTML page, ideal for documents that need to be displayed as one continuous webpage.
Step 1: Setup the Viewer
Initialize the Viewer
object:
string outputDirectory = Path.Combine("YOUR_DOCUMENT_DIRECTORY", "Output");
string pageFileFullPath = Path.Combine(outputDirectory, "Txt_result_single_page.html");
// Initialize a new Viewer instance for a different text file.
using (Viewer viewer = new Viewer(Path.Combine("YOUR_DOCUMENT_DIRECTORY", "Sample2.txt")))
{
// Continue to step 2...
Step 2: Configure Single Page HTML Options
Configure HtmlViewOptions
with the single-page setting enabled:
// Set up options for rendering into a single HTML page.
HtmlViewOptions options = HtmlViewOptions.ForEmbeddedResources(pageFileFullPath);
options.RenderToSinglePage = true;
// Render as a single HTML page.
viewer.View(options);
}
Explanation: The RenderToSinglePage
property ensures that the entire content is rendered on one page.
Render TXT Document to JPG
Overview
This feature allows you to convert a text document into a JPEG image, useful for visual presentations or archiving purposes.
Step 1: Setup the Viewer
Prepare your Viewer
object:
string outputDirectory = Path.Combine("YOUR_DOCUMENT_DIRECTORY", "Output");
string pageFileFullPath = Path.Combine(outputDirectory, "Txt_result.jpg");
// Initialize the viewer with a sample file.
using (Viewer viewer = new Viewer(Path.Combine("YOUR_DOCUMENT_DIRECTORY", "Sample.txt")))
{
// Continue to step 2...
Step 2: Configure JPG View Options
Set up JpgViewOptions
for image rendering:
// Set up options for rendering as a JPG image.
JpgViewOptions options = new JpgViewOptions(pageFileFullPath);
// Render the document as a JPEG file.
viewer.View(options);
}
Explanation: The JpgViewOptions
class specifies how to render and save each page of your document in JPEG format.
Render TXT Document to PNG
Overview
Convert a text document into PNG format, offering high-quality image output with transparency support.
Step 1: Setup the Viewer
Initialize the Viewer
object:
string outputDirectory = Path.Combine("YOUR_DOCUMENT_DIRECTORY", "Output");
string pageFileFullPath = Path.Combine(outputDirectory, "Txt_result.png");
// Create a viewer instance for your TXT file.
using (Viewer viewer = new Viewer(Path.Combine("YOUR_DOCUMENT_DIRECTORY", "Sample.txt")))
{
// Continue to step 2...
Step 2: Configure PNG View Options
Set up PngViewOptions
:
// Set up view options for rendering as a PNG image.
PngViewOptions options = new PngViewOptions(pageFileFullPath);
// Render the document in PNG format.
viewer.View(options);
}
Explanation: The PngViewOptions
class allows each page to be rendered with transparency, making it suitable for layered graphics.
Render TXT Document to PDF
Overview
This feature is perfect for converting text documents into a universally accessible PDF format.
Step 1: Setup the Viewer
Prepare your Viewer
object:
string outputDirectory = Path.Combine("YOUR_DOCUMENT_DIRECTORY", "Output");
string pageFileFullPath = Path.Combine(outputDirectory, "Txt_result.pdf");
// Initialize a viewer instance for your sample text file.
using (Viewer viewer = new Viewer(Path.Combine("YOUR_DOCUMENT_DIRECTORY", "Sample.txt")))
{
// Continue to step 2...
Step 2: Configure PDF View Options
Set up PdfViewOptions
:
// Set up view options for rendering as a PDF document.
PdfViewOptions options = new PdfViewOptions(pageFileFullPath);
// Render the document into a PDF file.
viewer.View(options);
}
Explanation: The PdfViewOptions
class specifies how to convert and save your TXT files as PDF documents.
Conclusion
With GroupDocs.Viewer for .NET, converting text documents into various formats is straightforward. This guide covered transforming TXT files into multi-page HTML, single-page HTML, JPG, PNG, and PDF using C#. Whether you’re looking to enhance document accessibility or compatibility, these methods provide robust solutions.
For further assistance or more advanced features, refer to the official GroupDocs.Viewer documentation. Happy coding!