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 sectionDocument document =
new Document();Section section = document.Sections.Add();
// add a new table to the sectionTable table =
new Table();section.Paragraphs.Add( table );
// add a new row to the table - set the background color of the row to SalmonRow row = table.Rows.Add();
row.Border =
new Border( new SolidBrush( System.Drawing.Color.Salmon ) ); // add the columnheadersaddCell( 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 XPathNavigatorXPathDocument 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 LightBluerow = table.Rows.Add();
row.Border =
new Border( new SolidBrush( System.Drawing.Color.LightBlue ) ); // add a content rowaddCell( 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 rowCell 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 widthcell.PreferredWidth = width;
// add the content to the cell as a textparagraph with one fragmentTextParagraph text =
new TextParagraph();text.Fragments.Add(
new Fragment( content, 12 ) );cell.Paragraphs.Add( text );
// add the cell to the rowrow.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
Write New Comment ▼
Write New Comment
Sorry! This knol's owner(s) have blocked you from editing, making suggestions, or commenting here.