Convert HTML to Word in C#
HTML to DOCX
Nutrient .NET SDK converts HTML files to DOCX documents through an intermediate PDF representation.
Download samplePrepare the project
Register the SDK license before converting a document. For setup details, refer to the getting started with .NET SDK guide.
using GdPicture14;
LicenseManager license = new LicenseManager();license.RegisterKEY("");Convert HTML to Word
Set the page dimensions and margins, convert the HTML stream to PDF, then load the PDF and save it as a DOCX file.
using GdPictureDocumentConverter converter = new GdPictureDocumentConverter();
converter.HtmlPageHeight = 842;converter.HtmlPageWidth = 1191;converter.HtmlPageMarginTop = 10;converter.HtmlPageMarginBottom = 10;converter.HtmlPageMarginLeft = 10;converter.HtmlPageMarginRight = 10;
using Stream inputStream = File.OpenRead(@"input.html");using MemoryStream outputStream = new MemoryStream();
GdPictureStatus status = converter.ConvertToPDF(inputStream, GdPicture14.DocumentFormat.DocumentFormatHTML, outputStream, PdfConformance.PDF1_5);if (status != GdPictureStatus.OK){ throw new Exception(status.ToString());}
outputStream.Position = 0;status = converter.LoadFromStream(outputStream, GdPicture14.DocumentFormat.DocumentFormatPDF);if (status != GdPictureStatus.OK){ throw new Exception(status.ToString());}
status = converter.SaveAsDOCX("output.docx");if (status != GdPictureStatus.OK){ throw new Exception(status.ToString());}
status = converter.CloseDocument();if (status != GdPictureStatus.OK && status != GdPictureStatus.Aborted){ throw new Exception(status.ToString());}