Click here to Skip to main content
14,735,933 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi,
How can you show a Table in flowdocumentviewer with repeatable table column headers?
That means iam showing a table with 100 rows and say 6 column when the limit of the page exceeds the
remaing rows will get paginated to next page but columns are not showing there i need to show the Table column heading

I have seen this one [http://www.codeproject.com/KB/WPF/PimpedDocumentPaginator.aspx] and it does not work.

code:
   private void LoadContent(string headerText,FlowDocument flow) {
     flow.ColumnWidth = 999999;
     Table content = new Table {CellSpacing = 5};

     int columns = 6;
     for (int index = 0;index < columns;index++) {
       content.Columns.Add(new TableColumn());
       content.Columns[index].Width = new GridLength(110);
     }
     content.RowGroups.Add(new TableRowGroup());
     content.RowGroups[0].Rows.Add(new TableRow());

     TableRow currentRow = content.RowGroups[0].Rows[0];
     currentRow.Background = Brushes.CornflowerBlue;
     currentRow.Foreground = Brushes.White;
     currentRow.FontFamily = new FontFamily("MS Sans Serif");
     currentRow.FontSize = 20;
     currentRow.FontWeight = FontWeights.Bold;

     currentRow.Cells.Add(new TableCell(new Paragraph(new Run(headerText))));
     currentRow.Cells[0].ColumnSpan = 6;
     currentRow.Cells[0].TextAlignment = TextAlignment.Center;
     currentRow.Cells[0].LineHeight = 30;


     content.RowGroups[0].Rows.Add(new TableRow());
     currentRow = content.RowGroups[0].Rows[1];
     currentRow.FontFamily = new FontFamily("MS Sans Serif");
     currentRow.FontSize = 14;
     currentRow.FontWeight = FontWeights.Bold;
     currentRow.Background = Brushes.Gainsboro;

     currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Stage"))));
     currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Duration"))));
     currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Elapsed"))));
     currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Speed"))));
     currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Grade(%)"))));
     currentRow.Cells.Add(new TableCell(new Paragraph(new Run("HR (bpm)"))));

     for (int index = 2;index < 200;index++) {
       content.RowGroups[0].Rows.Add(new TableRow());
       currentRow = content.RowGroups[0].Rows[index];
       currentRow.FontFamily = new FontFamily("MS Sans Serif");
       currentRow.FontSize = 14;

       if (index % 2 == 0)
         currentRow.Background = Brushes.MistyRose;

       currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Stage"))));
       currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Duration"))));
       currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Elapsed"))));
       currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Speed"))));
       currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Grade(%)"))));
       currentRow.Cells.Add(new TableCell(new Paragraph(new Run("HR (bpm)"))));
     }

     flow.Blocks.Add(content);
   }

 public IDocumentPaginatorSource Generate() {

     using (Package container = Package.Open(filepath,FileMode.Create)) {
       using (XpsDocument xpsDoc = new  XpsDocument(container,CompressionOption.Maximum)) {

         XpsSerializationManager xpsSerializer = new XpsSerializationManager(new XpsPackagingPolicy(xpsDoc),false);
         FlowDocument document = XamlReader.Load(new XmlTextReader(new StringReader(Template()))) as FlowDocument;
        IDocumentPaginatorSource flowDocument = Render(document);
         DocumentPaginator paginator = ((IDocumentPaginatorSource)document).DocumentPaginator;
         paginator = new CustomPaginator(paginator,new Size(768,900),new Size(48,48));
         xpsSerializer.SaveAsXaml(paginator);
       }
     }
     XpsDocument doc = new XpsDocument(filepath,FileAccess.Read);
     return doc.GetFixedDocumentSequence();
   }

private IDocumentPaginatorSource Render(FlowDocument document) {
   Load("Stages",document);
     return document;
   }
 public void Load(string headerText,FlowDocument flow) {
    LoadContent(headerText,flow);
  }

   public static string  Template() {
     string path = System.IO.Path.Combine(Environment.CurrentDirectory,"document.xaml");
     using (StreamReader streamReader = File.OpenText(path)) {
       return streamReader.ReadToEnd();
     }
     return String.Empty;
   }

public class CustomPaginator: DocumentPaginator {

   Size pageSize;
   Size margin;
   DocumentPaginator paginator;
   Typeface typeface;


   public CustomPaginator(DocumentPaginator documentPaginator,Size currentSize,Size size) {
     pageSize = currentSize;
     margin = size;
     paginator = documentPaginator;
     paginator.PageSize = new Size(currentSize.Width - margin.Width * 2,currentSize.Height - margin.Height * 2);
   }


   Rect Move(Rect rect) {
     if (rect.IsEmpty) {
       return rect;
     }
     else {
       return new Rect(rect.Left + margin.Width,rect.Top + margin.Height,
                       rect.Width,rect.Height);
     }
   }

   public override DocumentPage GetPage(int pageNumber) {
     DocumentPage page = paginator.GetPage(pageNumber);
     DrawingVisual newpage = new DrawingVisual();
     ContainerVisual header =XpsReport.GetHeader(pageSize.Width,pageSize.Height));
     newpage.Children.Add(header);
     ContainerVisual footer = new ContainerVisual();
     header.Children.Add(XpsReport.GetFooter(pageSize.Width,pageSize.Height));
     newpage.Children.Add(footer);
      ContainerVisual smallerPage = new ContainerVisual();
    smallerPage.Children.Add(page.Visual);
     smallerPage.Transform = new MatrixTransform(1,0,0,1,0,100);
     newpage.Children.Add(smallerPage);
     newpage.Transform = new TranslateTransform(margin.Width,margin.Height);
     return new DocumentPage(newpage,pageSize,page.BleedBox,page.ContentBox);

   }

     public override bool IsPageCountValid {
     get {
       return paginator.IsPageCountValid;
     }
   }

   public override int PageCount {
     get {
       return paginator.PageCount;
     }
   }

   public override Size PageSize {
     get {
       return paginator.PageSize;
     }
     set {
       paginator.PageSize = value;
     }
   }

   public override IDocumentPaginatorSource Source {
     get {
       return paginator.Source;
     }
   }
 }


regards,
sajith
Posted
Updated 15-Feb-12 7:19am
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900