Ejemplo n.º 1
0
        /// <summary>
        /// Main function: gets the PDF given a DataSet.
        /// </summary>
        /// <param name="data">DataSet to convert into a PDF.</param>
        /// <returns>The PDF.</returns>
        public static string GetPDF(DataSet data)
        {
            // Root
            PDFDocument doc = new PDFDocument();
            ////

            // Info
            PDFInfo info = new PDFInfo("Report", "PDFWR", "PDFWR");
            doc.Info = info;
            doc.AddChild(info);
            ////

            // Fonts
            foreach (PDFFont font in Fonts)
            {
                doc.AddChild(font);
            }
            ////

            // Outlines
            PDFOutlines outlines = new PDFOutlines();
            doc.AddChild(outlines);
            ////

            // Pages
            PDFPages pages = Page.CreatePages(data, doc, outlines);
            doc.AddChild(pages);
            ////

            // Add headers and footers
            int count = 1;
            foreach (PDFPage page in pages.Pages)
            {
                List<PDFGraphicObject> header = Page.CreateHeader();
                page.ContentStream.AddRange(header);

                List<PDFGraphicObject> footer = Page.CreateFooter(count, pages.Pages.Count);
                page.ContentStream.AddRange(footer);

                count++;
            }
            ////

            // Catalog
            PDFCatalog catalog = new PDFCatalog(outlines, pages);
            doc.Catalog = catalog;
            doc.AddChild(catalog);
            ////

            return doc.ToInnerPDF();
        }
Ejemplo n.º 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;
        }