Export Table to Pdf

3 posts, 0 answers
  1. Payal
    Payal avatar
    2 posts
    Member since:
    Aug 2014

    Posted 11 Aug 2014 Link to this post

    Hi,

    I have created one demo project for my wpf application in which i want to create table into Pdf 
    and i have added some columns in the table.but i have observed that as i added more columns int it 
    will throws an exception of "object reference not set to an instance" in function prepare 
    document() at line   "document.Measure
    (Telerik.Windows.Documents.Model.RadDocument.MAX_DOCUMENT_SIZE);."
    but it will successfully added table with column if we added less column into table and i want to 
    add more than 50 column in the table.
    so please give solution for it by which i can add no of column in table of Pdf.


    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using Telerik.Windows.Documents.FormatProviders.Pdf;
    using Telerik.Windows.Documents.Model;

    namespace ExportTableToPdfDemo
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                ExecuteExportSingleChartToPdf();
            }

            private void ExecuteExportSingleChartToPdf()
            {
                PdfFormatProvider provider = new PdfFormatProvider();
                Microsoft.Win32.SaveFileDialog saveDialog = new Microsoft.Win32.SaveFileDialog();
                saveDialog.FileName = "Company ";
                saveDialog.DefaultExt = ".pdf";
                saveDialog.Filter = "Documents|*.pdf";

                bool? dialogResult = saveDialog.ShowDialog();
                if (dialogResult == true)
                {
                    using (Stream output = saveDialog.OpenFile())
                    {
                        byte[] buffer = Export();
                        output.Write(buffer, 0, buffer.Length);
                        System.Windows.MessageBox.Show("Export Successful" + "\n" + saveDialog.FileName);
                    }
                }
            }

            public byte[] Export()
            {
                Telerik.Windows.Documents.Model.RadDocument document = CreateDocument();
                this.PrepareDocument(document);
                PdfFormatProvider docx = new PdfFormatProvider();
                return docx.Export(document);
            }
            private void PrepareDocument(RadDocument document)
            {
              
                document.LayoutMode = Telerik.Windows.Documents.Model.DocumentLayoutMode.Paged;
                document.Measure(Telerik.Windows.Documents.Model.RadDocument.MAX_DOCUMENT_SIZE);
                document.Arrange(new Telerik.Windows.Documents.Model.RectangleF(Telerik.Windows.Documents.Model.PointF.Empty, document.DesiredSize));
            }
      
            private void AddCellValue(TableCell cell, string value)
            {
                Telerik.Windows.Documents.Model.Paragraph paragraph = new Telerik.Windows.Documents.Model.Paragraph();
                cell.Blocks.Add(paragraph);
                Telerik.Windows.Documents.Model.Span span = new Telerik.Windows.Documents.Model.Span();
                span.Text = value;
                span.FontSize = 10;
                paragraph.Inlines.Add(span);
            }   
            private RadDocument CreateDocument()
            {
                RadDocument document = new RadDocument();
                CreateChartDocumentPart(document);
                return document;
            }
            private void CreateChartDocumentPart(RadDocument document)
            {
                Telerik.Windows.Documents.Model.Section section = new Telerik.Windows.Documents.Model.Section();

                #region TABLE
                Telerik.Windows.Documents.Model.Table table = new Telerik.Windows.Documents.Model.Table();
                Telerik.Windows.Documents.Model.TableRow row1 = new Telerik.Windows.Documents.Model.TableRow();
                Telerik.Windows.Documents.Model.TableCell cell1 = new Telerik.Windows.Documents.Model.TableCell();
                AddCellValue(cell1, "Market");
                row1.Cells.Add(cell1);

                Telerik.Windows.Documents.Model.TableCell cell2 = new Telerik.Windows.Documents.Model.TableCell();
                AddCellValue(cell2, "Source");
                row1.Cells.Add(cell2);

                Telerik.Windows.Documents.Model.TableCell cell3 = new Telerik.Windows.Documents.Model.TableCell();
                AddCellValue(cell3, "Profitability");
                row1.Cells.Add(cell3);

                Telerik.Windows.Documents.Model.TableCell cell4 = new Telerik.Windows.Documents.Model.TableCell();
                AddCellValue(cell4, "Source");
                row1.Cells.Add(cell4);
                table.Rows.Add(row1);

                Telerik.Windows.Documents.Model.TableRow row2 = new Telerik.Windows.Documents.Model.TableRow();
                Telerik.Windows.Documents.Model.TableCell cell5 = new Telerik.Windows.Documents.Model.TableCell();
                AddCellValue(cell5, "Market");
                row2.Cells.Add(cell5);

                Telerik.Windows.Documents.Model.TableCell cell7 = new Telerik.Windows.Documents.Model.TableCell();
                AddCellValue(cell7, "ReportedROE");
                row2.Cells.Add(cell7);
                table.Rows.Add(row2);

                Telerik.Windows.Documents.Model.TableRow row3 = new Telerik.Windows.Documents.Model.TableRow();
                Telerik.Windows.Documents.Model.TableCell cell9 = new Telerik.Windows.Documents.Model.TableCell();
                AddCellValue(cell9, "Latest Fiscal Year");
                row3.Cells.Add(cell9);

                Telerik.Windows.Documents.Model.TableCell cell11 = new Telerik.Windows.Documents.Model.TableCell();
                AddCellValue(cell11, "ReportedROE");
                row3.Cells.Add(cell11);
                table.Rows.Add(row3);

                Telerik.Windows.Documents.Model.TableRow row4 = new Telerik.Windows.Documents.Model.TableRow();
                Telerik.Windows.Documents.Model.TableCell cellA1 = new Telerik.Windows.Documents.Model.TableCell();
                AddCellValue(cellA1, "LTM as of");
                row4.Cells.Add(cellA1);

                Telerik.Windows.Documents.Model.TableCell cellA2 = new Telerik.Windows.Documents.Model.TableCell();
                AddCellValue(cellA2, "0.00");
                row4.Cells.Add(cellA2);

                Telerik.Windows.Documents.Model.TableCell cellA3 = new Telerik.Windows.Documents.Model.TableCell();
                AddCellValue(cellA3, "Forecast ROE");
                row4.Cells.Add(cellA3);
                table.Rows.Add(row4);

                Telerik.Windows.Documents.Model.TableRow row5 = new Telerik.Windows.Documents.Model.TableRow();
                Telerik.Windows.Documents.Model.TableCell cellB1 = new Telerik.Windows.Documents.Model.TableCell();
                AddCellValue(cellB1, "52 Week High");
                row5.Cells.Add(cellB1);

                Telerik.Windows.Documents.Model.TableCell cellB3 = new Telerik.Windows.Documents.Model.TableCell();
                AddCellValue(cellB3, "Net Margin (Trailing) (%)");
                row5.Cells.Add(cellB3);
                table.Rows.Add(row5);

                Telerik.Windows.Documents.Model.TableRow row6 = new Telerik.Windows.Documents.Model.TableRow();
                Telerik.Windows.Documents.Model.TableCell cellC1 = new Telerik.Windows.Documents.Model.TableCell();
                AddCellValue(cellC1, "52 Week Low");
                row6.Cells.Add(cellC1);

                Telerik.Windows.Documents.Model.TableCell cellC3 = new Telerik.Windows.Documents.Model.TableCell();
                AddCellValue(cellC3, "Net Margin (F'Cast) (%)");
                row6.Cells.Add(cellC3);
                table.Rows.Add(row6);

                Telerik.Windows.Documents.Model.TableRow row7 = new Telerik.Windows.Documents.Model.TableRow();
                Telerik.Windows.Documents.Model.TableCell cellD1 = new Telerik.Windows.Documents.Model.TableCell();
                AddCellValue(cellD1, "Daily Volume(mn)");
                row7.Cells.Add(cellD1);

                Telerik.Windows.Documents.Model.TableRow row13 = new Telerik.Windows.Documents.Model.TableRow();
                Telerik.Windows.Documents.Model.TableCell cellJ3 = new Telerik.Windows.Documents.Model.TableCell();
                AddCellValue(cellJ3, "Current Ratio");
                row13.Cells.Add(cellJ3);
                table.Rows.Add(row13);

                Telerik.Windows.Documents.Model.TableRow row14 = new Telerik.Windows.Documents.Model.TableRow();
                Telerik.Windows.Documents.Model.TableCell cellK1 = new Telerik.Windows.Documents.Model.TableCell();
                AddCellValue(cellK1, "Avg Traded Value(3M)");
                row13.Cells.Add(cellK1);

                Telerik.Windows.Documents.Model.TableCell cellK3 = new Telerik.Windows.Documents.Model.TableCell();
                AddCellValue(cellK3, "Quick Ratio");
                row13.Cells.Add(cellK3);
                table.Rows.Add(row14);

                Telerik.Windows.Documents.Model.TableRow row15 = new Telerik.Windows.Documents.Model.TableRow();
                Telerik.Windows.Documents.Model.TableCell cellL1 = new Telerik.Windows.Documents.Model.TableCell();
                AddCellValue(cellL1, "Volution");
                row15.Cells.Add(cellL1);

                Telerik.Windows.Documents.Model.TableCell cellL3 = new Telerik.Windows.Documents.Model.TableCell();
                AddCellValue(cellL3, "Debt to Asset");
                row15.Cells.Add(cellL3);
                table.Rows.Add(row15);

                Telerik.Windows.Documents.Model.TableRow row16 = new Telerik.Windows.Documents.Model.TableRow();
                Telerik.Windows.Documents.Model.TableCell cellM1 = new Telerik.Windows.Documents.Model.TableCell();
                AddCellValue(cellM1, "Divident Yield");
                row16.Cells.Add(cellM1);

                Telerik.Windows.Documents.Model.TableCell cellM3 = new Telerik.Windows.Documents.Model.TableCell();
                AddCellValue(cellM3, "Tot Debt to Common Equity");
                row16.Cells.Add(cellM3);
                table.Rows.Add(row16);

                Telerik.Windows.Documents.Model.TableRow row19 = new Telerik.Windows.Documents.Model.TableRow();
                Telerik.Windows.Documents.Model.TableCell cellP1 = new Telerik.Windows.Documents.Model.TableCell();
                AddCellValue(cellP1, "Price/CashFlow");
                row19.Cells.Add(cellP1);

                Telerik.Windows.Documents.Model.TableCell cellP3 = new Telerik.Windows.Documents.Model.TableCell();
                AddCellValue(cellP3, "Price Momentum");
                row19.Cells.Add(cellP3);
                table.Rows.Add(row19);
                #endregion
                section.Blocks.Add(new Telerik.Windows.Documents.Model.Paragraph());
                section.Blocks.Add(table);
                section.Blocks.Add(new Telerik.Windows.Documents.Model.Paragraph());
                document.Sections.Add(section);
            }
        }
    }











  2. Anna
    Admin
    Anna avatar
    99 posts

    Posted 13 Aug 2014 Link to this post

    Hi Payal,

    Thank you for your question.

    It seems there might be a mistake in the code where the table is constructed.

    Telerik.Windows.Documents.Model.TableRow row14 = new Telerik.Windows.Documents.Model.TableRow();
    Telerik.Windows.Documents.Model.TableCell cellK1 = new Telerik.Windows.Documents.Model.TableCell();
    AddCellValue(cellK1, "Avg Traded Value(3M)");
    row13.Cells.Add(cellK1);
     
    Telerik.Windows.Documents.Model.TableCell cellK3 = new Telerik.Windows.Documents.Model.TableCell();
    AddCellValue(cellK3, "Quick Ratio");
    row13.Cells.Add(cellK3);
    table.Rows.Add(row14);

    I assume that what was intended was to add the cells to row14, but they are instead added to row13. This way row14 is added without cells, which is not allowed.

    I hope this helps. If you encounter any further issues after you test with your desired number of columns, please, let us know.

    Regards,
    Anna
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  3. UI for WPF is Visual Studio 2017 Ready
  4. Payal
    Payal avatar
    2 posts
    Member since:
    Aug 2014

    Posted 13 Aug 2014 in reply to Anna Link to this post

    Hi Anna,

    Thank you very much for looking into issue.
    problem get solved.There were mistake in the code.

    Regards,
    Payal


Back to Top