Convert a document to another format within C#

Document .Net can help your application to convert a document from a one format to another.
You'll need only to Load a document and Save to a desired format:

            DocumentCore dc = DocumentCore.Load("...");

Document .Net supports these formats:

Create/Read/Write Create/Read/Write Create/Read/Write Create/Read/Write

Conversion examples:

  1. Convert DOCX to PDF:

                DocumentCore dc = DocumentCore.Load(@"d:\Before.docx");
  2. Convert PDF to DOCX:

                DocumentCore dc = DocumentCore.Load(@"d:\Before.pdf");
  3. Convert RTF to HTML:

                DocumentCore dc = DocumentCore.Load(@"d:\Before.rtf");
  4. 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();
  5. Convert PDF to HTML (with options):

                DocumentCore dc = DocumentCore.Load(@"d:\Before.pdf", 
                    new PdfLoadOptions()
                    ConversionMode = PdfConversionMode.Continuous,
                dc.Save(@"d:\After.html", new HtmlFixedSaveOptions()
                {Version = HtmlVersion.Html5,
                CssExportMode = CssExportMode.Inline,
                EmbedImages = true});

Furthermore, during the conversion cycle you may replace any element in a document or add something new: digital signature, a watermark, page numbering etc.


Complete code

using System.IO;
using SautinSoft.Document;

namespace Example
    class Program
        static void Main(string[] args)

        static void ConvertFromFile()
            string inpFile = @"..\..\example.pdf";
            string outFile = Path.ChangeExtension(inpFile, "-Result.docx");

            DocumentCore dc = DocumentCore.Load(inpFile);

            // Open the result for demonstation purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true });

        static void ConvertFromStream()

            // We need files only for demonstration purposes.
            // The conversion process will be done completely in memory.
            byte[] inpData = File.ReadAllBytes(@"..\..\example.pdf");
            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 another format.
                using (MemoryStream outMs = new MemoryStream())
                    dc.Save(outMs, new HtmlFixedSaveOptions()
                        CssExportMode = CssExportMode.Inline,
                        EmbedImages = true
                    outData = outMs.ToArray();


            Imports System
Imports System.IO
Imports SautinSoft.Document

Module ExampleVB
    Sub Main()
    End Sub

    Public Sub ConvertFromFile()
        Dim inpFile As String = "..\example.pdf"
        Dim outFile As String = Path.ChangeExtension(inpFile, ".docx")

        Dim dc As DocumentCore = DocumentCore.Load(inpFile)

        ' Open the result for demonstation purposes.
        System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outFile) With {.UseShellExecute = True})

    End Sub

    Public Sub ConvertFromStream()

        ' We need files only for demonstration purposes.
        ' The conversion process will be done completely in memory.
        Dim inpData() As Byte = File.ReadAllBytes("..\example.pdf")
        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 another format.
            Using outMs As New MemoryStream()
                dc.Save(outMs, New HtmlFixedSaveOptions() With {
                    .CssExportMode = CssExportMode.Inline,
                    .EmbedImages = True
                outData = outMs.ToArray()
            End Using
        End Using
    End Sub
End Module


© SautinSoft 2019