using System.Net; using OfficeIMO.Drawing; using OfficeIMO.Html; using OfficeIMO.Html.Pdf; using PdfCore = OfficeIMO.Pdf; namespace OfficeIMO.Examples.Html { internal static partial class Html { public static void Example_HtmlInvoiceShowcase(string folderPath, bool openPdf) { Console.WriteLine("[*] HTML purchase report: data-bound rows, repeated headers, PDF, PNG, and SVG"); IReadOnlyList purchases = Enumerable.Range(0, 48) .Select(index => new PurchaseLine( "SKU-" + index.ToString("D3"), index == 11 ? "Precision calibrated field-service subscription with multilingual reporting, audit-ready evidence, extended retention, and regional compliance mapping" : "Managed document service line " + index.ToString("D3"), index % 4 + 1, 19.95m)) .ToList(); string html = BuildInvoiceHtml(purchases); var options = new HtmlToPdfOptions { PageSize = OfficePageSizes.A4, Margins = HtmlRenderMargins.All(24D), BackgroundColor = OfficeColor.White, Scale = 1.5D, PdfOptions = new PdfCore.PdfOptions { FileVersion = PdfCore.PdfFileVersion.Pdf17, ObjectSerializationMode = PdfCore.PdfObjectSerializationMode.ForwardOnly, TaggedStructureMode = PdfCore.PdfTaggedStructureMode.CatalogMarkers } }; string pdfPath = Path.Combine(folderPath, "HtmlInvoiceShowcase.pdf"); string pngPath = Path.Combine(folderPath, "HtmlInvoiceShowcase.png"); string svgPath = Path.Combine(folderPath, "HtmlInvoiceShowcase.svg"); HtmlConversionDocument document = HtmlConversionDocument.Parse(html); PdfCore.PdfSaveResult saved = document.SaveAsPdf(pdfPath, options).RequireSuccess(); document.ToImage(options) .AsPng() .OnFileConflict(OfficeImageExportFileConflictPolicy.Replace) .Save(pngPath); document.ToImage(options) .AsSvg() .OnFileConflict(OfficeImageExportFileConflictPolicy.Replace) .Save(svgPath); Console.WriteLine($" PDF: {pdfPath} ({saved.Serialization?.PageCount} pages, forward-only objects: {saved.Serialization?.IsForwardOnlyObjectSerialization})"); Console.WriteLine($" PNG: {pngPath}"); Console.WriteLine($" SVG: {svgPath}"); if (openPdf) { System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(pdfPath) { UseShellExecute = true }); } } private static string BuildInvoiceHtml(IReadOnlyList purchases) { var rows = new StringBuilder(purchases.Count * 180); foreach (PurchaseLine purchase in purchases) { decimal total = purchase.Quantity * purchase.Rate; rows.Append("") .Append(WebUtility.HtmlEncode(purchase.Sku)) .Append("
") .Append(WebUtility.HtmlEncode(purchase.Description)) .Append("") .Append(purchase.Quantity) .Append("$") .Append(purchase.Rate.ToString("N2", System.Globalization.CultureInfo.InvariantCulture)) .Append("$") .Append(total.ToString("N2", System.Globalization.CultureInfo.InvariantCulture)) .Append(""); } decimal subtotal = purchases.Sum(purchase => purchase.Quantity * purchase.Rate); decimal tax = decimal.Round(subtotal * 0.08m, 2); decimal totalDue = subtotal + tax; return """
NORTHSTAR WORKS

Invoice INV-1042

Paid

Issued 11 July 2026
Due 25 July 2026

Bill to

Ada Lovelace
12 Analytical Way
London

From

OfficeIMO Services
VAT PL-104200
Warsaw

ROWS
ItemQtyRateTotal
Subtotal $SUBTOTAL
Tax $TAX
Total USD $TOTAL

View invoice

""" .Replace("ROWS", rows.ToString()) .Replace("SUBTOTAL", subtotal.ToString("N2", System.Globalization.CultureInfo.InvariantCulture)) .Replace("TAX", tax.ToString("N2", System.Globalization.CultureInfo.InvariantCulture)) .Replace("TOTAL", totalDue.ToString("N2", System.Globalization.CultureInfo.InvariantCulture)); } private sealed record PurchaseLine(string Sku, string Description, int Quantity, decimal Rate); } }