SautinSoft.Document может помочь Вашему приложению преобразовать документ из одного формата в
другой.
Вам нужно будет только Load() документ и
Save() в нужном формате:
Преобразование RTF в RTF-файл выглядит неразумным, но иногда это необходимо для восстановления документа и его внутренней структуры.
DocumentCore dc = DocumentCore.Load("...");
dc.Save("....");
SautinSoft.Document поддерживает форматы:
DOCX | RTF | HTML | Текст | Изображения | |
---|---|---|---|---|---|
Create/Read/Write | Create/Read/Write | Create/Read/Write | Create/Read/Write | Create/Read/Write | Create/Read(OCR)/Write |
Полный код
using System.IO;
using SautinSoft.Document;
namespace Example
{
class Program
{
static void Main(string[] args)
{
// Get your free 100-day key here:
// https://sautinsoft.com/start-for-free/
ConvertFromFile();
ConvertFromStream();
}
/// <summary>
/// Convert RTF to RTF (file to file).
/// </summary>
/// <remarks>
/// Details: https://sautinsoft.com/products/document/help/net/developer-guide/convert-rtf-to-rtf-in-csharp-vb.php
/// </remarks>
static void ConvertFromFile()
{
string inpFile = @"..\..\..\example.rtf";
string outFile = @"Result.rtf";
DocumentCore dc = DocumentCore.Load(inpFile);
dc.Save(outFile);
// Important for Linux: Install MS Fonts
// sudo apt install ttf-mscorefonts-installer -y
// Open the result for demonstration purposes.
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true });
}
/// <summary>
/// Convert RTF to RTF (using Stream).
/// </summary>
/// <remarks>
/// Details: https://sautinsoft.com/products/document/help/net/developer-guide/convert-rtf-to-rtf-in-csharp-vb.php
/// </remarks>
static void ConvertFromStream()
{
// We need files only for demonstration purposes.
// The conversion process will be done completely in memory.
string inpFile = @"..\..\..\example.rtf";
string outFile = @"ResultStream.rtf";
byte[] inpData = File.ReadAllBytes(inpFile);
byte[] outData = null;
using (MemoryStream msInp = new MemoryStream(inpData))
{
// Load a document.
DocumentCore dc = DocumentCore.Load(msInp, new RtfLoadOptions());
// Save the document to RTF format.
using (MemoryStream outMs = new MemoryStream())
{
dc.Save(outMs, new RtfSaveOptions() );
outData = outMs.ToArray();
// Important for Linux: Install MS Fonts
// sudo apt install ttf-mscorefonts-installer -y
}
// Show the result for demonstration purposes.
if (outData != null)
{
File.WriteAllBytes(outFile, outData);
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true });
}
}
}
}
}
Imports System.IO
Imports SautinSoft.Document
Namespace Example
Friend Class Program
Shared Sub Main(ByVal args() As String)
ConvertFromFile()
ConvertFromStream()
End Sub
''' Get your free 100-day key here:
''' https://sautinsoft.com/start-for-free/
''' <summary>
''' Convert RTF to RTF (file to file).
''' </summary>
''' <remarks>
''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/convert-rtf-to-rtf-in-csharp-vb.php
''' </remarks>
Private Shared Sub ConvertFromFile()
Dim inpFile As String = "..\..\..\example.rtf"
Dim outFile As String = "Result.rtf"
Dim dc As DocumentCore = DocumentCore.Load(inpFile)
dc.Save(outFile)
' Important for Linux: Install MS Fonts
' sudo apt install ttf-mscorefonts-installer -y
' Open the result for demonstration purposes.
System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outFile) With {.UseShellExecute = True})
End Sub
''' <summary>
''' Convert RTF to RTF (using Stream).
''' </summary>
''' <remarks>
''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/convert-rtf-to-rtf-in-csharp-vb.php
''' </remarks>
Private Shared Sub ConvertFromStream()
' We need files only for demonstration purposes.
' The conversion process will be done completely in memory.
Dim inpFile As String = "..\..\..\example.rtf"
Dim outFile As String = "ResultStream.rtf"
Dim inpData() As Byte = File.ReadAllBytes(inpFile)
Dim outData() As Byte = Nothing
Using msInp As New MemoryStream(inpData)
' Load a document.
Dim dc As DocumentCore = DocumentCore.Load(msInp, New RtfLoadOptions())
' Save the document to RTF format.
Using outMs As New MemoryStream()
dc.Save(outMs, New RtfSaveOptions())
outData = outMs.ToArray()
End Using
' Show the result for demonstration purposes.
If outData IsNot Nothing Then
File.WriteAllBytes(outFile, outData)
System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outFile) With {.UseShellExecute = True})
End If
End Using
End Sub
End Class
End Namespace
Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу support@sautinsoft.com или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже: