Example #1
0
        public Task <byte[]> GenerateTransactionHistory(TransactionHistoryPdf transactionHistoryPdf)
        {
            var output    = new MemoryStream();
            var iccStream = new FileStream(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, _imageColorMatchingFilename), FileMode.Open, FileAccess.Read);
            var pdfFont   = PdfFontFactory.CreateFont(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, _trueTypeFontFilename), PdfEncodings.IDENTITY_H, true);

            try
            {
                var      intent   = new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", iccStream);
                var      pdf      = new PdfADocument(new PdfWriter(output), PdfAConformanceLevel.PDF_A_2B, intent);
                Document document = new Document(pdf);

                if (transactionHistoryPdf.Columns.Any())
                {
                    var table = new Table(6, false);
                    table.AddCell(CreateRootCell(transactionHistoryPdf.Rows.Pid).SetFont(pdfFont));
                    table.AddCell(CreateRootCell(transactionHistoryPdf.Rows.TypeOfObject).SetFont(pdfFont));
                    table.AddCell(CreateRootCell(transactionHistoryPdf.Rows.TypeOfChanges).SetFont(pdfFont));
                    table.AddCell(CreateRootCell(transactionHistoryPdf.Rows.Descriptions).SetFont(pdfFont));
                    table.AddCell(CreateRootCell(transactionHistoryPdf.Rows.Author).SetFont(pdfFont));
                    table.AddCell(CreateRootCell(transactionHistoryPdf.Rows.Date).SetFont(pdfFont));

                    foreach (var column in transactionHistoryPdf.Columns)
                    {
                        table.AddCell(CreateCell(column.Pid).SetFont(pdfFont));
                        table.AddCell(CreateCell(column.TypeOfObject).SetFont(pdfFont));
                        table.AddCell(CreateCell(column.TypeOfChanges).SetFont(pdfFont));
                        table.AddCell(CreateCell(column.Descriptions, TextAlignment.LEFT).SetFont(pdfFont));
                        table.AddCell(CreateCell(column.Author, TextAlignment.LEFT).SetFont(pdfFont));
                        table.AddCell(CreateCell(column.Date, TextAlignment.LEFT).SetFont(pdfFont));
                    }

                    document.Add(table);
                    document.Add(new AreaBreak(AreaBreakType.NEXT_PAGE));
                }

                document.Add(CreateNewLine().SetFont(pdfFont));
                document.Add(CreateLineSeparator().SetFont(pdfFont));
                document.Add(CreateTextAlignment(transactionHistoryPdf.Name).SetFont(pdfFont));
                document.Add(CreateTextAlignment(transactionHistoryPdf.Originator).SetFont(pdfFont));
                document.Add(CreateTextAlignment(transactionHistoryPdf.Address).SetFont(pdfFont));
                document.Add(CreateTextAlignment(transactionHistoryPdf.SerialNumber).SetFont(pdfFont));
                document.Add(CreateTextAlignment($"{transactionHistoryPdf.NumberOfPages}{pdf.GetNumberOfPages()}").SetFont(pdfFont));

                var numberOfPages = pdf.GetNumberOfPages();

                pdf.MovePage(numberOfPages, 1);

                for (int pageNum = 1; pageNum <= numberOfPages; pageNum++)
                {
                    Rectangle pageSize = pdf.GetPage(pageNum).GetPageSize();

                    if (pageNum != 1)
                    {
                        document.ShowTextAligned(CreateTextAlignment(transactionHistoryPdf.Header, 7).SetFont(pdfFont), pageSize.GetLeft() + 35, pageSize.GetTop() - 13, pageNum, TextAlignment.LEFT, VerticalAlignment.TOP, 0);
                    }

                    document.ShowTextAligned(CreateText(pageNum + " / " + numberOfPages, 7).SetFont(pdfFont), pageSize.GetWidth() / 2, pageSize.GetBottom() + 15, pageNum, TextAlignment.CENTER, VerticalAlignment.BOTTOM, 0);
                }

                document.Close();

                return(Task.FromResult(output.ToArray()));
            }
            finally
            {
                iccStream.Dispose();
                output.Dispose();
            }
        }