andruhovski icon

Aspose.HTML Demo 2

andruhovski | PRO | 06/04/18 05:27:22 AM UTC | 0 ⭐ | 585 👁️ | Never ⏰ | []
C# |

5.91 KB

|

None

|

0 👍

/

0 👎

using System;
using System.Threading.Tasks;
using Aspose.Html;
using Aspose.Html.Dom.Canvas;
using Aspose.Html.Drawing;
using Aspose.Html.Rendering;
using Aspose.Html.Rendering.Pdf;
using Microsoft.ProjectOxford.Face;
using Microsoft.ProjectOxford.Face.Contract;
 
namespace Canvas
{
    class Program
    {        
        private const string ImageFilePath = "http://universe.bits-pilani.ac.in/Uploads/Pilani/pilanibiologicalScienceadmin/Gallery/2015-1-5--14-36-35-944_6.JPG";
        private const string OutputFile = @"C:\aspose\pdf\output01.pdf";
 
        private static void Main()
        {
            GeneratePdfDocumentWithCanvas();
        }
 
        private static void GeneratePdfDocumentWithCanvas()
        {            
            var document = CreateHtmlTemplate();
            var faces = MakeAnalysisRequest(ImageFilePath).Result;
            GenerateReport(document, faces);
            GeneratePdfDoc(document, OutputFile);
        }
 
        private static void GenerateReport(HTMLDocument document, Face[] faces)
        {
            const int canvasWidth = 640;
            const int canvasHeight = 480;
            var colors = new[] {
                "Red", "Cyan", "Orange", "Green", "Blue", "Purple", "Black", "Aqua", "Olive", "Navy", "Teal",
                "Maroon", "Gray", "Beige", "MediumSlateBlue", "Aquamarine", "LightSalmon", "Gold", "LightSeaGreen",
                "Violet", "Chartreuse", "LightSalmon", "MediumSlateBlue"
            };
            var imageObj = (HTMLImageElement) document.GetElementsByTagName("img")[0];
            {
                imageObj.Src = ImageFilePath;
                var canvas = (HTMLCanvasElement) document.GetElementsByTagName("canvas")[0];
                
                canvas.Width = canvasWidth;
                canvas.Height = canvasHeight;
 
                var context = (ICanvasRenderingContext2D) canvas.GetContext("2d");
                context.ClearRect(0, 0, canvasWidth, canvasHeight);
                context.DrawImage(imageObj, 0, 0);
 
                context.BeginPath();
                context.LineWidth = 2.0;
 
                for (var i = 0; i < faces.Length; i++)
                {
                    context.StrokeStyle = colors[i % 24];
                    context.StrokeRect(
                        faces[i].FaceRectangle.Left,
                        faces[i].FaceRectangle.Top,
                        faces[i].FaceRectangle.Width,
                        faces[i].FaceRectangle.Height);
                    context.Stroke();
                    var paragraph = (HTMLParagraphElement) document.CreateElement("p");
                    paragraph.TextContent = $"{colors[i]} - {faces[i].FaceAttributes.Age}, {faces[i].FaceAttributes.Gender}";
                    document.DocumentElement.LastElementChild.AppendChild(paragraph);
                }                
            }
        }
 
        private static void GeneratePdfDoc(HTMLDocument document, string outputfile)
        {
            var options = new PdfRenderingOptions();
            options.PageSetup.AnyPage.Margin.Top = Unit.FromMillimeters(20);
            options.PageSetup.AnyPage.Margin.Bottom = Unit.FromMillimeters(20);
            options.PageSetup.AnyPage.Margin.Left = Unit.FromMillimeters(20);
            options.PageSetup.AnyPage.Margin.Right = Unit.FromMillimeters(20);
            
            Console.WriteLine("Render PDF file started...");
            var device = new PdfDevice(options, outputfile);
            var renderer = new HtmlRenderer();
            renderer.Render(device, document);
            Console.WriteLine("Render PDF file finished.");
        }
 
        private static HTMLDocument CreateHtmlTemplate()
        {
            var document = new Aspose.Html.HTMLDocument();
 
            document.AppendChild(document.CreateDocumentType("html", "", "", ""));
            document.DocumentElement.SetAttribute("lang", "en");
            document.Title = "Face detection report";
 
            var head = document.GetElementsByTagName("head")[0];
            var metaCharSet = (HTMLMetaElement)document.CreateElement("meta");
            metaCharSet.SetAttribute("charset", "utf-8");
 
            var metaViewPort = (HTMLMetaElement)document.CreateElement("meta");
            metaViewPort.Name = "viewport";
            metaViewPort.Content = "width=device-width, initial-scale=1";
 
            head.AppendChild(metaCharSet);
            head.AppendChild(metaViewPort);
 
            var header1 = (HTMLHeadingElement)document.CreateElement("h1");
            header1.TextContent = "Detection report";
            document.DocumentElement.LastElementChild.AppendChild(header1);
 
            var imageObj = (HTMLImageElement)document.CreateElement("img");
            imageObj.Style.SetProperty("display", "none", "");
            document.DocumentElement.LastElementChild.AppendChild(imageObj);
 
            var canvas = (HTMLCanvasElement)document.CreateElement("canvas");
            canvas.Style.CSSText = "border: 1px solid black";
            document.DocumentElement.LastElementChild.AppendChild(canvas);
 
            return document;
        }
 
        /// <summary>
        /// Gets the analysis of the specified image by using the Face REST API.
        /// </summary>
        /// <param name="imageFilePath">The image file.</param>
        private static async Task<Microsoft.ProjectOxford.Face.Contract.Face[]> MakeAnalysisRequest(string imageFilePath)
        {
            const string subscriptionKey = "202eb3df06e448ee951ad10040efe5c";
            const string uriBase = "https://westcentralus.api.cognitive.microsoft.com/face/v1.0";
 
            var faceServiceClient = new FaceServiceClient(subscriptionKey, uriBase);
            return await faceServiceClient.DetectAsync(imageFilePath, false, true,
                new[] { FaceAttributeType.Gender, FaceAttributeType.Age });
        }
 
    }
}

Comments