RenderPage0.cs
  1. //
  2. // This code is part of Document Solutions for PDF 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.Pdf;
  9. using GrapeCity.Documents.Text;
  10. using GrapeCity.Documents.Html;
  11.  
  12. namespace DsPdfWeb.Demos
  13. {
  14. // This sample shows the simplest way to render a web page
  15. // specified by a URL to a PDF (here we render the Google home page).
  16. //
  17. // In this sample we use GcHtmlBrowser to create an instance of HtmlPage
  18. // that loads the specified URI, and then call the HtmlPage.SaveAsPdf()
  19. // method to render the page to PDF.
  20. //
  21. // A different approach that allows you to easily add HTML content
  22. // to a PDF file along with other content is via the extension
  23. // methods GcPdfGraphics.MeasureHtml()/GcPdfGraphics.DrawHtml()
  24. // as demonstrated by HelloWorldHtml and other samples.
  25. // Note that those methods require an instance of GcHtmlBrowser
  26. // to be passed as parameter.
  27. //
  28. // Please see notes in comments at the top of HelloWorldHtml
  29. // sample code for details on adding DsHtml to your projects.
  30. public class RenderPage0
  31. {
  32. public void CreatePDF(Stream stream)
  33. {
  34. // Get a temporary file where the web page will be rendered:
  35. var tmp = Path.GetTempFileName();
  36. // The Uri of the web page to render:
  37. var uri = new Uri("http://www.google.com");
  38. // Create an instance of GcHtmlBrowser that is used to render HTML:
  39. using var browser = Common.Util.NewHtmlBrowser();
  40. // Create an HtmlPage instance rendering the source Uri:
  41. using var htmlPage = browser.NewPage(uri);
  42. // Render the source Web page to the temporary file:
  43. htmlPage.SaveAsPdf(tmp);
  44. // Copy the created PDF from the temp file to target stream:
  45. using (var ts = File.OpenRead(tmp))
  46. ts.CopyTo(stream);
  47. // Clean up:
  48. File.Delete(tmp);
  49. // Done.
  50. }
  51. }
  52. }
  53.