Advertisement
andruhovski

Aspose.HTML Demo 2

Jun 4th, 2018
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.91 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.             GeneratePdfDocumentWithCanvas();
  21.         }
  22.  
  23.         private static void GeneratePdfDocumentWithCanvas()
  24.         {            
  25.             var document = CreateHtmlTemplate();
  26.             var faces = MakeAnalysisRequest(ImageFilePath).Result;
  27.             GenerateReport(document, faces);
  28.             GeneratePdfDoc(document, OutputFile);
  29.         }
  30.  
  31.         private static void GenerateReport(HTMLDocument document, Face[] faces)
  32.         {
  33.             const int canvasWidth = 640;
  34.             const int canvasHeight = 480;
  35.             var colors = new[] {
  36.                 "Red", "Cyan", "Orange", "Green", "Blue", "Purple", "Black", "Aqua", "Olive", "Navy", "Teal",
  37.                 "Maroon", "Gray", "Beige", "MediumSlateBlue", "Aquamarine", "LightSalmon", "Gold", "LightSeaGreen",
  38.                 "Violet", "Chartreuse", "LightSalmon", "MediumSlateBlue"
  39.             };
  40.             var imageObj = (HTMLImageElement) document.GetElementsByTagName("img")[0];
  41.             {
  42.                 imageObj.Src = ImageFilePath;
  43.                 var canvas = (HTMLCanvasElement) document.GetElementsByTagName("canvas")[0];
  44.                
  45.                 canvas.Width = canvasWidth;
  46.                 canvas.Height = canvasHeight;
  47.  
  48.                 var context = (ICanvasRenderingContext2D) canvas.GetContext("2d");
  49.                 context.ClearRect(0, 0, canvasWidth, canvasHeight);
  50.                 context.DrawImage(imageObj, 0, 0);
  51.  
  52.                 context.BeginPath();
  53.                 context.LineWidth = 2.0;
  54.  
  55.                 for (var i = 0; i < faces.Length; i++)
  56.                 {
  57.                     context.StrokeStyle = colors[i % 24];
  58.                     context.StrokeRect(
  59.                         faces[i].FaceRectangle.Left,
  60.                         faces[i].FaceRectangle.Top,
  61.                         faces[i].FaceRectangle.Width,
  62.                         faces[i].FaceRectangle.Height);
  63.                     context.Stroke();
  64.                     var paragraph = (HTMLParagraphElement) document.CreateElement("p");
  65.                     paragraph.TextContent = $"{colors[i]} - {faces[i].FaceAttributes.Age}, {faces[i].FaceAttributes.Gender}";
  66.                     document.DocumentElement.LastElementChild.AppendChild(paragraph);
  67.                 }                
  68.             }
  69.         }
  70.  
  71.         private static void GeneratePdfDoc(HTMLDocument document, string outputfile)
  72.         {
  73.             var options = new PdfRenderingOptions();
  74.             options.PageSetup.AnyPage.Margin.Top = Unit.FromMillimeters(20);
  75.             options.PageSetup.AnyPage.Margin.Bottom = Unit.FromMillimeters(20);
  76.             options.PageSetup.AnyPage.Margin.Left = Unit.FromMillimeters(20);
  77.             options.PageSetup.AnyPage.Margin.Right = Unit.FromMillimeters(20);
  78.            
  79.             Console.WriteLine("Render PDF file started...");
  80.             var device = new PdfDevice(options, outputfile);
  81.             var renderer = new HtmlRenderer();
  82.             renderer.Render(device, document);
  83.             Console.WriteLine("Render PDF file finished.");
  84.         }
  85.  
  86.         private static HTMLDocument CreateHtmlTemplate()
  87.         {
  88.             var document = new Aspose.Html.HTMLDocument();
  89.  
  90.             document.AppendChild(document.CreateDocumentType("html", "", "", ""));
  91.             document.DocumentElement.SetAttribute("lang", "en");
  92.             document.Title = "Face detection report";
  93.  
  94.             var head = document.GetElementsByTagName("head")[0];
  95.             var metaCharSet = (HTMLMetaElement)document.CreateElement("meta");
  96.             metaCharSet.SetAttribute("charset", "utf-8");
  97.  
  98.             var metaViewPort = (HTMLMetaElement)document.CreateElement("meta");
  99.             metaViewPort.Name = "viewport";
  100.             metaViewPort.Content = "width=device-width, initial-scale=1";
  101.  
  102.             head.AppendChild(metaCharSet);
  103.             head.AppendChild(metaViewPort);
  104.  
  105.             var header1 = (HTMLHeadingElement)document.CreateElement("h1");
  106.             header1.TextContent = "Detection report";
  107.             document.DocumentElement.LastElementChild.AppendChild(header1);
  108.  
  109.             var imageObj = (HTMLImageElement)document.CreateElement("img");
  110.             imageObj.Style.SetProperty("display", "none", "");
  111.             document.DocumentElement.LastElementChild.AppendChild(imageObj);
  112.  
  113.             var canvas = (HTMLCanvasElement)document.CreateElement("canvas");
  114.             canvas.Style.CSSText = "border: 1px solid black";
  115.             document.DocumentElement.LastElementChild.AppendChild(canvas);
  116.  
  117.             return document;
  118.         }
  119.  
  120.         /// <summary>
  121.         /// Gets the analysis of the specified image by using the Face REST API.
  122.         /// </summary>
  123.         /// <param name="imageFilePath">The image file.</param>
  124.         private static async Task<Microsoft.ProjectOxford.Face.Contract.Face[]> MakeAnalysisRequest(string imageFilePath)
  125.         {
  126.             const string subscriptionKey = "202eb3df06e448ee951ad10040efe5c";
  127.             const string uriBase = "https://westcentralus.api.cognitive.microsoft.com/face/v1.0";
  128.  
  129.             var faceServiceClient = new FaceServiceClient(subscriptionKey, uriBase);
  130.             return await faceServiceClient.DetectAsync(imageFilePath, false, true,
  131.                 new[] { FaceAttributeType.Gender, FaceAttributeType.Age });
  132.         }
  133.  
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement