コード例 #1
0
ファイル: CertificateService.cs プロジェクト: GIEMedia/PCT
        public void CreateCertificate(Account account, CourseProgress courseProgress, DateTime dateEarnedUtc)
        {
            var certificate = new Certificate { EarnedUtc = dateEarnedUtc };
            courseProgress.Certificate = certificate;
            _entityRepository.Save(courseProgress);

            CreateUserCertificate(certificate, account, courseProgress, dateEarnedUtc);

            var licensuresWithCEUs =
                account.StateLicensures.Where(
                    s => courseProgress.Course.StateCEUs.Any(ceu => ceu.StateAbbr == s.StateAbbr)).ToList();

            foreach (var licensure in licensuresWithCEUs)
                CreateStateCertificate(certificate, account, licensure, courseProgress, dateEarnedUtc);

            using (var document = new iTextSharp.text.Document())
            using (var stream = new FileStream(CertificatePath + certificate.ID + ".pdf",
                FileMode.Create, FileAccess.Write, FileShare.None))
            {
                document.SetPageSize(new iTextSharp.text.Rectangle(700, 570));
                iTextSharp.text.pdf.PdfWriter.GetInstance(document, stream);
                document.Open();

                // User Certificate
                document.SetPageSize(new iTextSharp.text.Rectangle(700, 570));
                using (var imageStream = new FileStream(CertificatePath + certificate.ID + "_user.jpg",
                    FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    var image = iTextSharp.text.Image.GetInstance(imageStream);
                    image.ScaleToFit(800f, 500f);
                    document.Add(image);
                }

                // State Certificates
                foreach (var licensure in licensuresWithCEUs)
                {
                    document.SetPageSize(new iTextSharp.text.Rectangle(2550, 3300));
                    using (
                        var imageStream =
                            new FileStream(CertificatePath + certificate.ID + "_state_" + licensure.ID + ".jpg",
                                FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        var image = iTextSharp.text.Image.GetInstance(imageStream);
                        //image.ScaleToFit(800f, 500f);
                        document.Add(image);
                    }
                }

                document.Close();
            }
        }
コード例 #2
0
ファイル: CertificateService.cs プロジェクト: GIEMedia/PCT
        private void CreateStateCertificate(Certificate certificate, Account account, StateLicensure licensure, CourseProgress courseProgress, DateTime dateEarnedUtc)
        {
            var certTemplate = new Bitmap(TemplatePathState);

            var ceu =
                courseProgress.Course.StateCEUs.First(
                    s => courseProgress.Course.StateCEUs.Any(c => c.StateAbbr == s.StateAbbr));

            var g = Graphics.FromImage(certTemplate);
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

            var alignCenter = new StringFormat
            {
                Alignment = StringAlignment.Center,
                LineAlignment = StringAlignment.Center
            };

            g.DrawString(account.FirstName + " " + account.LastName,
                new System.Drawing.Font("Arial", 28), Brushes.Black, new RectangleF(515, 818, 400, 100));

            g.DrawString(licensure.LicenseNum + "\n" + licensure.StateAbbr,
                new System.Drawing.Font("Arial", 28), Brushes.Black, new RectangleF(1498, 800, 400, 100));

            g.DrawString(ceu.Category.Number,
                new System.Drawing.Font("Arial", 28), Brushes.Black, new RectangleF(2218, 818, 400, 100));

            g.DrawString(courseProgress.Course.DisplayTitle,
                new System.Drawing.Font("Arial", 28), Brushes.Black, new RectangleF(587, 1040, 1252, 100));

            g.DrawString(ceu.ActivityID,
                new System.Drawing.Font("Arial", 28), Brushes.Black, new RectangleF(2155, 1040, 400, 100));

            g.DrawString(courseProgress.LastActivityUtc.ToLocalTime().ToString("d"),
                new System.Drawing.Font("Arial", 28), Brushes.Black, new RectangleF(515, 1263, 400, 100));

            g.DrawString(ceu.ActivityType,
               new System.Drawing.Font("Arial", 28), Brushes.Black, new RectangleF(2200, 1263, 400, 100));

            var activityTime = TimeSpan.FromMinutes(courseProgress.ActiveTime);
            g.DrawString(activityTime.Hours + ":" + activityTime.Minutes,
                new System.Drawing.Font("Arial", 28), Brushes.Black, new RectangleF(515, 1484, 400, 100));

            var hours = TimeSpan.FromHours((double)ceu.Hours);
            g.DrawString(hours.Hours + ":" + hours.Minutes,
                new System.Drawing.Font("Arial", 28), Brushes.Black, new RectangleF(1225, 1484, 400, 100));

            var percentage = courseProgress.TestProgress.CompletedQuestions.Count(r => r.CorrectOnAttempt != null) /
                             (decimal)courseProgress.Course.Test.Questions.Count;
            var grade = (percentage * 100).Round(true, 0) + "%";
            g.DrawString(grade, new System.Drawing.Font("Arial", 28), Brushes.Black, new RectangleF(1915, 1484, 400, 100));

            if (!courseProgress.VerificationInitials.IsNullOrEmpty() && courseProgress.VerificationDate.HasValue)
                g.DrawString(courseProgress.VerificationInitials + "  " + courseProgress.VerificationDate.Value.ToShortDateString(),
                    new System.Drawing.Font("Arial", 24), Brushes.Black, new RectangleF(595, 1685, 277, 100),
                    alignCenter);

            g.DrawString(DateTime.Now.ToString("d"),
                new System.Drawing.Font("Arial", 28), Brushes.Black, new RectangleF(788, 2240, 276, 100),
                alignCenter);

            certTemplate.Save(CertificatePath + certificate.ID + "_state_" + licensure.ID + ".jpg", ImageFormat.Jpeg);
        }
コード例 #3
0
ファイル: CertificateService.cs プロジェクト: GIEMedia/PCT
        private void CreateUserCertificate(Certificate certificate, Account account, CourseProgress courseProgress, DateTime dateEarnedUtc)
        {
            var certTemplate = new Bitmap(TemplatePathUser);

            var g = Graphics.FromImage(certTemplate);
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

            var alignCenter = new StringFormat
            {
                Alignment = StringAlignment.Center,
                LineAlignment = StringAlignment.Center
            };

            g.DrawString(account.FirstName + " " + account.LastName,
                new System.Drawing.Font("Javanese Text", 26), Brushes.Black, new RectangleF(0, 0, 1110, 797),
                alignCenter);
            var font = new System.Drawing.Font("Javanese Text", 26);
            var maxWidth = 1100f;
            if (CalculateLimitedWidth(courseProgress.Course.DisplayTitle, g, font, maxWidth))
            {
                g.DrawString(courseProgress.Course.DisplayTitle,
                    new System.Drawing.Font("Javanese Text", 26), Brushes.Black, new RectangleF(0, 0, maxWidth, 1000),
                    alignCenter);
            }
            else
            {
                DrawMultipleLines(courseProgress.Course.DisplayTitle, g, font, maxWidth, 445, alignCenter);
            }
            g.DrawString(dateEarnedUtc.ToLongDateString(),
                new System.Drawing.Font("Javanese Text", 21), Brushes.Black, new RectangleF(480, 515, 400, 100),
                alignCenter);
            g.DrawString(account.FirstName + " " + account.LastName,
                new System.Drawing.Font("Javanese Text", 18), Brushes.Black, new RectangleF(115, 630, 400, 100));
            g.DrawString(account.FirstName + " " + account.LastName,
                new System.Drawing.Font("Javanese Text", 18), Brushes.Black, new RectangleF(615, 630, 400, 100));

            float height = 690;
            foreach (var s in account.StateLicensures)
            {
                g.DrawString(s.StateAbbr,
                    new System.Drawing.Font("Javanese Text", 8), Brushes.Black, new RectangleF(119, height, 100, 50));
                var category = !string.IsNullOrEmpty(s.Category) ? _entityRepository.GetByID<CertificationCategory>(s.Category.ToGuid(true)) : null;
                if (category != null)
                {
                    g.DrawString(category.Name + " " + category.Number,
                        new System.Drawing.Font("Javanese Text", 8), Brushes.Black, new RectangleF(230, height, 100, 50));
                }
                g.DrawString(s.LicenseNum,
                    new System.Drawing.Font("Javanese Text", 8), Brushes.Black, new RectangleF(380, height, 100, 50));
                height = height + 15;
            }

            g.DrawString(account.CompanyName,
                new System.Drawing.Font("Javanese Text", 12, FontStyle.Bold), Brushes.Black,
                new RectangleF(615, 670, 400, 100));
            g.DrawString(account.CompanyAddress.Address1,
                new System.Drawing.Font("Javanese Text", 10), Brushes.Black, new RectangleF(615, 688, 400, 100));
            var cityStateZipTop = 706;
            if (!account.CompanyAddress.Address2.IsNullOrEmpty())
            {
                g.DrawString(account.CompanyAddress.Address2,
                    new System.Drawing.Font("Javanese Text", 10), Brushes.Black, new RectangleF(615, 706, 400, 100));
                cityStateZipTop = 724;
            }
            g.DrawString(
                account.CompanyAddress.City + ", " + account.CompanyAddress.State + ", " +
                account.CompanyAddress.ZipCode,
                new System.Drawing.Font("Javanese Text", 10), Brushes.Black, new RectangleF(615, cityStateZipTop, 400, 100));

            certTemplate.Save(CertificatePath + certificate.ID + "_user.jpg", ImageFormat.Jpeg);
        }
コード例 #4
0
ファイル: CertificateTests.cs プロジェクト: GIEMedia/PCT
        public void CanCreateCertificate()
        {
            var moqRepo = new Mock<IEntityRepository>();
            moqRepo.Setup(x => x.Save(It.IsAny<CourseProgress>()));

            var certService = new CertificateService(moqRepo.Object);
            certService.SetPaths(
                @"D:\Source Control\Prototype1\GIE-PST\PCT.Tests\Content\Certificates\",
                @"D:\Source Control\Prototype1\GIE-PST\PCT.Tests\Content\Certificates\Template\certificate_template_user.jpg",
                @"D:\Source Control\Prototype1\GIE-PST\PCT.Tests\Content\Certificates\Template\certificate_template_state.jpg");

            var account = new Account
            {
                FirstName = "Oleg",
                LastName = "Fridman",
                StateLicensures = new List<StateLicensure>
                {
                    new StateLicensure {Category = "Cat1", LicenseNum = "ABC123", StateAbbr = "OH", ID = Guid.NewGuid()}
                },
                CompanyAddress = new Address
                {
                    Address1 = "123 Main St",
                    Address2 = "Suite 123",
                    City = "Austin",
                    State = "TX",
                    ZipCode = "73723",
                    Phone = "5125232323"
                },
                CompanyName = "Prototype1, LLC"
            };

            var courseProgress = new CourseProgress
            {
                VerificationInitials = "OF",
                VerificationDate = DateTime.UtcNow,
                ActiveTime = (int) TimeSpan.FromHours(1.8).TotalMinutes,
                LastActivityUtc = DateTime.UtcNow.AddMinutes(-5),
                Course = new Course
                {
                    Title = "Allergy Technologies: ActiveGuard® Mattress Liners Allergy Technologies: ActiveGuard® Mattress Liners",
                    StateCEUs = new List<StateCEU>
                    {
                        new StateCEU
                        {
                            StateAbbr = "OH",
                            Category = new CertificationCategory
                            {
                                Name = "OH Category",
                                Number = "OHCAT1",
                                StateAbbr = "OH"
                            },
                            Hours = 1.5m,
                            ActivityID = "ActID",
                            ActivityType = "General"
                        }
                    },
                    Test = new Test
                    {
                        Questions = new List<Question>
                        {
                            new TextQuestion(),
                            new TextQuestion(),
                            new TextQuestion(),
                            new TextQuestion()
                        }
                    }
                },
                TestProgress = new TestProgress
                {
                    CompletedQuestions = new List<TestQuestionProgress>
                    {
                        new TestQuestionProgress { CorrectOnAttempt = 1 },
                        new TestQuestionProgress { CorrectOnAttempt = 1 },
                        new TestQuestionProgress { CorrectOnAttempt = 1 },
                        new TestQuestionProgress { CorrectOnAttempt = 1 }
                    }
                }
            };

            certService.CreateCertificate(account, courseProgress, DateTime.UtcNow);

            var id = DateTime.Now.Ticks;
            new FileInfo(
                @"D:\Source Control\Prototype1\GIE-PST\PCT.Tests\Content\Certificates\00000000-0000-0000-0000-000000000000.pdf")
                .MoveTo(
                    @"D:\Source Control\Prototype1\GIE-PST\PCT.Tests\Content\Certificates\00000000-0000-0000-0000-000000000000.pdf"
                        .Replace(Guid.Empty.ToString(), id.ToString()));

            foreach(var licensure in account.StateLicensures.Where(s => courseProgress.Course.StateCEUs.Any(ceu => ceu.StateAbbr == s.StateAbbr)))
                new FileInfo(
                    @"D:\Source Control\Prototype1\GIE-PST\PCT.Tests\Content\Certificates\00000000-0000-0000-0000-000000000000_state_" + licensure.ID + ".jpg")
                    .MoveTo(
                        (@"D:\Source Control\Prototype1\GIE-PST\PCT.Tests\Content\Certificates\00000000-0000-0000-0000-000000000000_state_" + licensure.ID + ".jpg")
                            .Replace(Guid.Empty.ToString(), id.ToString()));

            new FileInfo(
                @"D:\Source Control\Prototype1\GIE-PST\PCT.Tests\Content\Certificates\00000000-0000-0000-0000-000000000000_user.jpg")
                .MoveTo(
                    @"D:\Source Control\Prototype1\GIE-PST\PCT.Tests\Content\Certificates\00000000-0000-0000-0000-000000000000_user.jpg"
                        .Replace(Guid.Empty.ToString(), id.ToString()));
        }