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

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

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

using System;
using System.IO;
namespace Sample
{
  class Test
  {
    static void Main(string[] args)
    {
      // Convert HTML bytes to DOCX bytes.
      // If you need more information about "HTML to RTF .Net"
      // Email us at: [email protected].
      ConvertHtmlToDocxBytes();
    }
    public static void ConvertHtmlToDocxBytes()
    {
      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, ".docx");

      // Read our HTML file a bytes.
      byte[] htmlBytes = File.ReadAllBytes(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(htmlBytes))
      {
        byte[] docxBytes = h.ToDocx();
        // Open the result for demonstration purposes.
        if (docxBytes != null)
        {
          File.WriteAllBytes(outputFile, docxBytes);
          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 bytes to DOCX bytes.
    ' If you need more information about "HTML to RTF .Net"
    ' Email us at: [email protected].
    ConvertHtmlToDocxBytes()
    End Sub

    Public Sub ConvertHtmlToDocxBytes()
    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, ".docx")

    ' Read our HTML file a bytes.
    Dim htmlBytes() As Byte = File.ReadAllBytes(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(htmlBytes) Then
      Dim docxBytes() As Byte = h.ToDocx()

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

Преобразование HTML в файл DOCX

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

  Public Sub ConvertHtmlToDocxFile()
    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, ".docx")
    If h.OpenHtml(inputFile) Then
      Dim ok As Boolean = h.ToDocx(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 в поток DOCX

using System;
using System.IO;
namespace Sample
{
  class Test
  {
    static void Main(string[] args)
    {
      // Convert HTML Stream to DOCX Stream.
      // If you need more information about "HTML to RTF .Net"
      // Email us at: [email protected].
      ConvertHtmlToDocxStream();
    }
    public static void ConvertHtmlToDocxStream()
    {
      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, ".docx");

      // 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 docxMemoryStream = new MemoryStream())
          {
            bool ok = h.ToDocx(docxMemoryStream);
            // Open the result for demonstration purposes.
            if (ok)
            {
              File.WriteAllBytes(outputFile, docxMemoryStream.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 DOCX Stream.
    ' If you need more information about "HTML to RTF .Net"
    ' Email us at: [email protected].
    ConvertHtmlToDocxStream()
  End Sub

  Public Sub ConvertHtmlToDocxStream()
    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, ".docx")
    ' 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 docxMemoryStream As New MemoryStream()
          Dim ok As Boolean = h.ToDocx(docxMemoryStream)

          ' Open the result for demonstration purposes.
          If ok Then
            File.WriteAllBytes(outputFile, docxMemoryStream.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

Преобразование URL HTML в файл DOCX

using System;
using System.IO;
namespace Sample
{
  class Test
  {
    static void Main(string[] args)
    {
      // Convert HTML url to DOCX file.
      // If you need more information about "HTML to RTF .Net"
      // Email us at: [email protected].
      ConvertHtmlUrlToDocxFile();
    }
    public static void ConvertHtmlUrlToDocxFile()
    {
      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.docx";

      // 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.ToDocx(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 DOCX file.
    ' If you need more information about "HTML to RTF .Net"
    ' Email us at: [email protected].
    ConvertHtmlUrlToDocxFile()
  End Sub

  Public Sub ConvertHtmlUrlToDocxFile()
    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.docx"

    ' 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.ToDocx(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-файлов в DOCX-файлы

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

  Public Sub ConvertMultipleHtmlToDocx()
    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("DOCX").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, ".docx"))
        If h.ToDocx(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 Настройки
 ВВЕРХ