SautinSoft.Document поможет Вашему приложению преобразовать документ из одного формата в другой.
Преобразовать DOCX в PDF:
DocumentCore dc = DocumentCore.Load(@"d:\Before.docx");
dc.Save(@"d:\After.pdf");
byte[] docxBytes = ....; // Say, get DOCX bytes from DB or another Data resource.
byte[] pdfBytes = null;
using (MemoryStream msDocx = new MemoryStream(docxBytes))
{
DocumentCore dc = DocumentCore.Load(msDocx, new DocxLoadOptions());
using (MemoryStream msPdf = new MemoryStream())
{
dc.Save(msPdf, new PdfSaveOptions());
pdfBytes = msPdf.ToArray();
}
}
Преобразовать DOCX в PDF (с опциями):
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 DOCX to PDF (file to file).
/// </summary>
/// <remarks>
/// Details: https://sautinsoft.com/products/document/help/net/developer-guide/convert-docx-to-pdf-in-csharp-vb.php
/// </remarks>
static void ConvertFromFile()
{
string inpFile = @"..\..\..\example.docx";
string outFile = @"Result.pdf";
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 DOCX to PDF (using Stream).
/// </summary>
/// <remarks>
/// Details: https://sautinsoft.com/products/document/help/net/developer-guide/convert-docx-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.docx";
string outFile = @"ResultStream.pdf";
byte[] inpData = File.ReadAllBytes(inpFile);
byte[] outData = null;
using (MemoryStream msInp = new MemoryStream(inpData))
{
// Load a document.
DocumentCore dc = DocumentCore.Load(msInp, new DocxLoadOptions());
// 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);
// Important for Linux: Install MS Fonts
// sudo apt install ttf-mscorefonts-installer -y
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 DOCX to PDF (file to file).
''' </summary>
''' <remarks>
''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/convert-docx-to-pdf-in-csharp-vb.php
''' </remarks>
Sub ConvertFromFile()
Dim inpFile As String = "..\..\..\example.docx"
Dim outFile As String = "Result.pdf"
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 DOCX to PDF (using Stream).
''' </summary>
''' <remarks>
''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/convert-docx-to-pdf-in-csharp-vb.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.docx"
Dim outFile As String = "ResultStream.pdf"
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 DocxLoadOptions())
' 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 Module
Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу [email protected] или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже: