SautinSoft.Document может помочь Вашему приложению преобразовать документ из одного формата в
другой.
Вам нужно будет только Load() документ и
Save() в нужном формате:
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 |
Convert DOCX to PDF:
DocumentCore dc = DocumentCore.Load(@"d:\Before.docx");
dc.Save(@"d:\After.pdf");
Convert PDF to DOCX:
DocumentCore dc = DocumentCore.Load(@"d:\Before.pdf");
dc.Save(@"d:\After.docx");
Convert RTF to HTML:
DocumentCore dc = DocumentCore.Load(@"d:\Before.rtf");
dc.Save(@"d:\After.html");
Convert DOCX to RTF (in memory):
byte[] rtfBytes = ....; // Say, get RTF bytes from DB.
byte[] docxBytes = null;
using (MemoryStream msRtf = new MemoryStream(rtfBytes))
{
DocumentCore dc = DocumentCore.Load(msRtf, new RtfLoadOptions());
using (MemoryStream msDocx = new MemoryStream())
{
dc.Save(msDocx, new DocxSaveOptions());
docxBytes = msDocx.ToArray();
}
}
Convert PDF to HTML (with options):
DocumentCore dc = DocumentCore.Load(@"d:\Before.pdf",
new PdfLoadOptions()
{DetectTables=true,
ConversionMode = PdfConversionMode.Continuous,
PageIndex=0,
PageCount=1});
dc.Save(@"d:\After.html", new HtmlFixedSaveOptions()
{Version = HtmlVersion.Html5,
CssExportMode = CssExportMode.Inline,
EmbedImages = true});
Кроме того, во время цикла преобразования Вы можете заменить любой элемент в документе или добавить что-то новое: цифровую подпись, водяной знак, нумерацию страниц и так далее.
Полный код
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 PDF to DOCX (file to file).
/// </summary>
/// <remarks>
/// Details: https://sautinsoft.com/products/document/help/net/developer-guide/convert-document.php
/// </remarks>
static void ConvertFromFile()
{
string inpFile = @"..\..\..\example.pdf";
string outFile = @"Result.docx";
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 PDF to HTML (using Stream).
/// </summary>
/// <remarks>
/// Details: https://sautinsoft.com/products/document/help/net/developer-guide/convert-document.php
/// </remarks>
static void ConvertFromStream()
{
// We need files only for demonstration purposes.
// The conversion process will be done completely in memory.
string inpFile = @"..\..\..\example.pdf";
string outFile = @"Result.html";
byte[] inpData = File.ReadAllBytes(inpFile);
byte[] outData = null;
using (MemoryStream msInp = new MemoryStream(inpData))
{
// Load a document.
DocumentCore dc = DocumentCore.Load(msInp, new PdfLoadOptions()
{
PreserveGraphics = true,
DetectTables = true
});
// Save the document to HTML-fixed format.
using (MemoryStream outMs = new MemoryStream())
{
dc.Save(outMs, new HtmlFixedSaveOptions()
{
CssExportMode = CssExportMode.Inline,
EmbedImages = true
});
outData = outMs.ToArray();
}
// 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
Imports System.IO
Imports SautinSoft.Document
Module Sample
Sub Main()
ConvertFromFile()
ConvertFromStream()
End Sub
''' Get your free 100-day key here:
''' https://sautinsoft.com/start-for-free/
''' <summary>
''' Convert PDF to DOCX (file to file).
''' </summary>
''' <remarks>
''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/convert-document.php
''' </remarks>
Sub ConvertFromFile()
Dim inpFile As String = "..\..\..\example.pdf"
Dim outFile As String = "Result.docx"
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 PDF to HTML (using Stream).
''' </summary>
''' <remarks>
''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/convert-document.php
''' </remarks>
Sub ConvertFromStream()
' We need files only for demonstration purposes.
' The conversion process will be done completely in memory.
Dim inpFile As String = "..\..\..\example.pdf"
Dim outFile As String = "Result.html"
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 PdfLoadOptions() With {
.PreserveGraphics = True,
.DetectTables = True
})
' Save the document to HTML-fixed format.
Using outMs As New MemoryStream()
dc.Save(outMs, New HtmlFixedSaveOptions() With {
.CssExportMode = CssExportMode.Inline,
.EmbedImages = True
})
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 Module
Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу [email protected] или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже: