Advertisement
andruhovski

Untitled

Jun 26th, 2018
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.68 KB | None | 0 0
  1. using System;
  2. using System.Threading.Tasks;
  3. using Aspose.Html;
  4. using Aspose.Html.Dom.Canvas;
  5. using Aspose.Html.Drawing;
  6. using Aspose.Html.Rendering;
  7. using Aspose.Html.Rendering.Pdf;
  8. using Microsoft.ProjectOxford.Face;
  9. using Microsoft.ProjectOxford.Face.Contract;
  10.  
  11. namespace Canvas
  12. {
  13. class Program
  14. {
  15. private const string ImageFilePath = "http://universe.bits-pilani.ac.in/Uploads/Pilani/pilanibiologicalScienceadmin/Gallery/2015-1-5--14-36-35-944_6.JPG";
  16. private const string OutputFile = @"C:\aspose\pdf\output01.pdf";
  17.  
  18. private static void Main()
  19. {
  20. ConvertCanvastoPdf();
  21. //ConvertCanvastoPdf2();
  22. CanvasToPdfDemo();
  23. }
  24.  
  25. private static void ConvertCanvastoPdf()
  26. {
  27. const string content = "<!DOCTYPE html><html><head><title>A Simple Canvas Example</title><style>body{background: #aabbcc;}" +
  28. "#canvas{margin: 10px;padding: 10px;background: #ffffff;border: 3px groove #aabbcc;}</style></head>" +
  29. "<body>" +
  30. "<canvas id='canvas' width='320' height='240'> Canvas not supported</canvas>" +
  31. "<script>" +
  32. "var canvas=document.getElementById('canvas');" +
  33. "var context=canvas.getContext('2d');" +
  34. "context.font='20pt Arial';context.fillStyle='navy';context.strokeStyle='blue';" +
  35. "context.fillText('Hello Canvas', 30, 30);" +
  36. "context.strokeText('Hello Canvas', 30, 30);" +
  37. "</script>" +
  38. "</body></html>";
  39. var document = new HTMLDocument(content, string.Empty);
  40. GeneratePdfDoc(document, OutputFile);
  41. }
  42.  
  43. private static void CanvasToPdfDemo()
  44. {
  45. var document = CreateHtmlTemplate();
  46. var faces = MakeAnalysisRequest(ImageFilePath).Result;
  47. GenerateReport(document, faces);
  48. GeneratePdfDoc(document, OutputFile);
  49. }
  50.  
  51. private static void GenerateReport(HTMLDocument document, Face[] faces)
  52. {
  53. const int canvasWidth = 640;
  54. const int canvasHeight = 480;
  55. var colors = new[] {
  56. "Red", "Cyan", "Orange", "Green", "Blue", "Purple", "Black", "Aqua", "Olive", "Navy", "Teal",
  57. "Maroon", "Gray", "Beige", "MediumSlateBlue", "Aquamarine", "LightSalmon", "Gold", "LightSeaGreen",
  58. "Violet", "Chartreuse", "LightSalmon", "MediumSlateBlue"
  59. };
  60.  
  61. var imageObj = (HTMLImageElement)document.CreateElement("img");
  62. imageObj.Src = ImageFilePath;
  63.  
  64. var canvas = (HTMLCanvasElement)document.GetElementsByTagName("canvas")[0];
  65.  
  66. canvas.Width = canvasWidth;
  67. canvas.Height = canvasHeight;
  68.  
  69. var context = (ICanvasRenderingContext2D)canvas.GetContext("2d");
  70. context.ClearRect(0, 0, canvasWidth, canvasHeight);
  71. context.DrawImage(imageObj, 0, 0);
  72.  
  73. context.BeginPath();
  74. context.LineWidth = 2.0;
  75.  
  76. for (var i = 0; i < faces.Length; i++)
  77. {
  78. context.StrokeStyle = colors[i % 24];
  79. context.StrokeRect(
  80. faces[i].FaceRectangle.Left,
  81. faces[i].FaceRectangle.Top,
  82. faces[i].FaceRectangle.Width,
  83. faces[i].FaceRectangle.Height);
  84. context.Stroke();
  85. var paragraph = (HTMLParagraphElement)document.CreateElement("p");
  86. paragraph.TextContent = $"{colors[i]} - {faces[i].FaceAttributes.Age}, {faces[i].FaceAttributes.Gender}";
  87. document.DocumentElement.LastElementChild.AppendChild(paragraph);
  88. }
  89. }
  90.  
  91. private static void GeneratePdfDoc(HTMLDocument document, string outputfile)
  92. {
  93. var options = new PdfRenderingOptions();
  94. options.PageSetup.AnyPage.Margin.Top = Unit.FromMillimeters(20);
  95. options.PageSetup.AnyPage.Margin.Bottom = Unit.FromMillimeters(20);
  96. options.PageSetup.AnyPage.Margin.Left = Unit.FromMillimeters(20);
  97. options.PageSetup.AnyPage.Margin.Right = Unit.FromMillimeters(20);
  98.  
  99. Console.WriteLine("Render PDF file started...");
  100. var device = new PdfDevice(options, outputfile);
  101. var renderer = new HtmlRenderer();
  102. renderer.Render(device, document);
  103. Console.WriteLine("Render PDF file finished.");
  104. }
  105.  
  106. private static HTMLDocument CreateHtmlTemplate()
  107. {
  108. var document = new HTMLDocument();
  109.  
  110. document.AppendChild(document.CreateDocumentType("html", "", "", ""));
  111. document.DocumentElement.SetAttribute("lang", "en");
  112. document.Title = "Face detection report";
  113.  
  114. var head = document.GetElementsByTagName("head")[0];
  115. var metaCharSet = (HTMLMetaElement)document.CreateElement("meta");
  116. metaCharSet.SetAttribute("charset", "utf-8");
  117.  
  118. var metaViewPort = (HTMLMetaElement)document.CreateElement("meta");
  119. metaViewPort.Name = "viewport";
  120. metaViewPort.Content = "width=device-width, initial-scale=1";
  121.  
  122. head.AppendChild(metaCharSet);
  123. head.AppendChild(metaViewPort);
  124.  
  125. var header1 = (HTMLHeadingElement)document.CreateElement("h1");
  126. header1.TextContent = "Detection report";
  127. document.DocumentElement.LastElementChild.AppendChild(header1);
  128.  
  129. var canvas = (HTMLCanvasElement)document.CreateElement("canvas");
  130. canvas.Style.CSSText = "border: 1px solid black";
  131. document.DocumentElement.LastElementChild.AppendChild(canvas);
  132.  
  133. return document;
  134. }
  135.  
  136. /// <summary>
  137. /// Gets the analysis of the specified image by using the Face REST API.
  138. /// </summary>
  139. /// <param name="imageFilePath">The image file.</param>
  140. private static async Task<Face[]> MakeAnalysisRequest(string imageFilePath)
  141. {
  142. const string subscriptionKey = "<key>";
  143. const string uriBase = "https://westcentralus.api.cognitive.microsoft.com/face/v1.0";
  144.  
  145. var faceServiceClient = new FaceServiceClient(subscriptionKey, uriBase);
  146. return await faceServiceClient.DetectAsync(imageFilePath, false, true,
  147. new[] { FaceAttributeType.Gender, FaceAttributeType.Age });
  148. }
  149. }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement