/// <summary> /// Converts HTML to DOCX and sets the uniform Font Family, Size and Color for all text. /// </summary> public static void SetSingleFontProperties() { SautinSoft.HtmlToRtf h = new SautinSoft.HtmlToRtf(); // After purchasing the license, please insert your serial number here to activate the component. // h.Serial = "XXXXXXXXX"; string inputFile = @"..\..\Sample.html"; string outputFile = "Result.docx"; // Let's make all text in document the same: Calibri, 32pt, Gray. h.TextStyle.SingleFontFamily = "Calibri"; h.TextStyle.SingleFontSize = 32; h.TextStyle.SingleFontColor = System.Drawing.Color.Gray; if (h.OpenHtml(inputFile)) { bool ok = h.ToDocx(outputFile); // Open the result for demonstration purposes. if (ok) { System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outputFile) { UseShellExecute = true }); } } }
public static void ConvertHtmlToDocxBytes() { SautinSoft.HtmlToRtf h = new SautinSoft.HtmlToRtf(); // After purchasing the license, please insert your serial number here to activate the component. // h.Serial = "XXXXXXXXX"; string inputFile = @"..\..\pic.html"; string outputFile = "Result.docx"; // Read our HTML file a bytes. byte[] htmlBytes = File.ReadAllBytes(inputFile); // Specify the 'BaseURL' property that component can find the full path to images, like a: <img src="..\pict.png" and // to external css, like a: <link rel="stylesheet" href="/css/style.css">. h.BaseURL = Path.GetDirectoryName(Path.GetFullPath(inputFile)); if (h.OpenHtml(htmlBytes)) { byte[] docxBytes = h.ToDocx(); // Open the result for demonstration purposes. if (docxBytes != null) { File.WriteAllBytes(outputFile, docxBytes); System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outputFile) { UseShellExecute = true }); } } }
public static void ConvertHtmlToDocxFile() { SautinSoft.HtmlToRtf h = new SautinSoft.HtmlToRtf(); // After purchasing the license, please insert your serial number here to activate the component. // h.Serial = "XXXXXXXXX"; string inputFile = @"..\..\sample.html"; string outputFile = "Result.docx"; // Set page size and page margins. h.PageStyle.PageSize.A5(); h.PageStyle.PageMarginTop.Mm(30); h.PageStyle.PageMarginBottom.Mm(30); h.PageStyle.PageMarginLeft.Mm(50); h.PageStyle.PageMarginRight.Mm(50); if (h.OpenHtml(inputFile)) { bool ok = h.ToDocx(outputFile); // Open the result for demonstration purposes. if (ok) { System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outputFile) { UseShellExecute = true }); } } }
public static void ConvertHtmlToDocxStream() { SautinSoft.HtmlToRtf h = new SautinSoft.HtmlToRtf(); // After purchasing the license, please insert your serial number here to activate the component. // h.Serial = "XXXXXXXXX"; string inputFile = @"..\..\utf-8.html"; string outputFile = "Result.docx"; // Specify the 'BaseURL' property that component can find the full path to images, like a: <img src="..\pict.png" and // to external css, like a: <link rel="stylesheet" href="/css/style.css">. h.BaseURL = Path.GetDirectoryName(Path.GetFullPath(inputFile)); using (FileStream htmlFileStrem = new FileStream(inputFile, FileMode.Open)) { if (h.OpenHtml(htmlFileStrem)) { using (MemoryStream docxMemoryStream = new MemoryStream()) { bool ok = h.ToDocx(docxMemoryStream); // Open the result for demonstration purposes. if (ok) { File.WriteAllBytes(outputFile, docxMemoryStream.ToArray()); System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outputFile) { UseShellExecute = true }); } } } } }
public static void ConvertHtmlUrlToDocxFile() { SautinSoft.HtmlToRtf h = new SautinSoft.HtmlToRtf(); // After purchasing the license, please insert your serial number here to activate the component. // h.Serial = "XXXXXXXXX"; string inputFile = @"https://www.sautinsoft.net/samples/utf-8.html"; string outputFile = "Result.docx"; // Specify the 'BaseURL' property that component can find the full path to images, like a: <img src="..\pict.png" and // to external css, like a: <link rel="stylesheet" href="/css/style.css">. h.BaseURL = @"https://www.sautinsoft.net/samples/utf-8.html"; if (h.OpenHtml(inputFile)) { bool ok = h.ToDocx(outputFile); // Open the result for demonstration purposes. if (ok) { System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outputFile) { UseShellExecute = true }); } } }
protected void Button1_Click(object sender, EventArgs e) { byte[] docxBytes = null; SautinSoft.HtmlToRtf h = new SautinSoft.HtmlToRtf(); // After purchasing the license, please insert your serial number here to activate the component. // h.Serial = "XXXXXXXXX"; // Page properties. h.PageStyle.PageSize.Letter(); h.PageStyle.PageMarginLeft.Mm(25); // Get HTML content of the current .aspx page // 1. Gets the url of the page string url = Request.Url.ToString(); // 2. Download the HTML content string html = String.Empty; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.AutomaticDecompression = DecompressionMethods.GZip; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) using (Stream responseStream = response.GetResponseStream()) using (StreamReader streamReader = new StreamReader(responseStream)) { html = streamReader.ReadToEnd(); } // Specify the property 'BaseURL', because we're loading HTML string and // it may contain relative paths to images or external .css. // The 'BaseURL' helps to get an absolute path from a relative. h.BaseURL = url; if (h.OpenHtml(html)) { docxBytes = h.ToDocx(); } // Show result in the default DOCX viewer app. if (docxBytes != null) { Response.Clear(); Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; Response.AddHeader("content-disposition", "inline; filename=\"Result.docx\""); Response.BinaryWrite(docxBytes); Response.Flush(); Response.End(); } else { Result.Text = "Converting failed!"; } }
public static void ConvertMultipleHtmlToDocx() { SautinSoft.HtmlToRtf h = new SautinSoft.HtmlToRtf(); // After purchasing the license, please insert your serial number here to activate the component. // h.Serial = "XXXXXXXXX"; string inpFolder = @"..\..\Testing HTMLs\"; string outFolder = new DirectoryInfo(Directory.GetCurrentDirectory()).CreateSubdirectory("DOCX").FullName; string[] inpFiles = Directory.GetFiles(inpFolder, "*.htm*"); int total = inpFiles.Length; int currCount = 1; int successCount = 0; foreach (string inpFile in inpFiles) { string fileName = Path.GetFileName(inpFile); Console.Write("{0:D2} of {1} ... {2}", currCount, total, fileName); currCount++; bool ok = true; if (h.OpenHtml(inpFile)) { string outFile = Path.Combine(outFolder, Path.ChangeExtension(fileName, ".docx")); if (h.ToDocx(outFile)) { successCount++; } else { ok = false; } } else { ok = false; } Console.WriteLine(" ({0})", ok); } Console.WriteLine("{0} of {1} HTML(s) converted successfully!", successCount, total); Console.WriteLine("Press any key ..."); Console.ReadKey(); // Open the result for demonstration purposes. System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFolder) { UseShellExecute = true }); }
protected void Button1_Click(object sender, EventArgs e) { SautinSoft.HtmlToRtf h = new SautinSoft.HtmlToRtf(); // After purchasing the license, please insert your serial number here to activate the component. // h.Serial = "XXXXXXXXX"; //here we specify page numbers h.PageStyle.PageNumbers.Appearance = SautinSoft.HtmlToRtf.ePageNumberingAppearence.PageNumFirst; //specify HTML format as string h.PageStyle.PageHeader.Html("<table border=\"1\"><tr><td>We added this header using the property \"Header.Html\"</td></tr></table>"); //add footer from HTML file h.PageStyle.PageFooter.FromHtmlFile(Path.Combine(Server.MapPath(""), @"footer.htm")); // Get HTML content of the current .aspx page // 1. Gets the url of the page string url = Request.Url.ToString(); // 2. Download the HTML content string htmlString = GetHtmlFromAspx(url); // Specify the property 'BaseURL', because we're loading HTML string and // it may contain relative paths to images or external .css. // The 'BaseURL' helps to get an absolute path from a relative. h.BaseURL = url; byte[] docxBytes = null; if (h.OpenHtml(htmlString)) { docxBytes = h.ToDocx(); } // Show result in the default DOCX viewer app. if (docxBytes != null) { Response.Clear(); Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; Response.AddHeader("content-disposition", "inline; filename=\"Result.docx\""); Response.BinaryWrite(docxBytes); Response.Flush(); Response.End(); } else { Result.Text = "Converting failed!"; } }
public static void AddPageNumbering() { SautinSoft.HtmlToRtf h = new SautinSoft.HtmlToRtf(); // After purchasing the license, please insert your serial number here to activate the component. // h.Serial = "XXXXXXXXX"; string inputFile = @"..\..\Sample.html"; string outputFile = "Result.rtf"; // Add page numbering. // Let's set page numbers from 1st page h.PageStyle.PageNumbers.Appearance = SautinSoft.HtmlToRtf.ePageNumberingAppearence.PageNumFirst; // Let's align page numbers by top-center h.PageStyle.PageNumbers.AlignV = SautinSoft.HtmlToRtf.eAlign.Top; h.PageStyle.PageNumbers.AlignH = SautinSoft.HtmlToRtf.eAlign.Center; // Let's set page numbers format as "Page 1 of 20". h.PageStyle.PageNumbers.Format = "Page {page} of {numpages}"; // Set page numbers font: Calibry, 36. h.PageStyle.PageNumbers.Font.Face = "Calibri"; h.PageStyle.PageNumbers.Font.Size = 36; if (h.OpenHtml(inputFile)) { bool ok = h.ToDocx(outputFile); // Open the result for demonstration purposes. if (ok) { System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outputFile) { UseShellExecute = true }); } } }
public static void AddHeaderAndFooter() { SautinSoft.HtmlToRtf h = new SautinSoft.HtmlToRtf(); // After purchasing the license, please insert your serial number here to activate the component. // h.Serial = "XXXXXXXXX"; string inputFile = @"..\..\Sample.html"; string outputFile = "Result.docx"; // Set page header and footer. string headerFromHtml = File.ReadAllText(@"..\..\header.html"); string footerFromRtf = File.ReadAllText(@"..\..\footer.rtf"); // Add page header. h.PageStyle.PageHeader.Html(headerFromHtml); // Add extra space between header and page contents. h.PageStyle.PageHeader.MarginBottom.Mm(10); // Add page footer. h.PageStyle.PageFooter.Rtf(footerFromRtf); if (h.OpenHtml(inputFile)) { bool ok = h.ToDocx(outputFile); // Open the result for demonstration purposes. if (ok) { System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outputFile) { UseShellExecute = true }); } } }
public static void ConvertHtmlToDocxFile() { SautinSoft.HtmlToRtf h = new SautinSoft.HtmlToRtf(); // After purchasing the license, please insert your serial number here to activate the component. // h.Serial = "XXXXXXXXX"; string inputFile = @"..\..\Sample.html"; string outputFile = "Result.docx"; if (h.OpenHtml(inputFile)) { bool ok = h.ToDocx(outputFile); // Open the result for demonstration purposes. if (ok) { System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outputFile) { UseShellExecute = true }); } } }