Пример #1
0
 public void AddOutline(PDFOutline outline)
 {
     outline.Parent = this;
     if (_outlines.Count > 0)
     {
         outline.PrevOutline = _outlines.Last();
         _outlines.Last().NextOutline = outline;
     }
     _outlines.Add(outline);
 }
Пример #2
0
        /// <summary>
        /// Creates the pages contained inside the PDF.
        /// </summary>
        /// 
        /// <remarks>
        /// This method contains the main PDF algorithm.
        /// It splits DataSet rows into several PDF pages.
        /// 
        /// <code>
        ///    ----------------------------
        ///    | Column 1 | Column 2 | ...
        /// y  ----------------------------
        /// ^  | Row 10   | Row 11   | ...
        /// |  | Row 20   | Row 21   | ...
        /// |  | Row 30   | Row 31   | ...
        /// |  ----------------------------
        /// 0 ----> x
        /// </code>
        /// </remarks>
        /// 
        /// <param name="data">DataSet.</param>
        /// <param name="doc">Main PDF document.</param>
        /// <param name="outlines">PDF outlines so we can add the page to the "bookmarks"/outlines.</param>
        /// <returns>The PDF pages (a list of PDFPage).</returns>
        public static PDFPages CreatePages(DataSet data, PDFDocument doc, PDFOutlines outlines)
        {
            PDFPages pages = new PDFPages();

            foreach (DataTable table in data.Tables)
            {
                PDFOutline currentOutline = null;
                if (data.Tables.Count > 1)
                {
                    // More than 1 DataTable thus lets create outlines
                    currentOutline = new PDFOutline(table.TableName);
                    doc.AddChild(currentOutline);
                    outlines.AddOutline(currentOutline);
                }

                List<PDFGraphicObject> rows = new List<PDFGraphicObject>();

                double yPos = -Table.RowHeight;

                double tableWidth = Table.GetTableWidth(table);

                // Scaling
                double scaling = (PageLayout.Width - (PageLayout.RightMargin + PageLayout.LeftMargin)) / tableWidth;
                if (scaling > 1)
                {
                    scaling = 1;
                }
                ////

                // Page title
                // FIXME this is hardcoded
                List<string> title = new List<string>();
                title.Add("TITLE");
                title.Add("title");
                title.Add(table.TableName);
                double titleHeight = title.Count * Table.RowHeight * scaling;
                ////

                for (int row = 0; row < table.Rows.Count; row++)
                {
                    double totalTableWidth = 0;

                    for (int col = 0; col < table.Columns.Count; col++)
                    {
                        double pageHeightLimit = PageLayout.Height - PageLayout.BottomMargin - PageLayout.TopMargin - titleHeight;

                        // Detects end of page
                        bool endOfPage = -(yPos - Table.RowHeight) * scaling >= pageHeightLimit;
                        if (endOfPage)
                        {
                            // Creates the page
                            List<PDFGraphicObject> columns = Table.CreateColumns(table);
                            PDFPage page = CreatePage(doc, tableWidth, columns, rows, title);
                            pages.AddPage(page);
                            ////

                            // Add the page to the outline
                            if (currentOutline != null)
                            {
                                if (currentOutline.Page == null)
                                {
                                    currentOutline.Page = page;
                                }
                            }
                            ////

                            // Don't do a Clear() on the list, instead
                            // creates a new copy of the list otherwise
                            // objects referencing this list won't have their own copy
                            rows = new List<PDFGraphicObject>();

                            yPos = -Table.RowHeight;
                        }
                        ////

                        // Create a row
                        string rowName = table.Rows[row][col].ToString();
                        PDFTextBox text = Table.CreateRow(rowName, yPos);
                        PDFTranslation translation = new PDFTranslation(text, totalTableWidth, 0);
                        rows.Add(translation);
                        ////

                        DataColumn column = table.Columns[col];
                        double columnWidth = Table.GetColumnWidth(column, table);
                        totalTableWidth += columnWidth + 2;
                    }

                    // Change Y position inside the coordinate system of PDF
                    yPos -= Table.RowHeight;
                }

                if (rows.Count > 0)
                {
                    // Creates the page
                    List<PDFGraphicObject> columns = Table.CreateColumns(table);
                    PDFPage page = CreatePage(doc, tableWidth, columns, rows, title);
                    pages.AddPage(page);
                    ////

                    // Add the page to the outlines
                    if (currentOutline != null)
                    {
                        if (currentOutline.Page == null)
                        {
                            currentOutline.Page = page;
                        }
                    }
                    ////
                }
            }

            return pages;
        }