public static void ConvertHtmlToRtfStream() { 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.rtf"; // 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 rtfMemoryStream = new MemoryStream()) { bool ok = h.ToRtf(rtfMemoryStream); // Open the result for demonstration purposes. if (ok) { File.WriteAllBytes(outputFile, rtfMemoryStream.ToArray()); System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outputFile) { UseShellExecute = true }); } } } } }
public static void ConvertHtmlUrlToRtfFile() { 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.rtf"; // 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.ToRtf(outputFile); // Open the result for demonstration purposes. if (ok) { System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outputFile) { UseShellExecute = true }); } } }
public static void ConvertHtmlToRtfString() { 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.rtf"; // Read our HTML file a string. string htmlString = File.ReadAllText(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(htmlString)) { string rtfString = h.ToRtf(); // Open the result for demonstration purposes. if (!String.IsNullOrEmpty(rtfString)) { File.WriteAllText(outputFile, rtfString); System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outputFile) { UseShellExecute = true }); } } }
protected void Button1_Click(object sender, EventArgs e) { string rtfString = String.Empty; 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)) { rtfString = h.ToRtf(); } // Show result in the default RTF viewer app. if (!String.IsNullOrEmpty(rtfString)) { Response.Clear(); Response.ContentType = "application/rtf"; Response.AddHeader("content-disposition", "inline; filename=\"Result.rtf\""); byte[] data = System.Text.Encoding.UTF8.GetBytes(rtfString); Response.BinaryWrite(data); Response.Flush(); Response.End(); } else { Result.Text = "Converting failed!"; } }
public static void ConvertMultipleHtmlToRtf() { 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("RTF").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, ".rtf")); if (h.ToRtf(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; string rtfString = String.Empty; if (h.OpenHtml(htmlString)) { rtfString = h.ToRtf(); } // Show result in the default RTF viewer app. if (!String.IsNullOrEmpty(rtfString)) { Response.Clear(); Response.ContentType = "application/rtf"; Response.AddHeader("content-disposition", "inline; filename=\"Result.rtf\""); byte[] data = System.Text.Encoding.UTF8.GetBytes(rtfString); Response.BinaryWrite(data); Response.Flush(); Response.End(); } else { Result.Text = "Converting failed!"; } }
public static void ConvertHtmlToRtfFile() { 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"; if (h.OpenHtml(inputFile)) { bool ok = h.ToRtf(outputFile); // Open the result for demonstration purposes. if (ok) { System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outputFile) { UseShellExecute = true }); } } }
/// <summary> /// Convert xhtml to rtf with Sautin converter /// </summary> /// <param name="xhtml"></param> /// <param name="rtfFile"></param> public static bool ConvertSautin(string rtfFile, string xhtml) { try { xhtml = XhtmlFromReqIf(xhtml); // write to *.docx if (String.IsNullOrWhiteSpace(xhtml)) { xhtml = "Empty!!!"; } // Developer License SautinSoft.HtmlToRtf h; try { h = new SautinSoft.HtmlToRtf { Serial = "10281946238" }; } catch (Exception e) { MessageBox.Show($@"No license available You may use convert to *.docx (Settings: ""UseMariGold"" : ""true"" {e} ", @"Can't load SautinSoft library, Break"); return(false); } string xhtmlFile = System.IO.Path.GetDirectoryName(rtfFile); xhtmlFile = System.IO.Path.Combine(xhtmlFile, "xxxxxx.xhtml"); //rtfFile = System.IO.Path.GetDirectoryName(rtfFile); //rtfFile = System.IO.Path.Combine(rtfFile, "xxxxxx.rtf"); System.IO.File.WriteAllText(xhtmlFile, xhtml); if (h.OpenHtml(xhtmlFile)) { bool ok; if (xhtml.Contains("<img")) { xhtml = xhtml.Replace(@"type=""image/png""", ""); ok = h.ToRtf(rtfFile); } else { ok = h.ToRtf(rtfFile); } if (!ok) { MessageBox.Show($@"XHTML:'{xhtml}{Environment.NewLine}File:{rtfFile}", @"Error0 converting XHTML to *.rtf"); return(false); } } else { MessageBox.Show($@"XHTML:'{xhtml}{Environment.NewLine}File:{rtfFile}", @"Error1 converting XHTML to *.rtf"); return(false); } } catch (Exception e) { MessageBox.Show($@"XHTML:'{xhtml}{Environment.NewLine}{Environment.NewLine}{e}", @"Error converting XHTML to *.rtf"); return(false); } return(true); }