Beispiel #1
0
        /// <summary>
        /// Called when a page is started.
        /// </summary>
        /// <param name="writer">Writer the <see cref="iTextSharp.text.pdf.PdfWriter"/> for this document</param>
        /// <param name="document">Current document</param>
        public override void OnStartPage(PdfWriter writer, Document document)
        {
            base.OnStartPage(writer, document);

            #region initialize
            var table    = TabularWriter.Provider.Input.Model.Table;
            var provider = TabularWriter.Provider;
            #endregion

            #region add logo
            document.AddLogo(table.Logo);
            #endregion

            #region sets vertical table position
            document.SetVerticalLocationFrom(table.Location);
            #endregion

            #region add top aggregates
            var tempTable = new PdfPTable(PdfTable.NumberOfColumns);
            tempTable.SetTotalWidth(PdfTable.AbsoluteWidths);
            tempTable.HorizontalAlignment = PdfTable.HorizontalAlignment;
            tempTable.LockedWidth         = table.AutoFitColumns == YesNo.Yes;

            if (writer.PageNumber == 1)
            {
                tempTable.AddAggregateByLocation(
                    table,
                    RawData,
                    provider.SpecialChars.ToArray(),
                    KnownAggregateLocation.Top);
            }
            #endregion

            #region add column headers
            tempTable.AddColumnHeaders(table);
            #endregion

            #region add field headers
            tempTable.AddFieldHeaders(table);
            #endregion

            #region add table to this document
            document.Add(tempTable);
            #endregion
        }
Beispiel #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
            }
        }