горизонтально расположенные полосы: белая, синяя, красная

Как конвертировать HTML в Text на C# и VB.NET

Преобразование HTML в текстовый файл

using System;
using System.IO;
namespace Sample
{
  class Test
  {
  static void Main(string[] args)
  {
    // Convert HTML file to Text file.
    // If you need more information about "HTML to RTF .Net"
    // Email us at: [email protected].
    ConvertHtmlToTextFile();
  }
  public static void ConvertHtmlToTextFile()
  {
    SautinSoft.HtmlToRtf h = new SautinSoft.HtmlToRtf();
    // After purchasing the license, please insert your serial number here to activate the component.
    // h.Serial = "XXXXXXXXX";

    string inputFile = @"..\..\sample.html";
    string outputFile = Path.ChangeExtension(inputFile, ".txt");
    if (h.OpenHtml(inputFile))
    {
      bool ok = h.ToText(outputFile);
      // Open the result for demonstration purposes.
      if (ok)
        System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outputFile) { UseShellExecute = true });
      }
    }
  }
}
Imports System
Imports System.IO
Imports System.Text
Module Module1
  Sub Main()
    ' Convert HTML file to Text file.
    ' If you need more information about "HTML to RTF .Net"
    ' Email us at: [email protected].
    ConvertHtmlToTextFile()
  End Sub

  Public Sub ConvertHtmlToTextFile()
    Dim h As New SautinSoft.HtmlToRtf()

    ' After purchasing the license, please insert your serial number here to activate the component.
    'h.Serial = "XXXXXXXXX"

    Dim inputFile As String = "..\sample.html"
    Dim outputFile As String = Path.ChangeExtension(inputFile, ".txt")
    If h.OpenHtml(inputFile) Then
      Dim ok As Boolean = h.ToText(outputFile)
      ' Open the result for demonstration purposes.
      If ok Then
        System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outputFile) With {.UseShellExecute = True})
      End If
    End If
  End Sub
End Module

Преобразование HTML в текстовый поток

using System;
using System.IO;
namespace Sample
{
  class Test
  {
    static void Main(string[] args)
    {
      // Convert HTML Stream to Text Stream.
      // If you need more information about "HTML to RTF .Net"
      // Email us at: [email protected].
      ConvertHtmlToTextStream();
    }
    public static void ConvertHtmlToTextStream()
    {
      SautinSoft.HtmlToRtf h = new SautinSoft.HtmlToRtf();
      // After purchasing the license, please insert your serial number here to activate the component.
      // h.Serial = "XXXXXXXXX";

      string inputFile = @"..\..\sample.html";
      string outputFile = Path.ChangeExtension(inputFile, ".txt");
      using (FileStream htmlFileStrem = new FileStream(inputFile, FileMode.Open))
      {
        if (h.OpenHtml(htmlFileStrem))
        {
          using (MemoryStream ms = new MemoryStream())
          {
            bool ok = h.ToText(ms);
            // Open the result for demonstration purposes.
            if (ok)
            {
              File.WriteAllBytes(outputFile, ms.ToArray());
              System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outputFile) { UseShellExecute = true });
            }
          }
        }
      }
    }
  }
}
Imports System
Imports System.IO
Imports System.Text
Module Module1
  Sub Main()
    ' Convert HTML Stream to Text Stream.
    ' If you need more information about "HTML to RTF .Net"
    ' Email us at: [email protected].
    ConvertHtmlToTextStream()
  End Sub

  Public Sub ConvertHtmlToTextStream()
    Dim h As New SautinSoft.HtmlToRtf()

    ' After purchasing the license, please insert your serial number here to activate the component.
    'h.Serial = "XXXXXXXXX"

    Dim inputFile As String = "..\sample.html"
    Dim outputFile As String = Path.ChangeExtension(inputFile, ".txt")

    Using htmlFileStrem As New FileStream(inputFile, FileMode.Open)
      If h.OpenHtml(htmlFileStrem) Then
        Using ms As New MemoryStream()
          Dim ok As Boolean = h.ToText(ms)

          ' Open the result for demonstration purposes.
          If ok Then
            File.WriteAllBytes(outputFile, ms.ToArray())
            System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outputFile) With {.UseShellExecute = True})
          End If
        End Using
      End If
    End Using
  End Sub
End Module

Преобразование HTML в текстовую строку

using System;
using System.IO;
namespace Sample
{
  class Test
  {
    static void Main(string[] args)
    {
      // Convert HTML string to Text string.
      // If you need more information about "HTML to RTF .Net"
      // Email us at: [email protected].
      ConvertHtmlToTextString();
    }
    public static void ConvertHtmlToTextString()
    {
      SautinSoft.HtmlToRtf h = new SautinSoft.HtmlToRtf();
      // After purchasing the license, please insert your serial number here to activate the component.
      // h.Serial = "XXXXXXXXX";

      string inputFile = @"..\..\sample.html";
      string outputFile = Path.ChangeExtension(inputFile, ".txt");

      // Read our HTML file a string.
      string htmlString = File.ReadAllText(inputFile);
      if (h.OpenHtml(htmlString))
      {
        string textString = h.ToText();
        // Open the result for demonstration purposes.
        if (!String.IsNullOrEmpty(textString))
        {
          File.WriteAllText(outputFile, textString);
          System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outputFile) { UseShellExecute = true });
        }
      }
    }
  }
}
Imports System
Imports System.IO
Imports System.Text
Module Module1
  Sub Main()
    ' Convert HTML string to Text string.
    ' If you need more information about "HTML to RTF .Net"
    ' Email us at: [email protected].
    ConvertHtmlToTextString()
  End Sub

  Public Sub ConvertHtmlToTextString()
    Dim h As New SautinSoft.HtmlToRtf()

    ' After purchasing the license, please insert your serial number here to activate the component.
    'h.Serial = "XXXXXXXXX"

    Dim inputFile As String = "..\sample.html"
    Dim outputFile As String = Path.ChangeExtension(inputFile, ".txt")

    ' Read our HTML file a string.
    Dim htmlString As String = File.ReadAllText(inputFile)
    If h.OpenHtml(htmlString) Then
      Dim textString As String = h.ToText()

      ' Open the result for demonstration purposes.
      If Not String.IsNullOrEmpty(textString) Then
        File.WriteAllText(outputFile, textString)
        System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outputFile) With {.UseShellExecute = True})
      End If
    End If
  End Sub
End Module

Преобразование множества HTML-файлов в текстовые файлы

using System;
using System.IO;
namespace Sample
{
  class Test
  {
    static void Main(string[] args)
    {
      // Convert multiple HTML to Text files.
      // If you need more information about "HTML to RTF .Net"
      // Email us at: [email protected].
      ConvertMultipleHtmlToText();
    }
    public static void ConvertMultipleHtmlToText()
    {
      SautinSoft.HtmlToRtf h = new SautinSoft.HtmlToRtf();
      // After purchasing the license, please insert your serial number here to activate the component.
      // h.Serial = "XXXXXXXXX";

      string inpFolder = @"..\..\Testing HTMLs\";
      string outFolder = new DirectoryInfo(Directory.GetCurrentDirectory()).CreateSubdirectory("Text").FullName;
      string[] inpFiles = Directory.GetFiles(inpFolder, "*.htm*");

      int total = inpFiles.Length;
      int currCount = 1;
      int successCount = 0;

      foreach (string inpFile in inpFiles)
      {
        string fileName = Path.GetFileName(inpFile);
        Console.Write("{0:D2} of {1} ... {2}", currCount, total, fileName);
        currCount++;
        bool ok = true;
        if (h.OpenHtml(inpFile))
        {
          string outFile = Path.Combine(outFolder, Path.ChangeExtension(fileName, ".txt"));
          if (h.ToText(outFile))
            successCount++;
          else
            ok = false;
        }
        else
          ok = false;
          Console.WriteLine(" ({0})",ok);
      }
      Console.WriteLine("{0} of {1} HTML(s) converted successfully!", successCount, total);
      Console.WriteLine("Press any key ...");
      Console.ReadKey();

      // Open the result for demonstration purposes.
      System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFolder) { UseShellExecute = true });
    }
  }
}
Imports System
Imports System.IO
Imports System.Text
Module Module1
  Sub Main(ByVal args() As String)
    ' Convert multiple HTML to Text files.
    ' If you need more information about "HTML to RTF .Net"
    ' Email us at: [email protected].
    ConvertMultipleHtmlToText()
  End Sub

  Public Sub ConvertMultipleHtmlToText()
    Dim h As New SautinSoft.HtmlToRtf()

    ' After purchasing the license, please insert your serial number here to activate the component.
    ' h.Serial = "XXXXXXXXX"

    Dim inpFolder As String = "..\Testing HTMLs\"
    Dim outFolder As String = (New DirectoryInfo(Directory.GetCurrentDirectory())).CreateSubdirectory("Text").FullName
    Dim inpFiles() As String = Directory.GetFiles(inpFolder, "*.htm*")

    Dim total As Integer = inpFiles.Length
    Dim currCount As Integer = 1
    Dim successCount As Integer = 0

    For Each inpFile As String In inpFiles
      Dim fileName As String = Path.GetFileName(inpFile)
      Console.Write("{0:D2} of {1} ... {2}", currCount, total, fileName)
      currCount += 1

      Dim ok As Boolean = True
      If h.OpenHtml(inpFile) Then
        Dim outFile As String = Path.Combine(outFolder, Path.ChangeExtension(fileName, ".txt"))
        If h.ToText(outFile) Then
          successCount += 1
        Else
          ok = False
        End If
      Else
        ok = False
      End If

      Console.WriteLine(" ({0})", ok)
      Next inpFile
      Console.WriteLine("{0} of {1} HTML(s) converted successfully!", successCount, total)
      Console.WriteLine("Press any key ...")
      Console.ReadKey()

      ' Open the result for demonstration purposes.
      System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outFolder) With {.UseShellExecute = True})
  End Sub
End Module

Другие примеры кода SautinSoft.HtmlToRtf

HTML в RTF HTML в DOCX ✦ HTML в Text Слияние/Замена RTF Настройки
 ВВЕРХ