How to specify CSS Stream in HTML in C# and .NET
In modern programming, processing and converting document formats plays a significant role. One common scenario is converting RTF (Rich Text Format) to HTML, while also incorporating external CSS styles to style the resulting document. In this article, we'll look at how to use the component RTF TO HTML .NET from SautinSoft SDK to specify a CSS Stream — that is, to pass CSS styles, either indirectly or directly, when converting RTF to HTML in C# and .NET.
A CSS Stream is a stream into which CSS styles can be written, and then embedded into the HTML code as needed. In the context of document conversion libraries like SautinSoft, this means a way to dynamically specify styles, making the resulting HTML more flexible, customizable, and responsive to design requirements.
Technically, this involves providing a method or property that allows you to specify a stream (e.g., MemoryStream) to which the CSS will be written. This approach allows for on-the-fly style management, enabling dynamic CSS generation or including external styles without manually editing HTML. This solution eliminates unnecessary manual steps and automates the process, relying solely on programmatic methods.
Advantages of Using CSS Stream:
- Design flexibility: You can define different styles for different documents or sections.
- Dynamic generation: Allows you to programmatically generate CSS styles at runtime, which is convenient for automated systems.
- Style encapsulation: Provides cleaner code by separating styles from text content.
- Improved usability: When generating multiple documents with different styles, you can centrally manage styles via a stream.
This approach is a practical solution for those who need automatic styling of documents converted from RTF to HTML. For example:
- Creating templates with unique styles for different document types.
- Ensuring consistent design during mass conversion.
- Support for complex custom styling that cannot be defined statically.
This mechanism is particularly relevant in the following scenarios:
- Report and documentation generation: when a uniform, programmatically defined design is required.
- Automated document management systems: where the style can dynamically change based on business logic.
- Multi-style support: different CSS styles can be set for different document sections.
- Custom theme implementation: support for custom design settings.
Using CSS Stream in the context of RTF conversion is not the most common scenario, but it is becoming increasingly popular in professional systems that require mass automation, styling, and dynamic management of document types. In .NET systems with robust support for document formats, such mechanisms are combined with other approaches to expand visualization and design capabilities.
Other important aspects:
- Error handling: When working with streams, it is recommended to use try-catch blocks to handle possible errors.
- Dynamic CSS generation: CSS can be generated on the fly based on business logic or user settings.
- Reusability: the same CSS Stream can be connected to different documents.
- Integration with other systems: This technique is well suited for integration with web applications and report generation.
Complete code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using SautinSoft;
namespace Example
{
class Program
{
static void Main(string[] args)
{
SetCssStream();
}
/// <summary>
/// This sample shows how to specify CSS Stream.
/// </summary>
static void SetCssStream()
{
// Get your free key here:
// https://sautinsoft.com/start-for-free/
// If you need more information about "RTF to HTML .Net"
// Email us at: support@sautinsoft.com.
string inpFile = @"..\..\..\example.docx";
string outFile = @"Result.html";
string cssFile = @"Styles.css";
RtfToHtml r = new RtfToHtml();
// Create a separate file to store css.
FileStream fs = new FileStream(cssFile, FileMode.Create);
RtfToHtml.HtmlFlowingSaveOptions opt = new RtfToHtml.HtmlFlowingSaveOptions()
{
CssStream = fs,
KeepCssStreamOpen = false,
CssExportMode = RtfToHtml.CssExportMode.External,
CssFileName = cssFile,
Title = "Working with CSS."
};
try
{
r.Convert(inpFile, outFile, opt);
}
catch (Exception ex)
{
Console.WriteLine($"Conversion failed! {ex.Message}");
}
// Open the result.
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true });
}
}
}Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.IO
Imports SautinSoft
Namespace Example
Friend Class Program
Shared Sub Main(ByVal args() As String)
SetCssStream()
End Sub
''' <summary>
''' This sample shows how to specify CSS Stream.
''' </summary>
Private Shared Sub SetCssStream()
' Get your free key here:
' https://sautinsoft.com/start-for-free/
' If you need more information about "RTF to HTML .Net"
' Email us at: support@sautinsoft.com.
Dim inpFile As String = "..\..\..\example.docx"
Dim outFile As String = "Result.html"
Dim cssFile As String = "Styles.css"
Dim r As New RtfToHtml()
' Create a separate file to store css.
Dim fs As New FileStream(cssFile, FileMode.Create)
Dim opt As New RtfToHtml.HtmlFlowingSaveOptions() With {
.CssStream = fs,
.KeepCssStreamOpen = False,
.CssExportMode = RtfToHtml.CssExportMode.External,
.CssFileName = cssFile,
.Title = "Working with CSS."
}
Try
r.Convert(inpFile, outFile, opt)
Catch ex As Exception
Console.WriteLine($"Conversion failed! {ex.Message}")
End Try
' Open the result.
System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outFile) With {.UseShellExecute = True})
End Sub
End Class
End Namespace
If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below: