public static PdfPTable DefineGridHeaderColumns(this PdfPTable @this, CellColumns cellsParams, params string[] columns)
        {
            if (columns.Length <= 0)
            {
                return(@this);
            }

            if (cellsParams == null)
            {
                cellsParams = new CellColumns();
            }

            var cells = new PdfPCell[columns.Length];

            for (int i = 0; i < columns.Length; i++)
            {
                cells[i] = new PdfPCell(new Paragraph(columns[i], Fonts.Bold09))
                {
                    HorizontalAlignment = cellsParams.HorizontalAlignment,
                    VerticalAlignment   = cellsParams.VerticalAlignment,
                    BackgroundColor     = cellsParams.BackgroundColor
                };
            }

            @this.AddCells(cells);
            return(@this);
        }
Exemple #2
0
        /// <inheritdoc />
        /// <summary>
        /// Generates output in Portable Document Format [ pdf ].
        /// </summary>
        protected override void Execute()
        {
            using (var stream = new MemoryStream())
            {
                using (var document = new Document())
                {
                    #region initialize
                    var fields = Table.Fields;
                    #endregion

                    #region get input data
                    var rows = Service.RawDataFiltered;
                    #endregion

                    #region add data
                    var cells       = new Collection <PdfPCell>();
                    var fieldsTable = new Dictionary <BaseDataFieldModel, int>();
                    foreach (var row in rows)
                    {
                        foreach (var field in fields)
                        {
                            field.DataSource = row;
                            var value       = field.Value.GetValue(Provider.SpecialChars);
                            var valueLenght = value.FormattedValue.Length;

                            if (!fieldsTable.ContainsKey(field))
                            {
                                fieldsTable.Add(field, valueLenght);
                            }
                            else
                            {
                                var entry = fieldsTable[field];
                                if (valueLenght > entry)
                                {
                                    fieldsTable[field] = valueLenght;
                                }
                            }

                            cells.Add(PdfHelper.CreateCell(value));
                        }
                    }
                    #endregion

                    #region create pdf document, sets document orientation and create an empty table
                    var writer = PdfWriter.GetInstance(document, stream);

                    var hostDocument = Host.Document;
                    var pageSize     = hostDocument.Size.ToPageSize();
                    if (hostDocument.Orientation == KnownDocumentOrientation.Landscape)
                    {
                        pageSize = pageSize.Rotate();
                    }

                    document.SetPageSize(pageSize);
                    document.SetMarginsFromModel(hostDocument.Margins);

                    var table = new PdfPTable(fields.Count);
                    table.SetHorizontalLocationFrom(Table.Location);
                    #endregion

                    #region autofitcolumns?
                    table.AutoFitColumns(fieldsTable, this);
                    #endregion

                    #region assign class to handle document events
                    writer.PageEvent = new PdfPageEvent(this, table, rows);
                    #endregion

                    #region opens document
                    document.Open();
                    #endregion

                    #region add cells to table
                    table.AddCells(cells);
                    #endregion

                    #region add bottom aggregate
                    table.AddAggregateByLocation(Table, rows, Provider.SpecialChars.ToArray(), KnownAggregateLocation.Bottom);
                    #endregion

                    #region add table to document
                    document.Add(table);
                    #endregion
                }

                #region save
                Result.Add(stream.ToArray());
                #endregion
            }
        }