Ejemplo n.º 1
0
        private void ProcessStyledTable(PdfPage page, Com.Delta.Print.Engine.StyledTable table)
        {
            try
            {
                DataTable data = table.DataSource == null ? table.DisplayData : table.Data;

                int visibleColumnCount = table.GetVisibleColumnsCount();

                if (data == null)
                {
                    data = new DataTable();
                    for (int i = 0; i < visibleColumnCount; i++)
                    {
                        data.Columns.Add();
                    }
                }


                if (data.Rows.Count == 0)
                {
                    if (table.DrawEmptyRows)
                    {
                        int      maxRows  = table.Bounds.Height / table.CellHeight;
                        PdfTable pdfTable = pdfDocument.NewTable(table.DataFont, maxRows, visibleColumnCount, 2);

                        System.Data.DataTable dt = new System.Data.DataTable();
                        for (int i = 0; i < visibleColumnCount; i++)
                        {
                            dt.Columns.Add("");
                        }

                        for (int i = 0; i < table.Height / table.CellHeight; i++)
                        {
                            dt.Rows.Add(dt.NewRow());
                            if (dt.Columns.Count > 0)
                            {
                                dt.Rows[i][0] = " ";
                            }
                        }

                        pdfTable.ImportDataTable(dt);

                        pdfTable.SetRowHeight(Convert(table.CellHeight));

                        pdfTable.HeadersRow.SetRowHeight(Convert(table.CellHeight));
                        pdfTable.HeadersRow.SetColors(table.HeaderFontColor, table.HeaderBackgroundColor);
                        pdfTable.HeadersRow.SetFont(table.HeaderFont);

                        int count = 0;
                        for (int i = 0; i < table.Columns.Length; i++)
                        {
                            if (table.Columns[i].Width > 0)
                            {
                                string columnName = table.Columns[i].Label == String.Empty || table.Columns[i].Label == "" ? " " : table.Columns[i].Label;
                                pdfTable.HeadersRow[count].SetContent(columnName);
                                count++;
                            }
                        }

                        pdfTable.SetColors(table.DataFontColor, table.BackgroundColor);
                        pdfTable.SetBorders(table.BorderColor, 1, BorderType.CompleteGrid);

                        if (table.AlternateBackColor)
                        {
                            for (int i = 0; i < dt.Rows.Count; i++)
                            {
                                if (i % 2 == 1)
                                {
                                    pdfTable.Rows[i].SetBackgroundColor(table.AlternatingBackColor);
                                }
                            }
                        }


                        int[] columnWidths = new int[visibleColumnCount];
                        int   tableWidth   = 0;
                        count = 0;
                        for (int i = 0; i < table.Columns.Length; i++)
                        {
                            if (table.Columns[i].Width > 0)
                            {
                                columnWidths[count] = (int)Convert(table.Columns[i].Width);
                                tableWidth         += columnWidths[count];
                                count++;
                            }
                        }
                        pdfTable.SetColumnsWidth(columnWidths);

                        count = 0;
                        for (int i = 0; i < table.Columns.Length; i++)
                        {
                            if (table.Columns[i].Width > 0)
                            {
                                Com.Delta.Print.Engine.Pdf.HorizontalAlignment columnAlignment = GetColumnContentAlignment(table.Columns[i].Alignment);
                                pdfTable.HeadersRow[count].SetContentAlignment(columnAlignment, VerticalAlignment.Middle);
                                pdfTable.Columns[count].SetContentAlignment(columnAlignment, VerticalAlignment.Middle);

                                count++;
                            }
                        }

                        pdfTable.VisibleHeaders = table.DrawHeader;
                        PdfTablePage tablePage = pdfTable.CreateTablePage(new PdfArea(pdfDocument, Convert(table.Bounds.X), Convert(table.Bounds.Y), tableWidth, Convert(table.Bounds.Height)));
                        page.Add(tablePage);
                    }
                    else if (table.DrawHeader && !table.DrawEmptyRows)
                    {
                        int position = table.Bounds.X;

                        int headerRelativeHeight = 1;
                        for (int i = 0; i < table.Columns.Length; i++)
                        {
                            if (table.Columns[i].Width > 0)
                            {
                                PdfArea area = new PdfArea(pdfDocument, Convert(position + 2), Convert(table.Bounds.Y + 2), Convert(table.Columns[i].Width - 4), Convert(table.CellHeight - 4));

                                PdfTextArea pdfTextArea  = new PdfTextArea(table.HeaderFont, table.HeaderFontColor, area, GetColumnContentAlignment(table.Columns[i].Alignment), VerticalAlignment.Middle, table.Columns[i].Label, false);
                                int         minimumLines = pdfTextArea.RenderLines().Count;
                                headerRelativeHeight = Math.Max(headerRelativeHeight, minimumLines);
                            }
                        }

                        int headerHeight = table.CellHeight * headerRelativeHeight;

                        for (int i = 0; i < table.Columns.Length; i++)
                        {
                            if (table.Columns[i].Width > 0)
                            {
                                PdfArea area       = new PdfArea(pdfDocument, Convert(position + 2), Convert(table.Bounds.Y + 2), Convert(table.Columns[i].Width - 4), Convert(headerHeight - 4));
                                PdfArea borderArea = new PdfArea(pdfDocument, Convert(position), Convert(table.Bounds.Y), Convert(table.Columns[i].Width), Convert(headerHeight));

                                PdfRectangle border = new PdfRectangle(pdfDocument, borderArea, table.BorderColor, 1, table.HeaderBackgroundColor);
                                page.Add(border);

                                PdfTextArea pdfTextArea = new PdfTextArea(table.HeaderFont, table.HeaderFontColor, area, GetColumnContentAlignment(table.Columns[i].Alignment), VerticalAlignment.Middle, table.Columns[i].Label, false);

                                // arbitrary line spacing
                                pdfTextArea.SetLineSpacing(Convert(table.CellHeight / 8));
                                page.Add(pdfTextArea);

                                position += table.Columns[i].Width;
                            }
                        }
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    int maxRows  = table.Height / table.CellHeight;
                    int rowCount = data.Rows.Count;

                    PdfTable pdfTable;

                    double cellPadding = Math.Max(0, Math.Min(2, Math.Floor(0.5 * (table.CellHeight - table.DataFont.GetHeight()))));

                    if (table.DrawEmptyRows)
                    {
                        pdfTable = pdfDocument.NewTable(table.DataFont, rowCount + maxRows, visibleColumnCount, cellPadding);
                    }
                    else
                    {
                        pdfTable = pdfDocument.NewTable(table.DataFont, rowCount, visibleColumnCount, cellPadding);
                    }

                    pdfTable.pdfPage = page;

                    System.Data.DataTable dt = new System.Data.DataTable();
                    for (int i = 0; i < table.Columns.Length; i++)
                    {
                        if (table.Columns[i].Width > 0)
                        {
                            if (table.Columns[i].FormatMask == "Image")
                            {
                                dt.Columns.Add("", typeof(byte[]));
                            }
                            else
                            {
                                dt.Columns.Add("", typeof(System.String));
                            }
                        }
                    }

                    for (int i = 0; i < data.Rows.Count; i++)
                    {
                        if (data.Rows[i].RowError == "Subtotal")
                        {
                            object[] rowData = new object[visibleColumnCount];
                            int      cnt     = 0;
                            for (int j = 0; j < table.Columns.Length; j++)
                            {
                                if (table.Columns[j].Width > 0)
                                {
                                    rowData[cnt] = table.Subtotals[j];
                                    cnt++;
                                }
                            }

                            dt.Rows.Add(rowData);
                        }
                        else
                        {
                            object[] rowData = new object[visibleColumnCount];
                            int      cnt     = 0;
                            for (int j = 0; j < table.Columns.Length; j++)
                            {
                                if (table.Columns[j].Width > 0)
                                {
                                    if (table.Columns[j].FormatMask == null || table.Columns[j].FormatMask == String.Empty)
                                    {
                                        rowData[cnt] = String.Format("{0}", data.Rows[i][j]);
                                    }
                                    else if (table.Columns[j].FormatMask == "Image")
                                    {
                                        rowData[cnt] = data.Rows[i][j] is byte[] ? data.Rows[i][j] : null;
                                    }
                                    else
                                    {
                                        rowData[cnt] = String.Format("{0:" + table.Columns[j].FormatMask + "}", data.Rows[i][j]);
                                    }
                                    cnt++;
                                }
                            }
                            dt.Rows.Add(rowData);
                        }
                    }

                    if (table.DrawEmptyRows)
                    {
                        for (int i = 0; i < table.Height / table.CellHeight; i++)
                        {
                            dt.Rows.Add(dt.NewRow());
                        }
                    }

                    pdfTable.ImportDataTable(dt);


                    pdfTable.SetRowHeight(Convert(table.CellHeight));

                    pdfTable.VisibleHeaders = table.DrawHeader;
                    pdfTable.HeadersRow.SetRowHeight(Convert(table.CellHeight));
                    pdfTable.HeadersRow.SetColors(table.HeaderFontColor, table.HeaderBackgroundColor);
                    pdfTable.HeadersRow.SetFont(table.HeaderFont);

                    pdfTable.SetColors(table.DataFontColor, table.BackgroundColor);
                    pdfTable.SetBorders(table.BorderColor, 1, BorderType.CompleteGrid);
                    //pdfTable.SetBorders(table.BorderColor, (table.BorderColor == Color.Transparent ? 0 : 1), BorderType.CompleteGrid);


                    int[] columnWidths = new int[visibleColumnCount];
                    int   tableWidth   = 0;
                    int   count        = 0;
                    for (int i = 0; i < table.Columns.Length; i++)
                    {
                        if (table.Columns[i].Width > 0)
                        {
                            columnWidths[count] = (int)Convert(table.Columns[i].Width);
                            tableWidth         += columnWidths[count];

                            string columnName = table.Columns[i].Label == String.Empty || table.Columns[i].Label == "" ? " " : table.Columns[i].Label;
                            pdfTable.HeadersRow[count].SetContent(columnName);

                            Com.Delta.Print.Engine.Pdf.HorizontalAlignment columnAlignment = GetColumnContentAlignment(table.Columns[i].Alignment);
                            pdfTable.HeadersRow[count].SetContentAlignment(columnAlignment, VerticalAlignment.Middle);
                            pdfTable.Columns[count].SetContentAlignment(columnAlignment, VerticalAlignment.Middle);

                            count++;
                        }
                    }
                    pdfTable.SetColumnsWidth(columnWidths);


                    ArrayList alterRows = table.AlterRows;
                    for (int i = 0; i < data.Rows.Count; i++)
                    {
                        if (table.AlternateBackColor && i % 2 == 1)
                        {
                            pdfTable.Rows[i].SetBackgroundColor(table.AlternatingBackColor);
                        }

                        if (alterRows != null && alterRows.Count > 0)
                        {
                            if (alterRows.Contains(data.Rows[i]) && i < pdfTable.Rows.Length)
                            {
                                pdfTable.Rows[i].SetForegroundColor(table.AlterDataColor);
                                pdfTable.Rows[i].SetBackgroundColor(table.AlterDataBackColor);
                            }
                        }
                    }


                    if (table.DrawEmptyRows && table.AlternateBackColor)
                    {
                        for (int i = data.Rows.Count; i < table.Height / table.CellHeight; i++)
                        {
                            if (i % 2 == 1)
                            {
                                pdfTable.Rows[i].SetBackgroundColor(table.AlternatingBackColor);
                            }
                        }
                    }



                    if (table.HasSubtotals)
                    {
                        for (int i = 0; i < data.Rows.Count; i++)
                        {
                            if (data.Rows[i].RowError == "Subtotal" && i < pdfTable.Rows.Length)
                            {
                                pdfTable.Rows[i].SetForegroundColor(table.SubtotalsColor);
                            }
                        }
                    }

                    PdfTablePage tablePage = pdfTable.CreateTablePage(new PdfArea(pdfDocument, Convert(table.Bounds.X), Convert(table.Bounds.Y), tableWidth, Convert(table.Bounds.Height)));

                    page.Add(tablePage);

                    foreach (PdfImage image in pdfTable.Images.Keys)
                    {
                        RectangleF area = (RectangleF)pdfTable.Images[image];
                        page.Add(image, area.X, area.Y, 0.72 * area.Width, 0.72 * area.Height);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }
        }