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

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

Конвертирование HTML-файла в RTF-файл

using System;
using System.IO;
namespace Sample
{
  class Test
  {
    static void Main(string[] args)
    {
      // Convert HTML file to RTF file.
      // If you need more information about "HTML to RTF .Net"
      // Email us at: [email protected]
      ConvertHtmlToRtfFile();
    }
    public static void ConvertHtmlToRtfFile()
    {
      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, ".rtf");
      if (h.OpenHtml(inputFile))
      {
        bool ok = h.ToRtf(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 RTF file.
    ' If you need more information about "HTML to RTF .Net"
    ' Email us at: [email protected]
    ConvertHtmlToRtfFile()
  End Sub

  Public Sub ConvertHtmlToRtfFile()
    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, ".rtf")
    If h.OpenHtml(inputFile) Then
      Dim ok As Boolean = h.ToRtf(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 в поток RTF

using System;
using System.IO;
namespace Sample
{
  class Test
  {
    static void Main(string[] args)
    {
      // Convert HTML Stream to RTF Stream.
      // If you need more information about "HTML to RTF .Net"
      // Email us at: [email protected]
      ConvertHtmlToRtfStream();
    }
    public static void ConvertHtmlToRtfStream()
    {
      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 = @"..\..\utf-8.html";
      string outputFile = Path.ChangeExtension(inputFile, ".rtf");

      // Specify the 'BaseURL' property that component can find the full path to images, like a: <img src="..\pict.png"> and
      // to external css, like a: <link rel="stylesheet" href="/css/style.css">.
      h.BaseURL = Path.GetFullPath(inputFile);
      using (FileStream htmlFileStrem = new FileStream(inputFile, FileMode.Open))
      {
        if (h.OpenHtml(htmlFileStrem))
        {
          using (MemoryStream rtfMemoryStream = new MemoryStream())
          {
            bool ok = h.ToRtf(rtfMemoryStream);
            // Open the result for demonstration purposes.
            if (ok)
            {
              File.WriteAllBytes(outputFile, rtfMemoryStream.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 RTF Stream.
    ' If you need more information about "HTML to RTF .Net"
    ' Email us at: [email protected]
    ConvertHtmlToRtfStream()
  End Sub

  Public Sub ConvertHtmlToRtfStream()
    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 = "..\utf-8.html"
    Dim outputFile As String = Path.ChangeExtension(inputFile, ".rtf")

    ' Specify the 'BaseURL' property that component can find the full path to images, like a: <img src="..\pict.png"> and
    ' to external css, like a: <link rel="stylesheet" href="/css/style.css">.
    h.BaseURL = Path.GetFullPath(inputFile)
    Using htmlFileStrem As New FileStream(inputFile, FileMode.Open)
      If h.OpenHtml(htmlFileStrem) Then
        Using rtfMemoryStream As New MemoryStream()
          Dim ok As Boolean = h.ToRtf(rtfMemoryStream)
          ' Open the result for demonstration purposes.
          If ok Then
            File.WriteAllBytes(outputFile, rtfMemoryStream.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 в строку RTF

using System;
using System.IO;
namespace Sample
{
  class Test
  {
    static void Main(string[] args)
    {
      // Convert HTML string to RTF string.
      // If you need more information about "HTML to RTF .Net"
      // Email us at: [email protected]
      ConvertHtmlToRtfString();
    }
    public static void ConvertHtmlToRtfString()
    {
      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 = @"..\..\pic.html";
      string outputFile = Path.ChangeExtension(inputFile, ".rtf");

      // Read our HTML file a string.
      string htmlString = File.ReadAllText(inputFile);

      // Specify the 'BaseURL' property that component can find the full path to images, like a: <img src="..\pict.png"> and
      // to external css, like a: <link rel="stylesheet" href="/css/style.css">.
      h.BaseURL = Path.GetFullPath(inputFile);
      if (h.OpenHtml(htmlString))
      {
        string rtfString = h.ToRtf();
        // Open the result for demonstration purposes.
        if (!String.IsNullOrEmpty(rtfString))
        {
          File.WriteAllText(outputFile, rtfString);
          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 RTF string.
    ' If you need more information about "HTML to RTF .Net"
    ' Email us at: [email protected]
    ConvertHtmlToRtfString()
  End Sub

  Public Sub ConvertHtmlToRtfString()
    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 = "..\pic.html"
    Dim outputFile As String = Path.ChangeExtension(inputFile, ".rtf")

    ' Read our HTML file a string.
    Dim htmlString As String = File.ReadAllText(inputFile)

    ' Specify the 'BaseURL' property that component can find the full path to images, like a: <img src="..\pict.png"> and
    ' to external css, like a: <link rel="stylesheet" href="/css/style.css">.
    h.BaseURL = Path.GetFullPath(inputFile)
    If h.OpenHtml(htmlString) Then
      Dim rtfString As String = h.ToRtf()

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

Конвертирование URL HTML в RTF-файл

using System;
using System.IO;
namespace Sample
{
  class Test
  {
    static void Main(string[] args)
    {
      // Convert HTML url to RTF file.
      // If you need more information about "HTML to RTF .Net"
      // Email us at: [email protected]
      ConvertHtmlUrlToRtfFile();
    }
    public static void ConvertHtmlUrlToRtfFile()
    {
      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 = @"http://www.sautinsoft.net/samples/utf-8.html";
       string outputFile = "Result.rtf";

       // Specify the 'BaseURL' property that component can find the full path to images, like a: <img src="..\pict.png"> and
       // to external css, like a: <link rel="stylesheet" href="/css/style.css">.
       h.BaseURL = @"http://www.sautinsoft.net/samples/utf-8.html";
       if (h.OpenHtml(inputFile))
       {
         bool ok = h.ToRtf(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 url to RTF file.
    ' If you need more information about "HTML to RTF .Net"
    ' Email us at: [email protected]
    ConvertHtmlUrlToRtfFile()
  End Sub

  Public Sub ConvertHtmlUrlToRtfFile()
    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 = "http://www.sautinsoft.net/samples/utf-8.html"
    Dim outputFile As String = "Result.rtf"

    ' Specify the 'BaseURL' property that component can find the full path to images, like a: <img src="..\pict.png"> and
    ' to external css, like a: <link rel="stylesheet" href="/css/style.css">.
    h.BaseURL = "http://www.sautinsoft.net/samples/utf-8.html"
    If h.OpenHtml(inputFile) Then
      Dim ok As Boolean = h.ToRtf(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 в RTF-файлы

using System;
using System.IO;
namespace Sample
{
  class Test
  {
    static void Main(string[] args)
    {
      // Convert multiple HTML to RTF files.
      // If you need more information about "HTML to RTF .Net"
      // Email us at: [email protected]
      ConvertMultipleHtmlToRtf();
    }
    public static void ConvertMultipleHtmlToRtf()
    {
      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("RTF").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, ".rtf"));
          if (h.ToRtf(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 RTF files.
    ' If you need more information about "HTML to RTF .Net"
    ' Email us at: [email protected]
    ConvertMultipleHtmlToRtf()
  End Sub

  Public Sub ConvertMultipleHtmlToRtf()
    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("RTF").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, ".rtf"))
        If h.ToRtf(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 Настройки
 ВВЕРХ