The package is in really active development, and I've already experienced some breaking changes, but it seems to be a very professional package.
The code below just scratches the surface of what it can do (tested with version 0.57.1 of the package).
Of course you'll have to change the hard-coded file paths to suit your system.
This code creates a docx from an HTML string (no Word required) and also creates a PDF from that docx. Then the documents created are saved to your file system. That alone makes it pretty useful for me!
Note this code:
What you get at first is byte arrays, and you have to use C#'s File.WriteAllBytesAsync to write the 'byte[]'s to files.
The code below just scratches the surface of what it can do (tested with version 0.57.1 of the package).
Of course you'll have to change the hard-coded file paths to suit your system.
This code creates a docx from an HTML string (no Word required) and also creates a PDF from that docx. Then the documents created are saved to your file system. That alone makes it pretty useful for me!
Note this code:
await File.WriteAllBytesAsync(landscapePath, docx);
await File.WriteAllBytesAsync(landscapePDF, pdf);
What you get at first is byte arrays, and you have to use C#'s File.WriteAllBytesAsync to write the 'byte[]'s to files.
/*/ nuget DocToolKit\ank.doctoolkit; /*/
using DocToolkit;
const string Html = "<h1>Invoice</h1><p>Total: 18,100.00</p>";
print.it("HTML conversion");
print.it("===============");
#region convert and save
// byte[] docx = await HtmlToDocxConverter.ConvertAsync("<h1>Invoice</h1><p>Total: 18,100.00</p>", PageSetup.Letter);
byte[] docx = await HtmlToDocxConverter.ConvertAsync(Html, PageSetup.Letter);
byte[] pdf = await HtmlToPdfConverter.ConvertAsync(Html);
byte[] rendered = DocxToPdfConverter.Convert(docx);
string docxPath = @"D:\Projects_D\doctoolkit tests\firstout.docx";
string pdfPath = @"D:\Projects_D\doctoolkit tests\firstout.pdf";
string renderedPath = @"D:\Projects_D\doctoolkit tests\secondout.pdf";
await File.WriteAllBytesAsync(docxPath, docx);
await File.WriteAllBytesAsync(pdfPath, pdf);
await File.WriteAllBytesAsync(renderedPath, pdf);
#endregion
#region
byte[] report = DocxEditor.Create(
new[]
{
DocxBlock.Heading("Quarterly report", 1),
DocxBlock.Paragraph("Revenue by region, in thousands."),
DocxBlock.Table(
new[] { "Region", "Q1", "Q2" },
new[]
{
#nullable enable
new object?[] { "EMEA", 1200, 1310 },
new object?[] { "APAC", 980, 1040 },
}),
},
PageSetup.Letter.WithMargins(54));
string blockPath = @"D:\Projects_D\doctoolkit tests\block_edit.docx";
await File.WriteAllBytesAsync(blockPath, report);
#endregion
print.it($"\nHTML -> DOCX : {docx.Length,7:N0} bytes");
print.it($"HTML -> PDF : {pdf.Length,7:N0} bytes (pivots through DOCX internally)");
print.it($"DOCX -> PDF : {rendered.Length,7:N0} bytes (from the DOCX above)");
// --- Page setup ----------------------------------------------------------------------------
// Every producer lays out on A4 unless told otherwise. PageSetup is immutable: Landscape() and
// WithMargins() return a NEW instance rather than mutating the shared PageSetup.Letter - which is
// what makes those static properties safe to hand around a running application.
#region page-setup
DocToolkit.PageSetup wide = PageSetup.Letter.Landscape().WithMargins(36);
byte[] landscape = await HtmlToDocxConverter.ConvertAsync(Html, wide);
byte[] landscapePdf = await HtmlToPdfConverter.ConvertAsync(Html, wide);
string landscapePath = @"D:\Projects_D\doctoolkit tests\landscape_out.docx";
string landscapePDF = @"D:\Projects_D\doctoolkit tests\landscape_pdf_out.pdf";
await File.WriteAllBytesAsync(landscapePath, docx);
await File.WriteAllBytesAsync(landscapePDF, pdf);
#endregion
print.it($"\nDefault : {PageSetup.Letter}");
print.it($"This one : {wide}");
print.it($"Landscape : {landscape.Length,7:N0} bytes DOCX, {landscapePdf.Length:N0} bytes PDF");
print.it($"Letter intact: {PageSetup.Letter.WidthPoints < PageSetup.Letter.HeightPoints} (Landscape() did not mutate it)");
print.it("\nDone.");
