HelloWorldHtml.vb
  1. ''
  2. '' This code is part of Document Solutions for Imaging demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports GrapeCity.Documents.Drawing
  8. Imports GrapeCity.Documents.Imaging
  9. Imports GrapeCity.Documents.Text
  10. Imports GrapeCity.Documents.Html
  11.  
  12. '' This sample shows how to render a hard-coded HTML string.
  13. ''
  14. '' Adding DsHtml to your projects:
  15. '' - Public classes and extension methods that enable rendering HTML
  16. '' are provided by the GrapeCity.Documents.Html (DsHtml) package.
  17. '' - DsHtml supports Windows, macOS and Linux platforms.
  18. ''
  19. '' Dealing with errors when using DsHtml:
  20. '' - If DsHtml is not doing what you expect (e.g. rendering HTML to PDF
  21. '' produces an invalid PDF), check the content of the string property
  22. '' GcHtmlBrowser.ErrorLog after the call to SaveAsPdf returns,
  23. '' it may explain why the error occurred.
  24. ''
  25. '' DsHtml uses a local copy of a Chromium-based browser.
  26. '' You can either rely on Google Chrome or Microsoft Edge browsers
  27. '' installed in the operating system, or download an instance of
  28. '' Chromium browser to the application's local folder or some shared folder.
  29. '' If you know the custom path to an installed Chromium-based browser,
  30. '' use it instead of the above-mentioned options.
  31. '' The BrowserFetcher class can be used to install or fetch an already
  32. '' installed browser, you can then use the BrowserFetcher.GetDownloadedPath()
  33. '' to get the path to the browser. See the BrowserFetcher class for methods
  34. '' retrieving the path to Chrome or Edge executables and for other
  35. '' options and methods that help to download Chromium browser
  36. '' to a local folder if necessary.
  37. ''
  38. '' See the Util.NewHtmlBrowser() utility method used by this sample for a
  39. '' implementation example.
  40. ''
  41. '' The above notes apply to any project that uses DsHtml.
  42. Public Class HelloWorldHtml
  43. Function GenerateImage(
  44. ByVal pixelSize As Size,
  45. ByVal dpi As Single,
  46. ByVal opaque As Boolean,
  47. Optional ByVal sampleParams As String() = Nothing) As GcBitmap
  48.  
  49. '' HTML code that represents the content to render:
  50. Const 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. Dim margin = dpi
  73. Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, True, dpi, dpi)
  74. Using g = bmp.CreateGraphics(Color.White), browser = Util.NewHtmlBrowser()
  75. '' Render HTML.
  76. '' The return value from DrawHtml() indicates whether anything has been rendered.
  77. '' The output parameter 'size' returns the actual size of the rendered content.
  78. Dim size As SizeF
  79. Dim ok = g.DrawHtml(browser, html,
  80. margin, margin,
  81. New HtmlToImageFormat(True) With {
  82. .WindowSize = New Size(pixelSize.Height - margin * 2, pixelSize.Width - margin * 2)
  83. },
  84. size)
  85.  
  86. '' If anything has been rendered, draw an extra border around the rendered content:
  87. If ok Then
  88. Dim rc = New RectangleF(margin - 4, margin - 4, size.Width + 8, size.Height + 8)
  89. g.DrawRoundRect(rc, 8, Color.PaleVioletRed)
  90. ElseIf Not String.IsNullOrEmpty(browser.ErrorLog) Then
  91. '' Optional diagnostics that may be useful when diagnosing browser errors.
  92. '' Note that the error log may contain harmless information messages
  93. '' even if there were no errors, so testing whether the error log is empty
  94. '' should not be used as the error indicator.
  95. Util.AddNote(browser.ErrorLog, g,
  96. New RectangleF(margin, margin + size.Height + 36, bmp.PixelWidth - margin * 2, bmp.PixelHeight - size.Height - margin * 2))
  97. End If
  98. End Using
  99. Return bmp
  100. End Function
  101. End Class
  102.