/// <summary>
        /// Demonstrate PDF Table Creations
        /// </summary>
        /// <returns></returns>
        private FileResult _DemonstrateTableCreation()
        {
            // Create new document
            Document pdfDocument = new Document();

            pdfDocument.RegistrationName = "demo";
            pdfDocument.RegistrationKey  = "demo";

            // Add page
            Bytescout.PDF.Page page = new Bytescout.PDF.Page(PaperFormat.A4);
            pdfDocument.Pages.Add(page);

            DeviceColor lightGrayColor = new ColorGray(200);
            DeviceColor whiteColor     = new ColorGray(255);
            DeviceColor lightBlueColor = new ColorRGB(200, 200, 250);
            DeviceColor lightRedColor  = new ColorRGB(255, 200, 200);

            // Create a table and set default background color
            Bytescout.PDF.Table table = new Bytescout.PDF.Table();
            table.BackgroundColor = lightGrayColor;

            // Add row headers column and set its color
            table.Columns.Add(new TableColumn("RowHeaders"));
            table.Columns[0].BackgroundColor = lightGrayColor;

            // Add columns A, B, C, ...
            for (int c = 0; c < 10; c++)
            {
                string columnName = Convert.ToChar('A' + c).ToString();
                table.Columns.Add(new TableColumn(columnName, columnName));
            }

            // Add rows
            for (int r = 0; r < 10; r++)
            {
                // Create new row and set its background color
                Bytescout.PDF.TableRow row = table.NewRow();
                row.BackgroundColor = whiteColor;

                // Set row header text
                row["RowHeaders"].Text = (r + 1).ToString();

                // Set cell text
                for (int c = 0; c < 10; c++)
                {
                    string columnName = Convert.ToChar('A' + c).ToString();
                    row[columnName].Text = columnName + (r + 1);
                }

                // Add the row to the table
                table.Rows.Add(row);
            }

            // Decorate the table
            table.Rows[1]["B"].BackgroundColor          = lightRedColor;
            table.Columns[2].BackgroundColor            = lightBlueColor;
            table.Rows[1].BackgroundColor               = lightBlueColor;
            table.Rows[1]["RowHeaders"].BackgroundColor = lightBlueColor;

            // Draw the table on canvas
            page.Canvas.DrawTable(table, 20, 20);

            // Save created PDF to memory stream
            MemoryStream memoryStream = new MemoryStream();

            pdfDocument.Save(memoryStream);

            // Stream back to beginning to allow the data to be read back out
            memoryStream.Seek(0, SeekOrigin.Begin);

            // Return result
            return(File(memoryStream, "text/pdf", "sample_PDFWithTable.pdf"));
        }
Exemple #2
0
        /// <summary>
        /// Handle Table Creation with PDF SDK
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnTableCreation_Click(object sender, EventArgs e)
        {
            try
            {
                // Create new document
                Document pdfDocument = new Document();
                pdfDocument.RegistrationName = "demo";
                pdfDocument.RegistrationKey  = "demo";

                // Add page
                Bytescout.PDF.Page page = new Bytescout.PDF.Page(PaperFormat.A4);
                pdfDocument.Pages.Add(page);

                DeviceColor lightGrayColor = new ColorGray(200);
                DeviceColor whiteColor     = new ColorGray(255);
                DeviceColor lightBlueColor = new ColorRGB(200, 200, 250);
                DeviceColor lightRedColor  = new ColorRGB(255, 200, 200);

                // Create a table and set default background color
                Bytescout.PDF.Table table = new Bytescout.PDF.Table();
                table.BackgroundColor = lightGrayColor;

                // Add row headers column and set its color
                table.Columns.Add(new TableColumn("RowHeaders"));
                table.Columns[0].BackgroundColor = lightGrayColor;

                // Add columns A, B, C, ...
                for (int c = 0; c < 10; c++)
                {
                    string columnName = Convert.ToChar('A' + c).ToString();
                    table.Columns.Add(new TableColumn(columnName, columnName));
                }

                // Add rows
                for (int r = 0; r < 10; r++)
                {
                    // Create new row and set its background color
                    Bytescout.PDF.TableRow row = table.NewRow();
                    row.BackgroundColor = whiteColor;

                    // Set row header text
                    row["RowHeaders"].Text = (r + 1).ToString();

                    // Set cell text
                    for (int c = 0; c < 10; c++)
                    {
                        string columnName = Convert.ToChar('A' + c).ToString();
                        row[columnName].Text = columnName + (r + 1);
                    }

                    // Add the row to the table
                    table.Rows.Add(row);
                }

                // Decorate the table
                table.Rows[1]["B"].BackgroundColor          = lightRedColor;
                table.Columns[2].BackgroundColor            = lightBlueColor;
                table.Rows[1].BackgroundColor               = lightBlueColor;
                table.Rows[1]["RowHeaders"].BackgroundColor = lightBlueColor;

                // Draw the table on canvas
                page.Canvas.DrawTable(table, 20, 20);

                // Save created PDF to memory stream
                MemoryStream memoryStream = new MemoryStream();
                pdfDocument.Save(memoryStream);

                // Perform download of file
                Response.Clear();
                Response.ClearHeaders();

                Response.AppendHeader("Content-Length", memoryStream.Length.ToString());
                Response.ContentType = "text/pdf";
                Response.AppendHeader("Content-Disposition", "attachment;filename=\"sample_PDFWithTable.pdf\"");

                Response.BinaryWrite(memoryStream.ToArray());
                Response.End();
            }
            catch (Exception ex)
            {
                lblTableCreation.Text = "Error: " + ex.Message;
            }
        }