Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /***************************************************************************************
  2. DocX – DocX is the community edition of Xceed Words for .NET
  3. Copyright (C) 2009-2017 Xceed Software Inc.
  4. This program is provided to you under the terms of the Microsoft Public
  5. License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license
  6. For more features and fast professional support,
  7. pick up Xceed Words for .NET at https://xceed.com/xceed-words-for-net/
  8. *************************************************************************************/
  9. using System;
  10. using System.Drawing;
  11. using System.IO;
  12. using System.Linq;
  13. namespace Xceed.Words.NET.Examples
  14. {
  15. public class TableSample
  16. {
  17. #region Private Members
  18. private static Random rand = new Random();
  19. private const string TableSampleResourcesDirectory = Program.SampleDirectory + @"Table\Resources\";
  20. private const string TableSampleOutputDirectory = Program.SampleDirectory + @"Table\Output\";
  21. #endregion
  22. #region Constructors
  23. static TableSample()
  24. {
  25. if( !Directory.Exists( TableSample.TableSampleOutputDirectory ) )
  26. {
  27. Directory.CreateDirectory( TableSample.TableSampleOutputDirectory );
  28. }
  29. }
  30. #endregion
  31. #region Public Methods
  32. /// <summary>
  33. /// Create a table, insert rows, image and replace text.
  34. /// </summary>
  35. public static void InsertRowAndImageTable()
  36. {
  37. Console.WriteLine( "\tInsertRowAndImageTable()" );
  38. // Create a document.
  39. using( DocX document = DocX.Create( TableSample.TableSampleOutputDirectory + @"InsertRowAndImageTable.docx" ) )
  40. {
  41. // Add a title
  42. document.InsertParagraph( "Inserting table" ).FontSize( 15d ).SpacingAfter( 50d ).Alignment = Alignment.center;
  43. // Add a Table into the document and sets its values.
  44. var t = document.AddTable( 5, 2 );
  45. t.Design = TableDesign.ColorfulListAccent1;
  46. t.Alignment = Alignment.center;
  47. t.Rows[ 0 ].Cells[ 0 ].Paragraphs[ 0 ].Append( "Mike" );
  48. t.Rows[ 0 ].Cells[ 1 ].Paragraphs[ 0 ].Append( "65" );
  49. t.Rows[ 1 ].Cells[ 0 ].Paragraphs[ 0 ].Append( "Kevin" );
  50. t.Rows[ 1 ].Cells[ 1 ].Paragraphs[ 0 ].Append( "62" );
  51. t.Rows[ 2 ].Cells[ 0 ].Paragraphs[ 0 ].Append( "Carl" );
  52. t.Rows[ 2 ].Cells[ 1 ].Paragraphs[ 0 ].Append( "60" );
  53. t.Rows[ 3 ].Cells[ 0 ].Paragraphs[ 0 ].Append( "Michael" );
  54. t.Rows[ 3 ].Cells[ 1 ].Paragraphs[ 0 ].Append( "59" );
  55. t.Rows[ 4 ].Cells[ 0 ].Paragraphs[ 0 ].Append( "Shawn" );
  56. t.Rows[ 4 ].Cells[ 1 ].Paragraphs[ 0 ].Append( "57" );
  57. // Add a row at the end of the table and sets its values.
  58. var r = t.InsertRow();
  59. r.Cells[ 0 ].Paragraphs[ 0 ].Append( "Mario" );
  60. r.Cells[ 1 ].Paragraphs[ 0 ].Append( "54" );
  61. // Add a row at the end of the table which is a copy of another row, and sets its values.
  62. var newPlayer = t.InsertRow( t.Rows[ 2 ] );
  63. newPlayer.ReplaceText( "Carl", "Max" );
  64. newPlayer.ReplaceText( "60", "50" );
  65. // Add an image into the document.
  66. var image = document.AddImage( TableSample.TableSampleResourcesDirectory + @"logo_xceed.png" );
  67. // Create a picture from image.
  68. var picture = image.CreatePicture( 25, 100 );
  69. // Calculate totals points from second column in table.
  70. var totalPts = 0;
  71. foreach( var row in t.Rows )
  72. {
  73. totalPts += int.Parse( row.Cells[ 1 ].Paragraphs[ 0 ].Text );
  74. }
  75. // Add a row at the end of the table and sets its values.
  76. var totalRow = t.InsertRow();
  77. totalRow.Cells[ 0 ].Paragraphs[ 0 ].Append( "Total for " ).AppendPicture( picture );
  78. totalRow.Cells[ 1 ].Paragraphs[ 0 ].Append( totalPts.ToString() );
  79. totalRow.Cells[ 1 ].VerticalAlignment = VerticalAlignment.Center;
  80. // Insert a new Paragraph into the document.
  81. var p = document.InsertParagraph( "Xceed Top Players Points:" );
  82. p.SpacingAfter( 40d );
  83. // Insert the Table after the Paragraph.
  84. p.InsertTableAfterSelf( t );
  85. document.Save();
  86. Console.WriteLine( "\tCreated: InsertRowAndImageTable.docx\n" );
  87. }
  88. }
  89. /// <summary>
  90. /// Create a table and set the text direction of each cell.
  91. /// </summary>
  92. public static void TextDirectionTable()
  93. {
  94. Console.WriteLine( "\tTextDirectionTable()" );
  95. // Create a document.
  96. using( DocX document = DocX.Create( TableSample.TableSampleOutputDirectory + @"TextDirectionTable.docx" ) )
  97. {
  98. // Add a title
  99. document.InsertParagraph( "Text Direction of Table's cells" ).FontSize( 15d ).SpacingAfter( 50d ).Alignment = Alignment.center;
  100. // Create a table.
  101. var table = document.AddTable( 2, 3 );
  102. table.Design = TableDesign.ColorfulList;
  103. // Set the table's values.
  104. table.Rows[ 0 ].Cells[ 0 ].Paragraphs[ 0 ].Append( "First" );
  105. table.Rows[ 0 ].Cells[ 0 ].TextDirection = TextDirection.btLr;
  106. table.Rows[ 0 ].Cells[ 0 ].Paragraphs[ 0 ].Spacing( 5d );
  107. table.Rows[ 0 ].Cells[ 1 ].Paragraphs[ 0 ].Append( "Second" );
  108. table.Rows[ 0 ].Cells[ 1 ].TextDirection = TextDirection.right;
  109. table.Rows[ 0 ].Cells[ 2 ].Paragraphs[ 0 ].Append( "Third" );
  110. table.Rows[ 0 ].Cells[ 2 ].Paragraphs[ 0 ].Spacing( 5d );
  111. table.Rows[ 0 ].Cells[ 2 ].TextDirection = TextDirection.btLr;
  112. table.Rows[ 1 ].Cells[ 0 ].Paragraphs[ 0 ].Append( "Fourth" );
  113. table.Rows[ 1 ].Cells[ 0 ].TextDirection = TextDirection.btLr;
  114. table.Rows[ 1 ].Cells[ 0 ].Paragraphs[ 0 ].Spacing( 5d );
  115. table.Rows[ 1 ].Cells[ 1 ].Paragraphs[ 0 ].Append( "Fifth" );
  116. table.Rows[ 1 ].Cells[ 2 ].Paragraphs[ 0 ].Append( "Sixth" ).Color( Color.White );
  117. table.Rows[ 1 ].Cells[ 2 ].TextDirection = TextDirection.btLr;
  118. // Last cell have a green background
  119. table.Rows[ 1 ].Cells[ 2 ].FillColor = Color.Green;
  120. table.Rows[ 1 ].Cells[ 2 ].Paragraphs[ 0 ].Spacing( 5d );
  121. // Set the table's column width.
  122. table.SetWidths( new float[] { 200, 300, 100 } );
  123. // Add the table into the document.
  124. document.InsertTable( table );
  125. document.Save();
  126. Console.WriteLine( "\tCreated: TextDirectionTable.docx\n" );
  127. }
  128. }
  129. /// <summary>
  130. /// Load a document, gets its table and replace the default row with updated copies of it.
  131. /// </summary>
  132. public static void CreateRowsFromTemplate()
  133. {
  134. Console.WriteLine( "\tCreateRowsFromTemplate()" );
  135. // Load a document
  136. using( DocX document = DocX.Load( TableSample.TableSampleResourcesDirectory + @"DocumentWithTemplateTable.docx" ) )
  137. {
  138. // get the table with caption "GROCERY_LIST" from the document.
  139. var groceryListTable = document.Tables.FirstOrDefault( t => t.TableCaption == "GROCERY_LIST" );
  140. if( groceryListTable == null )
  141. {
  142. Console.WriteLine( "\tError, couldn't find table with caption GROCERY_LIST in current document." );
  143. }
  144. else
  145. {
  146. if( groceryListTable.RowCount > 1 )
  147. {
  148. // Get the row pattern of the second row.
  149. var rowPattern = groceryListTable.Rows[ 1 ];
  150. // Add items (rows) to the grocery list.
  151. TableSample.AddItemToTable( groceryListTable, rowPattern, "Banana" );
  152. TableSample.AddItemToTable( groceryListTable, rowPattern, "Strawberry" );
  153. TableSample.AddItemToTable( groceryListTable, rowPattern, "Chicken" );
  154. TableSample.AddItemToTable( groceryListTable, rowPattern, "Bread" );
  155. TableSample.AddItemToTable( groceryListTable, rowPattern, "Eggs" );
  156. TableSample.AddItemToTable( groceryListTable, rowPattern, "Salad" );
  157. // Remove the pattern row.
  158. rowPattern.Remove();
  159. }
  160. }
  161. document.SaveAs( TableSample.TableSampleOutputDirectory + @"CreateTableFromTemplate.docx" );
  162. Console.WriteLine( "\tCreated: CreateTableFromTemplate.docx\n" );
  163. }
  164. }
  165. /// <summary>
  166. /// Add a Table in a document where its columns will have a specific width. In addition,
  167. /// the left margin of the row cells will be removed for all rows except the first.
  168. /// Finally, a blank border will be set for the table's top and bottom borders.
  169. /// </summary>
  170. public static void ColumnsWidth()
  171. {
  172. Console.WriteLine( "\tColumnsWidth()" );
  173. // Create a document
  174. using( DocX document = DocX.Create( TableSample.TableSampleOutputDirectory + @"ColumnsWidth.docx" ) )
  175. {
  176. // Add a title
  177. document.InsertParagraph( "Columns width" ).FontSize( 15d ).SpacingAfter( 50d ).Alignment = Alignment.center;
  178. // Insert a title paragraph.
  179. var p = document.InsertParagraph( "In the following table, the cell's left margin has been removed for rows 2-6 as well as the top/bottom table's borders." ).Bold();
  180. p.Alignment = Alignment.center;
  181. p.SpacingAfter( 40d );
  182. // Add a table in a document of 1 row and 3 columns.
  183. var columnWidths = new float[] { 100f, 300f, 200f };
  184. var t = document.InsertTable( 1, columnWidths.Length );
  185. // Set the table's column width and background
  186. t.SetWidths( columnWidths );
  187. t.Design = TableDesign.TableGrid;
  188. t.AutoFit = AutoFit.Contents;
  189. var row = t.Rows.First();
  190. // Fill in the columns of the first row in the table.
  191. for( int i = 0; i < row.Cells.Count; ++i )
  192. {
  193. row.Cells[i].Paragraphs.First().Append( "Data " + i );
  194. }
  195. // Add rows in the table.
  196. for( int i = 0; i < 5; i++ )
  197. {
  198. var newRow = t.InsertRow();
  199. // Fill in the columns of the new rows.
  200. for( int j = 0; j < newRow.Cells.Count; ++j )
  201. {
  202. var newCell = newRow.Cells[ j ];
  203. newCell.Paragraphs.First().Append( "Data " + i );
  204. // Remove the left margin of the new cells.
  205. newCell.MarginLeft = 0;
  206. }
  207. }
  208. // Set a blank border for the table's top/bottom borders.
  209. var blankBorder = new Border( BorderStyle.Tcbs_none, 0, 0, Color.White );
  210. t.SetBorder( TableBorderType.Bottom, blankBorder );
  211. t.SetBorder( TableBorderType.Top, blankBorder );
  212. document.Save();
  213. Console.WriteLine( "\tCreated: ColumnsWidth.docx\n" );
  214. }
  215. }
  216. /// <summary>
  217. /// Add a table and merged some cells. Individual cells can also be removed by shifting their right neighbors to the left.
  218. /// </summary>
  219. public static void MergeCells()
  220. {
  221. Console.WriteLine( "\tMergeCells()" );
  222. // Create a document.
  223. using( DocX document = DocX.Create( TableSample.TableSampleOutputDirectory + @"MergeCells.docx" ) )
  224. {
  225. // Add a title.
  226. document.InsertParagraph( "Merge and delete cells" ).FontSize( 15d ).SpacingAfter( 50d ).Alignment = Alignment.center;
  227. // Add A table .
  228. var t = document.AddTable( 3, 2 );
  229. t.Design = TableDesign.TableGrid;
  230. var t1 = document.InsertTable( t );
  231. // Add 4 columns in the table.
  232. t1.InsertColumn( t1.ColumnCount, true );
  233. t1.InsertColumn( t1.ColumnCount, true );
  234. t1.InsertColumn( t1.ColumnCount, true );
  235. t1.InsertColumn( t1.ColumnCount, true );
  236. // Merged Cells 1 to 4 in first row of the table.
  237. t1.Rows[ 0 ].MergeCells( 1, 4 );
  238. // Merged the last 2 Cells in the second row of the table.
  239. var columnCount = t1.Rows[ 1 ].ColumnCount;
  240. t1.Rows[ 1 ].MergeCells( columnCount - 2, columnCount - 1 );
  241. // Add text in each cell of the table.
  242. foreach( var r in t1.Rows )
  243. {
  244. for( int i = 0; i < r.Cells.Count; ++i )
  245. {
  246. var c = r.Cells[ i ];
  247. c.Paragraphs[ 0 ].InsertText( "Column " + i );
  248. c.Paragraphs[ 0 ].Alignment = Alignment.center;
  249. }
  250. }
  251. // Delete the second cell from the third row and shift the cells on its right by 1 to the left.
  252. t1.DeleteAndShiftCellsLeft( 2, 1 );
  253. document.Save();
  254. Console.WriteLine( "\tCreated: MergeCells.docx\n" );
  255. }
  256. }
  257. #endregion
  258. #region Private Methods
  259. private static void AddItemToTable( Table table, Row rowPattern, string productName )
  260. {
  261. // Gets a random unit price and quantity.
  262. var unitPrice = Math.Round( rand.NextDouble(), 2 );
  263. var unitQuantity = rand.Next( 1, 10 );
  264. // Insert a copy of the rowPattern at the last index in the table.
  265. var newItem = table.InsertRow( rowPattern, table.RowCount - 1 );
  266. // Replace the default values of the newly inserted row.
  267. newItem.ReplaceText( "%PRODUCT_NAME%", productName );
  268. newItem.ReplaceText( "%PRODUCT_UNITPRICE%", "$ " + unitPrice.ToString( "N2" ) );
  269. newItem.ReplaceText( "%PRODUCT_QUANTITY%", unitQuantity.ToString() );
  270. newItem.ReplaceText( "%PRODUCT_TOTALPRICE%", "$ " + ( unitPrice * unitQuantity ).ToString( "N2" ) );
  271. }
  272. #endregion
  273. }
  274. }