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("...");
dc.Save("....");
Document .Net supports these formats:
DOCX | RTF | HTML | Text | Image | |
---|---|---|---|---|---|
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)
{
ConvertFromFile();
ConvertFromStream();
}
/// <summary>
/// Convert HTML to PDF (file to file).
/// </summary>
/// <remarks>
/// Details: https://sautinsoft.com/products/document/help/net/developer-guide/convert-html-to-pdf-in-csharp-vb.php
/// </remarks>
static void ConvertFromFile()
{
string inpFile = @"..\..\example.html";
string outFile = @"result from file.pdf";
// Preparing new HtmlLoadOptions with some parameters
HtmlLoadOptions lo = new HtmlLoadOptions()
{
PageSetup = new PageSetup()
{
Orientation = Orientation.Landscape,
PageMargins = new PageMargins()
{
Left = 10,
Right = 20
}
},
DefaultFontFamily = "Verdana",
DefaultFontSize = 10
};
DocumentCore dc = DocumentCore.Load(inpFile, lo);
dc.Save(outFile, new PdfSaveOptions { });
// Open the result for demonstration purposes.
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true });
}
/// <summary>
/// Convert HTML to PDF (using Stream).
/// </summary>
/// <remarks>
/// Details: https://sautinsoft.com/products/document/help/net/developer-guide/convert-html-to-pdf-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.html";
string outFile = @"result from stream.pdf";
byte[] inpData = File.ReadAllBytes(inpFile);
byte[] outData = null;
using (MemoryStream msInp = new MemoryStream(inpData))
{
// Preparing new HtmlLoadOptions with some parameters
HtmlLoadOptions lo = new HtmlLoadOptions()
{
PageSetup = new PageSetup()
{
Orientation = Orientation.Landscape,
PageMargins = new PageMargins()
{
Left = 10,
Right = 20
}
},
DefaultFontFamily = "Verdana",
DefaultFontSize = 10
};
// Load a document.
DocumentCore dc = DocumentCore.Load(msInp, lo);
// Save the document to PDF format.
using (MemoryStream outMs = new MemoryStream())
{
dc.Save(outMs, new PdfSaveOptions());
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.IO
Imports SautinSoft.Document
Namespace Example
Friend Class Program
Shared Sub Main(ByVal args() As String)
ConvertFromFile()
ConvertFromStream()
End Sub
''' <summary>
''' Convert HTML to PDF (file to file).
''' </summary>
''' <remarks>
''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/convert-html-to-pdf-in-csharp-vb.php
''' </remarks>
Private Shared Sub ConvertFromFile()
Dim inpFile As String = "..\example.html"
Dim outFile As String = "result from file.pdf"
' Preparing new HtmlLoadOptions with some parameters
Dim lo As New HtmlLoadOptions() With {
.PageSetup = New PageSetup() With {
.Orientation = Orientation.Landscape,
.PageMargins = New PageMargins() With {
.Left = 10,
.Right = 20
}
},
.DefaultFontFamily = "Verdana",
.DefaultFontSize = 10
}
Dim dc As DocumentCore = DocumentCore.Load(inpFile, lo)
dc.Save(outFile, New PdfSaveOptions)
' Open the result for demonstration purposes.
System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outFile) With {.UseShellExecute = True})
End Sub
''' <summary>
''' Convert HTML to PDF (using Stream).
''' </summary>
''' <remarks>
''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/convert-html-to-pdf-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.html"
Dim outFile As String = "result from stream.pdf"
Dim inpData() As Byte = File.ReadAllBytes(inpFile)
Dim outData() As Byte = Nothing
Using msInp As New MemoryStream(inpData)
' Preparing new HtmlLoadOptions with some parameters
Dim lo As New HtmlLoadOptions() With {
.PageSetup = New PageSetup() With {
.Orientation = Orientation.Landscape,
.PageMargins = New PageMargins() With {
.Left = 10,
.Right = 20
}
},
.DefaultFontFamily = "Verdana",
.DefaultFontSize = 10
}
' Load a document.
Dim dc As DocumentCore = DocumentCore.Load(msInp, lo)
' Save the document to PDF format.
Using outMs As New MemoryStream()
dc.Save(outMs, New PdfSaveOptions())
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
If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below: