### Convert HTML to WORD Source: https://github.com/sautinsoft/sautinsoft.htmltortf.examples/blob/master/readme.md Example demonstrating how to convert an HTML file to DOCX or RTF format using SautinSoft.HtmlToRtf. ```csharp SautinSoft.HtmlToRtf h = new SautinSoft.HtmlToRtf(); string inputFile = @"sample.html"; // You want to save in DOCX. string outputFile = @"result.docx"; // You want to save in RTF. string outputFile = @"result.rtf"; h.OpenHtml(inputFile); h.ToDocx(outputFile); ``` -------------------------------- ### Python HTML to RTF Conversion Example Source: https://github.com/sautinsoft/sautinsoft.htmltortf.examples/blob/master/Platforms/Python/Sample.html A basic example demonstrating how to convert an HTML string to RTF format using the SautinSoft.HtmlToRtf library in Python. ```python import sys import os # Add the parent directory to the system path sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..\..\..\..\lib"))) from SautinSoft.HtmlToRtf import * # Convert HTML string to RTF # Example 1: Convert HTML string to RTF file htmlString = "\n\n

This is a Heading

\n

This is a paragraph.

\n\n" converter = HtmlToRtf() converter.ConvertHtmlToRtfFile(htmlString, "output.rtf") # Example 2: Convert HTML file to RTF file converter.Convert("sample.html", "output_from_file.rtf") # Example 3: Convert HTML string to RTF string rtfString = converter.ConvertHtmlToRtfString(htmlString) print("RTF String:\n", rtfString) print("\nConversion complete. Check output.rtf and output_from_file.rtf.") ``` -------------------------------- ### Merge multiple RTF files Source: https://github.com/sautinsoft/sautinsoft.htmltortf.examples/blob/master/readme.md Example showing how to merge multiple RTF files into a single RTF document, with an option to insert page breaks between them. ```csharp SautinSoft.HtmlToRtf h = new SautinSoft.HtmlToRtf(); // Array with several RTF files. string[] rtfFiles = new string[] { "footer.rtf", "footer.rtf", "footer.rtf" }; string singleRtf = String.Empty; // Let's divide RTF documents using page break. h.MergeOptions.PageBreakBetweenDocuments = true; foreach (string rtfFile in rtfFiles) { string rtfFilePath = Path.Combine(htmlDir.FullName, rtfFile); // Copy 1st RTF to 'singleRtf' if (String.IsNullOrEmpty(singleRtf)) singleRtf = File.ReadAllText(rtfFilePath); // Merge 2nd, 3rd .... else singleRtf = h.MergeRtfString(singleRtf, File.ReadAllText(rtfFilePath)); } // Save 'singleRtf' to a file only for demonstration purposes. string singleRtfPath = Path.Combine(htmlDir.FullName, "single.rtf"); File.WriteAllText(singleRtfPath, singleRtf); ``` -------------------------------- ### HTML to RTF Stream Conversion Source: https://github.com/sautinsoft/sautinsoft.htmltortf.examples/blob/master/VB.Net/02. HTML to RTF/02. Convert HTML to RTF Stream/utf-8.html This example demonstrates the basic conversion of an HTML string to an RTF stream. ```VB.Net Imports System.IO Imports SautinSoft.HtmlToRtf.converters Module Module1 Sub Main() ' Create instance of HtmlToRtf. Dim converter As New HtmlToRtf() ' Specify the HTML content. Dim htmlString As String = "

Hello, World!

This is a sample paragraph.

" ' Create a MemoryStream to hold the RTF data. Using rtfStream As New MemoryStream() ' Convert HTML to RTF and write to the stream. converter.ConvertHtmlToStream(htmlString, rtfStream) ' Reset the stream position to the beginning. rtfStream.Position = 0 ' You can now read from the rtfStream or save it to a file. ' For demonstration, let's read the content. Using reader As New StreamReader(rtfStream) Dim rtfContent As String = reader.ReadToEnd() Console.WriteLine("RTF Content:") Console.WriteLine(rtfContent) End Using End Using Console.WriteLine("Conversion complete.") End Sub End Module ``` -------------------------------- ### Convert HTML to Text file Source: https://github.com/sautinsoft/sautinsoft.htmltortf.examples/blob/master/CSharp/03. HTML to Text/01. Convert HTML to Text file/Sample.html This code snippet shows the basic usage of the HtmlToText class to convert an HTML file to a plain text file. ```C# using System; using Sautinsoft.HtmlToText; namespace Sautinsoft.Examples { class Program { static void Main(string[] args) { // Specify the path to your HTML file. string htmlFilePath = @"your_html_file.html"; // Specify the path for the output text file. string textFilePath = @"output.txt"; try { // Create an instance of the HtmlToText class. HtmlToText h = new HtmlToText(); // Convert HTML to text and save to a file. // You can also use h.Convert("...") to convert from a string. h.ConvertToFile(htmlFilePath, textFilePath); Console.WriteLine($"HTML file "{htmlFilePath}" converted to text file "{textFilePath}" successfully."); } catch (Exception ex) { Console.WriteLine($"An error occurred: {ex.Message}"); } } } } ``` -------------------------------- ### Convert HTML to Text Stream Source: https://github.com/sautinsoft/sautinsoft.htmltortf.examples/blob/master/CSharp/03. HTML to Text/02. Convert HTML to Text Stream/Sample.html This C# code snippet shows how to use the HtmlToText class to convert an HTML string to a text stream. ```csharp using System; using System.IO; using SautinSoft.HtmlToText; namespace SautinSoft.Examples { class Program { static void Main(string[] args) { // Sample HTML content. string htmlContent = "

Hello, World!

This is a sample paragraph.

"; // Create an instance of HtmlToText. HtmlToText converter = new HtmlToText(); // Use a MemoryStream to capture the text output. using (MemoryStream stream = new MemoryStream()) { // Convert HTML to text and write to the stream. converter.Convert(htmlContent, stream); // Reset the stream position to the beginning to read its content. stream.Position = 0; // Read the text from the stream. using (StreamReader reader = new StreamReader(stream)) { string textOutput = reader.ReadToEnd(); Console.WriteLine("Converted Text:"); Console.WriteLine(textOutput); } } } } } ``` -------------------------------- ### Convert HTML to Text string Source: https://github.com/sautinsoft/sautinsoft.htmltortf.examples/blob/master/CSharp/03. HTML to Text/03. Convert HTML to Text string/Sample.html This C# code snippet shows how to use the HtmlToText class to convert an HTML string into a plain text string. ```csharp using System; using Sautinsoft.HtmlToText; namespace Sautinsoft.Examples { class Program { static void Main(string[] args) { // The HTML content to convert. string htmlContent = "

Hello, World!

This is a sample paragraph.

"; // Create an instance of the HtmlToText class. HtmlToText converter = new HtmlToText(); // Convert the HTML content to plain text. string plainText = converter.Convert(htmlContent); // Print the resulting plain text. Console.WriteLine(plainText); } } } ``` -------------------------------- ### HTML to RTF Conversion Example Source: https://github.com/sautinsoft/sautinsoft.htmltortf.examples/blob/master/CSharp/02. HTML to RTF/01. Convert HTML to RTF file/Sample.html This HTML content includes various formatting elements that will be converted to RTF. ```html

Welcome to SautinSoft!

This is HTML sample

Subtitle

This rich text document content serves as basis for trying out various rich text formatting.

Header 1

Bold italic underlined strikethrough Nsubscript Nsuperscript.

Sub header 1.1

Fonts:

Heading level 3 (1.1.1)

Background testing

Foreground testing

A combination of both

A combination of both

A combination of both

Heading level 3 (1.1.2)

Heading level 4 (1.1.2.1)

Ordered list of Popular web fonts

  1. Gotham
  2. Museo Sans
  3. Futura
  4. Helvetica
  5. Akkurat
  6. Neutra Text
  7. Avenir
  8. Bliss
  9. Frutiger
  10. Univers
Heading level 5 (1.1.2.1.1)

Multi-level

  1. A
    1. A1
      1. A.1.1
    2. A2
      1. A.2.1
  2. B
    1. B1
      1. B.1.1
      2. B.1.2
    2. B2
      1. B.2.1
      2. B.2.2
  3. C
Heading level 5 (1.1.2.1.2)

Bullet lists

Heading level 4 (1.1.2.2)

Heading level 3 (1.2.2)

ABCD
EFGH
IJKL
MNOP
ABCD
EFGH
IJKL
MNOP
ABCD
EFGH
IJKL
MNOP
ABCD
EFGH
IJKL
MNOP
ABCD
EFGH
IJKL
MNOP
A Testing testing testing testingB Testing testing testing testingC Testing testing testing testingD Testing testing testing testing
E Testing testing testing testingF Testing testing testing testingG Testing testing testing testingH Testing testing testing testing
I Testing testing testing testingJ Testing testing testing testingK Testing testing testing testingL Testing testing testing testing
M Testing testing testing testingN Testing testing testing testingO Testing testing testing testingP Testing testing testing testing
``` -------------------------------- ### Docker Commands for HTMLtoRTF Source: https://github.com/sautinsoft/sautinsoft.htmltortf.examples/blob/master/Platforms/DockerHTMLtoRTF/Readme.md A sequence of Docker commands to build an image, run a container, copy output files, and remove the container and image. ```cmd :: 1. Create docker image named "htmltortfimage". docker build -t htmltortfimage -f Dockerfile . :: 2. Create and start docker container named "htmltortfcontainer". docker run --name htmltortfcontainer htmltortfimage :: 3. Copy output files from container to project's folder. docker cp htmltortfcontainer:/app/sample.html . docker cp htmltortfcontainer:/app/sample.rtf . :: 4. Clean up, remove container and image. docker container rm htmltortfcontainer docker image rm htmltortfimage ``` -------------------------------- ### HTML Structure with CSS Source: https://github.com/sautinsoft/sautinsoft.htmltortf.examples/blob/master/VB.Net/05. Properties and Settings/03. Set page size and margins/Sample.html This HTML snippet includes CSS for styling various text elements, which can be used as a basis for rich text document generation. ```html HTML to RTF Example ``` -------------------------------- ### VB.Net Sample Source: https://github.com/sautinsoft/sautinsoft.htmltortf.examples/blob/master/VB.Net/05. Properties and Settings/01. Add page header and footer/Sample.html This VB.Net code snippet shows how to use the HTML to RTF converter to add a header and footer to a document. It includes setting the header and footer text, and converting an HTML string to RTF. ```VB.Net Imports System Imports System.IO Imports SautinSoft.HtmlToRtf Namespace HtmlToRtfExamples Friend Class Sample ' Sample creates a new document and adds a header and footer. ' 1. Initialize the converter. ' 2. Set the header and footer text. ' 3. Convert HTML to RTF. ' 4. Save the RTF document. Shared Sub Main(args As String()) Dim converter As New HtmlToRtf() converter.WorkingDirectory = "c:\temp\" ' Set header and footer text. converter.Header.Text = "My Header Text" converter.Footer.Text = "My Footer Text" ' Convert HTML to RTF. Dim rtf As String = converter.Convert("

Hello World!

This is a sample document with header and footer.

") ' Save the RTF document. File.WriteAllText("C:\Users\Public\Documents\header_footer.rtf", rtf) End Sub End Class End Namespace ``` -------------------------------- ### Add page numbers Source: https://github.com/sautinsoft/sautinsoft.htmltortf.examples/blob/master/CSharp/05. Properties and Settings/02. Add page numbers/Sample.html This C# code snippet shows how to use the HtmlToRtf class to convert an HTML string to RTF format and add page numbers. ```csharp using System; using System.IO; using SautinSoft.HtmlToRtf.Rtf.Constants; namespace SautinSoft { class Program { static void Main(string[] args) { // Set the license key. HtmlToRtf.SetLicenseKey("1234567890"); // HTML content with basic styling. string sFileIn = "\n\n\n\n\n

Sample Paragraph 1

\n

Sample Paragraph 2

\n\n"; // Create an instance of HtmlToRtf. HtmlToRtf converter = new HtmlToRtf(); // Convert HTML to RTF and add page numbers. // The 'true' parameter indicates that page numbers should be added. converter.ConvertHtmlToRtfFile(sFileIn, "output.rtf", eDocumentType.rtf, true); Console.WriteLine("Successfully converted HTML to RTF with page numbers."); } } } ``` -------------------------------- ### Convert HTML to DOCX Stream Source: https://github.com/sautinsoft/sautinsoft.htmltortf.examples/blob/master/VB.Net/01. HTML to DOCX/03. Convert HTML to DOCX Stream/utf-8.html This VB.Net code snippet shows how to convert an HTML file to a DOCX stream. ```VB.Net Imports System.IO Imports Sautinsoft.HtmlToRtf Public Class Class1 Public Shared Sub ConvertHtmlToDocxStream() ' Specify the path to your HTML file Dim htmlFilePath As String = "your_html_file.html" ' Create a MemoryStream to hold the DOCX content Using ms As New MemoryStream() ' Create an instance of HtmlToDocx Dim htmldocx As New HtmlToDocx() ' Convert the HTML file to DOCX and save it to the MemoryStream htmldocx.Convert(htmlFilePath, ms, "utf-8") ' At this point, the DOCX content is in the MemoryStream (ms) ' You can now save the stream to a file, send it over a network, etc. ' For example, to save it to a file: Dim outputDocxPath As String = "output_stream.docx" File.WriteAllBytes(outputDocxPath, ms.ToArray()) Console.WriteLine($"Successfully converted '{htmlFilePath}' to DOCX stream and saved to '{outputDocxPath}'") End Using End Sub End Class ```