PDF Creator ASP.NET Core samples · .NET 10 and .NET 11

Shapes

Draws lines, rectangles, ellipses, circles, arcs, pie slices, polygons and Bezier curves, with line styles, fill colors, gradients and transparency.

C# code of this sample

Pages/ShapesDemo.cshtml.cs
using System;
using System.ComponentModel.DataAnnotations;
using System.Drawing;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

using ExpertPdf.PdfCreator;

namespace ExpertPdf.PdfCreator.Samples.Pages
{
    public class ShapesDemoModel : PageModel
    {
        [BindProperty]
        public bool Gradients { get; set; } = true;

        public string Message { get; set; }

        public void OnGet()
        {
        }

        public IActionResult OnPost()
        {
            try
            {
                return File(CreatePdf(), "application/pdf", "Shapes.pdf");
            }
            catch (Exception ex)
            {
                Message = "The PDF document was not created. " + ex.Message;
                return Page();
            }
        }

        private byte[] CreatePdf()
        {
            // LicensingManager.LicenseKey = "your license key";

            Document document = new Document();
            PdfPage page = document.AddPage();

            // lines with different widths, dash styles and line caps
            LineElement line1 = new LineElement(10, 10, 250, 10);
            line1.LineStyle.LineWidth = 3;
            line1.ForeColor = Color.Navy;
            page.AddElement(line1);

            LineElement line2 = new LineElement(10, 30, 250, 30);
            line2.LineStyle.LineDashStyle = LineDashStyle.Dash;
            line2.ForeColor = Color.Red;
            page.AddElement(line2);

            LineElement line3 = new LineElement(10, 50, 250, 50);
            line3.LineStyle.LineWidth = 8;
            line3.LineStyle.LineCapStyle = LineCapStyle.RoundCap;
            line3.ForeColor = Color.Green;
            page.AddElement(line3);

            // rectangles: an outline with round joins, a filled one and one with a gradient
            RectangleElement rectangle1 = new RectangleElement(10, 80, 150, 100);
            rectangle1.ForeColor = Color.Blue;
            rectangle1.LineStyle.LineWidth = 5;
            rectangle1.LineStyle.LineJoinStyle = LineJoinStyle.RoundJoin;
            page.AddElement(rectangle1);

            RectangleElement rectangle2 = new RectangleElement(190, 80, 150, 100);
            rectangle2.ForeColor = Color.Blue;
            rectangle2.BackColor = Color.LightGreen;
            page.AddElement(rectangle2);

            RectangleElement rectangle3 = new RectangleElement(370, 80, 150, 100);
            rectangle3.ForeColor = Color.Blue;
            if (Gradients)
                rectangle3.Gradient = new GradientColor(GradientDirection.ForwardDiagonal, Color.LightGreen, Color.Blue);
            else
                rectangle3.BackColor = Color.SkyBlue;
            page.AddElement(rectangle3);

            // ellipses and a circle: the coordinates are the center, then the radiuses
            EllipseElement ellipse1 = new EllipseElement(85, 260, 75, 45);
            ellipse1.ForeColor = Color.Blue;
            ellipse1.LineStyle.LineDashStyle = LineDashStyle.Dash;
            page.AddElement(ellipse1);

            EllipseElement ellipse2 = new EllipseElement(265, 260, 75, 45);
            ellipse2.ForeColor = Color.Blue;
            if (Gradients)
                ellipse2.Gradient = new GradientColor(GradientDirection.Vertical, Color.Yellow, Color.Red);
            else
                ellipse2.BackColor = Color.Orange;
            page.AddElement(ellipse2);

            CircleElement circle = new CircleElement(445, 260, 45);
            circle.ForeColor = Color.DarkGreen;
            circle.BackColor = Color.LightGreen;
            circle.Transparency = 60;
            page.AddElement(circle);

            // an arc and a pie slice of an ellipse (bounding rectangle, start angle and sweep angle)
            EllipseArcElement arc = new EllipseArcElement(10, 340, 150, 100, 0, 270);
            arc.ForeColor = Color.Purple;
            arc.LineStyle.LineWidth = 3;
            page.AddElement(arc);

            EllipseSliceElement slice = new EllipseSliceElement(190, 340, 150, 100, 30, 120);
            slice.ForeColor = Color.Maroon;
            slice.BackColor = Color.Salmon;
            page.AddElement(slice);

            // a polygon
            PolygonElement polygon = new PolygonElement(new PointF[] {
                new PointF(445, 340), new PointF(520, 390), new PointF(490, 440), new PointF(400, 440), new PointF(370, 390) });
            polygon.ForeColor = Color.Navy;
            polygon.BackColor = Color.LightSteelBlue;
            page.AddElement(polygon);

            // Bezier curves: the start point, two control points and the end point
            BezierCurveElement curve1 = new BezierCurveElement(10, 520, 100, 470, 200, 570, 300, 520);
            curve1.ForeColor = Color.Blue;
            curve1.LineStyle.LineWidth = 3;
            page.AddElement(curve1);

            BezierCurveElement curve2 = new BezierCurveElement(10, 520, 100, 570, 200, 470, 300, 520);
            curve2.ForeColor = Color.Green;
            curve2.LineStyle.LineWidth = 3;
            page.AddElement(curve2);

            byte[] pdf = document.Save();
            document.Close();
            return pdf;
        }
    }
}