Adding Push Mode DataGrid TallPDF3.0 in .Net

Code Demonstrates Adding Push Mode DataGrid TallPDF3.0 in .Net


Adding Push Mode DataGrid TallPDF3.0 in .Net

 
using
System;

using

System.IO;

using

System.Xml;

using

System.Xml.XPath;

using

TallComponents.PDF.Layout;

using

TallComponents.PDF.Layout.Brushes;

using

TallComponents.PDF.Layout.Paragraphs;

using

TallComponents.PDF.Layout.Pens;

namespace

TallComponents.Samples.TallPDF

{

public class DataGridPushMode

{

static void Main()

{

using( FileStream file = new FileStream( @"..\..\datagridpushmode.pdf", FileMode.Create, FileAccess.Write ) )

{

// create a document with a single section

Document document =

new Document();

Section section = document.Sections.Add();

// add a new table to the section

Table table =

new Table();

section.Paragraphs.Add( table );

// add a new row to the table - set the background color of the row to Salmon

Row row = table.Rows.Add();

row.Border =

new Border( new SolidBrush( System.Drawing.Color.Salmon ) );

// add the columnheaders

addCell( row, "Book ID", 80,

true, true );

addCell( row, "Author", 150,

false, true );

addCell( row, "Title", 200,

false, true );

// set up a datasource using a XPathDocument and XPathNavigator

XPathDocument xpathDocument =

new XPathDocument( @"..\..\Data\books.xml" );

XPathNavigator xpathNavigator = xpathDocument.CreateNavigator();

XPathNodeIterator nodeIterator = xpathNavigator.Select( "catalog/book" );

// iterator over the XML nodes

while( nodeIterator.MoveNext() )

{

// add a new row to the table - set the background color of the row to LightBlue

row = table.Rows.Add();

row.Border =

new Border( new SolidBrush( System.Drawing.Color.LightBlue ) );

// add a content row

addCell( row, nodeIterator.Current.GetAttribute( "id",

string.Empty ).ToString(), 50, true, false );

addCell( row, GetValue( nodeIterator.Current, "author" ), 150,

false, false );

addCell( row, GetValue( nodeIterator.Current, "title" ), 250,

false, false );

}

document.Write( file );

}

}

// helper function to add a cell to a row

private static void addCell( Row row, string content, double width, bool first, bool top )

{

// create a new cell with a black 1-point border

// add a left border edge only if this the first cell in a row

// add a top border edge only if this a cell in a top row

Cell cell =

new Cell();

cell.Border =

new Border();

cell.Border.Left = first ?

new Pen( System.Drawing.Color.Black, 1 ) : null;

cell.Border.Right =

new Pen( System.Drawing.Color.Black, 1 );

cell.Border.Top = top ?

new Pen( System.Drawing.Color.Black, 1 ) : null;

cell.Border.Bottom =

new Pen( System.Drawing.Color.Black, 1 );

// prevent breaking content (if possible)

cell.FitToContent =

true;

// set the preferred width

cell.PreferredWidth = width;

// add the content to the cell as a textparagraph with one fragment

TextParagraph text =

new TextParagraph();

text.Fragments.Add(

new Fragment( content, 12 ) );

cell.Paragraphs.Add( text );

// add the cell to the row

row.Cells.Add( cell );

}

// helper function to retrieve a text from the current element

private static string GetValue( XPathNavigator nav, string xPathExpression )

{

object result = nav.Evaluate( xPathExpression );

if ( result is XPathNodeIterator )

{

XPathNodeIterator resultIterator = result

as XPathNodeIterator;

if ( resultIterator.MoveNext() )

{

return resultIterator.Current.Value;

}

}

return "";

}

}

}

Comments

Article rating:
Your rating:

Categories

Based on community consensus.

Activity for this knol

This week:

24pageviews

Totals:

291pageviews