HelloWorldHtml.cs
  1. //
  2. // This code is part of Document Solutions for Imaging demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using GrapeCity.Documents.Drawing;
  9. using GrapeCity.Documents.Imaging;
  10. using GrapeCity.Documents.Text;
  11. using GrapeCity.Documents.Html;
  12.  
  13. namespace DsImagingWeb.Demos
  14. {
  15. // This sample shows how to render a hard-coded HTML string.
  16. //
  17. // Adding DsHtml to your projects:
  18. // - Public classes and extension methods that enable rendering HTML
  19. // are provided by the GrapeCity.Documents.Html (DsHtml) package.
  20. // - DsHtml supports Windows, macOS and Linux platforms.
  21. //
  22. // Dealing with errors when using DsHtml:
  23. // - If DsHtml is not doing what you expect (e.g. rendering HTML to PDF
  24. // produces an invalid PDF), check the content of the string property
  25. // DsHtmlBrowser.ErrorLog after the call to SaveAsPdf returns,
  26. // it may explain why the error occurred.
  27. //
  28. // DsHtml uses a local copy of a Chromium-based browser.
  29. // You can either rely on Google Chrome or Microsoft Edge browsers
  30. // installed in the operating system, or download an instance of
  31. // Chromium browser to the application's local folder or some shared folder.
  32. // If you know the custom path to an installed Chromium-based browser,
  33. // use it instead of the above-mentioned options.
  34. // The BrowserFetcher class can be used to install or fetch an already
  35. // installed browser, you can then use the BrowserFetcher.GetDownloadedPath()
  36. // to get the path to the browser. See the BrowserFetcher class for methods
  37. // retrieving the path to Chrome or Edge executables and for other
  38. // options and methods that help to download Chromium browser
  39. // to a local folder if necessary.
  40. //
  41. // See the Util.NewHtmlBrowser() utility method used by this sample for a
  42. // implementation example.
  43. //
  44. // The above notes apply to any project that uses DsHtml.
  45. public class HelloWorldHtml
  46. {
  47. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  48. {
  49. // HTML code that represents the content to render:
  50. var html = "<!DOCTYPE html>" +
  51. "<html>" +
  52. "<head>" +
  53. "<style>" +
  54. "span.bold {" +
  55. "font-weight: bold;" +
  56. "}" +
  57. "p.round {" +
  58. "font: 36px arial, sans-serif;" +
  59. "color: DarkSlateBlue;" +
  60. "border: 4px solid SlateBlue;" +
  61. "border-radius: 16px;" +
  62. "padding: 3px 5px 3px 5px;" +
  63. "text-shadow: 3px 2px LightSkyBlue;" +
  64. "}" +
  65. "</style>" +
  66. "</head>" +
  67. "<body>" +
  68. "<p class='round'>Hello, World, from <span class='bold'>DsHtml</span>!</p>" +
  69. "</body>" +
  70. "</html>";
  71.  
  72. var margin = dpi;
  73. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, true, dpi, dpi);
  74. using (var g = bmp.CreateGraphics(Color.White))
  75. {
  76. // Create an instance of GcHtmlBrowser that is used to render HTML:
  77. using var browser = Common.Util.NewHtmlBrowser();
  78.  
  79. // Render HTML.
  80. // The return value from DrawHtml() indicates whether anything has been rendered.
  81. // The output parameter 'size' returns the actual size of the rendered content.
  82. var ok = g.DrawHtml(browser, html,
  83. margin, margin,
  84. new HtmlToImageFormat(true)
  85. {
  86. WindowSize = new Size((int)(pixelSize.Height - margin * 2), (int)(pixelSize.Width - margin * 2))
  87. },
  88. out SizeF size);
  89.  
  90. // If anything has been rendered, draw an extra border around the rendered content:
  91. if (ok)
  92. {
  93. var rc = new RectangleF(margin - 4, margin - 4, size.Width + 8, size.Height + 8);
  94. g.DrawRoundRect(rc, 8, Color.PaleVioletRed);
  95. }
  96. else if (!string.IsNullOrEmpty(browser.ErrorLog))
  97. {
  98. // Optional diagnostics that may be useful when diagnosing browser errors.
  99. // Note that the error log may contain harmless information messages
  100. // even if there were no errors, so testing whether the error log is empty
  101. // should not be used as the error indicator.
  102. Common.Util.AddNote(browser.ErrorLog, g,
  103. new RectangleF(margin, margin + size.Height + 36, bmp.PixelWidth - margin * 2, bmp.PixelHeight - size.Height - margin * 2));
  104. }
  105. }
  106. return bmp;
  107. }
  108. }
  109. }
  110.