示例#1
0
        public static bool Generate(string filename)
        {
            Document  document = new Document(PageSize.A4, 0f, 0f, 0f, 0f);
            PdfWriter writer   = PdfWriter.GetInstance(document, new FileStream(filename, FileMode.Create));

            document.Open();

            PdfContentByte canvas = writer.DirectContent;

            Font font = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10);

            string[] captions = { "ATM", "CASH", "WEB", "MOBILE" };
            //double[] values = { 2, 60, 20, 18 };
            double[] values = { 5, 60, 35, 0 };
            PieChart chart  = new PieChart(values, captions);

            canvas.DrawPieChart(chart, 110, 730, 100);
            canvas.DrawPieChart(chart, 410, 730, 60);

            string[] captions2 = { "CAPTION 1", "CAPTION 2", "CAPTION 3", "CAPTION 4", "CAPTION 5", "CAPTION 6", "CAPTION 7", "CAPTION 8", "CAPTION 9", "CAPTION 10" };
            double[] values2   = { 100, 80, 60, 40, 50, 60, 30, 0, 90, 40 };
            chart = new PieChart(values2, captions2);
            canvas.DrawPieChart(chart, 110, 520, 100, font);
            canvas.DrawPieChart(chart, 410, 520, 60);

            //string[] captions3 = { "CAPTION 1", "CAPTION 2", "CAPTION 3" };
            //double[] values3 = { 100, 80, 60 };
            //System.Drawing.Color[] colors = new System.Drawing.Color[] { System.Drawing.Color.Black,
            //    System.Drawing.Color.CornflowerBlue,
            //    System.Drawing.Color.Brown };

            //chart = new PieChart(values3, captions3, colors);
            //canvas.DrawPieChart(chart, 110, 300, 100, font);
            //canvas.DrawPieChart(chart, 410, 300, 60);

            string[]  captions4        = { "CAPTION 1", "CAPTION 2", "CAPTION 3", "CAPTION 4", "CAPTION 5" };
            double[]  values4          = { 100, 80, 60, 50, 50 };
            BaseColor pantome376       = new BaseColor(130, 197, 91);
            BaseColor pantone369       = new BaseColor(95, 182, 85);
            BaseColor pantonecool      = new BaseColor(88, 89, 91);
            BaseColor pantone447       = new BaseColor(67, 66, 61);
            BaseColor pantonecoolLight = new BaseColor(173, 216, 230);

            BaseColor[] colors4 = { pantome376, pantone369, pantonecool, pantone447, pantonecoolLight };
            chart = new PieChart(values4, captions4, colors4);
            canvas.DrawPieChart(chart, 110, 300, 100, font);
            canvas.DrawPieChart(chart, 410, 300, 60);

            document.Close();
            return(true);
        }
示例#2
0
        public byte[] Generate()
        {
            using (MemoryStream memStream = new MemoryStream())
            {
                Document  document = new Document(PageSize.A4, 25, 25, 30, 30);
                PdfWriter writer   = PdfWriter.GetInstance(document, memStream);
                document.Open();

                PdfPTable topSection = new PdfPTable(2);
                topSection.DefaultCell.Border = Rectangle.NO_BORDER;

                // Full Name
                var fullNamePhrase = new Phrase();
                topSection.DefaultCell.Border = Rectangle.NO_BORDER;
                fullNamePhrase.Add(new Chunk("Name: ", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 16)));
                fullNamePhrase.Add(new Chunk(FullName, FontFactory.GetFont(FontFactory.HELVETICA, 16)));
                PdfPCell fullNameCell = new PdfPCell(fullNamePhrase);
                fullNameCell.Border = Rectangle.NO_BORDER;
                topSection.AddCell(fullNameCell);

                // Base Salary
                var annualSalaryPhrase = new Phrase();
                annualSalaryPhrase.Add(new Chunk("Base Salary: ", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 16)));
                annualSalaryPhrase.Add(new Chunk("$" + AnnualSalary.ToString("f2"), FontFactory.GetFont(FontFactory.HELVETICA, 16)));
                PdfPCell baseSalaryCell = new PdfPCell(annualSalaryPhrase);
                baseSalaryCell.Border = Rectangle.NO_BORDER;
                topSection.AddCell(baseSalaryCell);

                document.Add(topSection);

                PdfPTable dependentSection = new PdfPTable(1);
                dependentSection.SpacingBefore      = 20f;
                dependentSection.DefaultCell.Border = Rectangle.NO_BORDER;
                PdfPCell headerLabel = new PdfPCell(new Phrase(new Chunk("Dependents", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 16))));
                headerLabel.Border = Rectangle.NO_BORDER;
                dependentSection.AddCell(headerLabel);

                foreach (var dependent in Dependents.Where(x => !string.IsNullOrEmpty(x)))
                {
                    PdfPCell dependentCell = new PdfPCell(new Phrase(new Chunk("-" + dependent, FontFactory.GetFont(FontFactory.HELVETICA, 14))));
                    dependentCell.Border = Rectangle.NO_BORDER;
                    dependentSection.AddCell(dependentCell);
                }

                document.Add(dependentSection);

                PdfPTable bottomSection = new PdfPTable(1);
                bottomSection.SpacingBefore      = 40f;
                bottomSection.DefaultCell.Border = Rectangle.NO_BORDER;

                PdfPTable detailsTable = new PdfPTable(2);
                detailsTable.DefaultCell.Border = Rectangle.NO_BORDER;
                PdfPCell discountHeaderCell = new PdfPCell(new Phrase(new Chunk("Dependents", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 14))));
                discountHeaderCell.Colspan = 2;
                detailsTable.AddCell(discountHeaderCell);

                foreach (var discount in Discounts)
                {
                    PdfPCell discountItemCell = new PdfPCell(new Phrase(new Chunk(discount, FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 12))));
                    discountItemCell.Colspan = 2;
                    detailsTable.AddCell(discountItemCell);
                }

                detailsTable.AddCell(new PdfPCell(new Phrase(new Chunk("Total: ", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 14)))));
                detailsTable.AddCell(new PdfPCell(new Phrase(new Chunk(Total.ToString("f2"), FontFactory.GetFont(FontFactory.HELVETICA, 14)))));
                detailsTable.AddCell(new PdfPCell(new Phrase(new Chunk("Total Per Check: ", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 14)))));
                detailsTable.AddCell(new PdfPCell(new Phrase(new Chunk(TotalPerCheck.ToString("f2"), FontFactory.GetFont(FontFactory.HELVETICA, 14)))));
                bottomSection.AddCell(new PdfPCell(detailsTable));
                document.Add(bottomSection);

                PdfContentByte canvas   = writer.DirectContent;
                PieChart       pieChart = new PieChart(new double[] { Total, TotalEmployeeBenefitCost, TotalDependentsBenefitCost },
                                                       new[] { "Total After Benefits", "Employee Benefits", "Employee Dependent Benefits" });

                canvas.DrawPieChart(pieChart, 300, 300, 100);

                document.Close();
                return(memStream.ToArray());
            }
        }