Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

MiscellaneousSample.cs 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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.Collections.Generic;
  11. using System.Data;
  12. using System.Drawing;
  13. using System.IO;
  14. using System.Linq;
  15. namespace Xceed.Words.NET.Examples
  16. {
  17. public class MiscellaneousSample
  18. {
  19. #region Private Members
  20. private const string MiscellaneousSampleResourcesDirectory = Program.SampleDirectory + @"Miscellaneous\Resources\";
  21. private const string MiscellaneousSampleOutputDirectory = Program.SampleDirectory + @"Miscellaneous\Output\";
  22. #endregion
  23. #region Constructors
  24. static MiscellaneousSample()
  25. {
  26. if( !Directory.Exists( MiscellaneousSample.MiscellaneousSampleOutputDirectory ) )
  27. {
  28. Directory.CreateDirectory( MiscellaneousSample.MiscellaneousSampleOutputDirectory );
  29. }
  30. }
  31. #endregion
  32. #region Public Methods
  33. /// <summary>
  34. /// Create a file and add a picture, a table, an hyperlink, paragraphs, a bulleted list and a numbered list.
  35. /// </summary>
  36. public static void CreateRecipe()
  37. {
  38. Console.WriteLine( "\tCreateRecipe()" );
  39. // Create a new document.
  40. using( DocX document = DocX.Create( MiscellaneousSample.MiscellaneousSampleOutputDirectory + @"CreateRecipe.docx" ) )
  41. {
  42. // Create a rotated picture from existing image.
  43. var image = document.AddImage( MiscellaneousSample.MiscellaneousSampleResourcesDirectory + @"cupcake.png" );
  44. var picture = image.CreatePicture();
  45. picture.Rotation = 20;
  46. // Create an hyperlink.
  47. var hyperlink = document.AddHyperlink( "Food.com", new Uri( "http://www.food.com/recipe/simple-vanilla-cupcakes-178370" ) );
  48. // Create a bulleted list for the ingredients.
  49. var bulletsList = document.AddList( "2 cups of flour", 0, ListItemType.Bulleted );
  50. document.AddListItem( bulletsList, "3⁄4 cup of sugar" );
  51. document.AddListItem( bulletsList, "1⁄2 cup of butter" );
  52. document.AddListItem( bulletsList, "2 eggs" );
  53. document.AddListItem( bulletsList, "1 cup of milk" );
  54. document.AddListItem( bulletsList, "2 teaspoons of baking powder" );
  55. document.AddListItem( bulletsList, "1⁄2 teaspoon of salt" );
  56. document.AddListItem( bulletsList, "1 teaspoon of vanilla essence" );
  57. // Create a table for text and the picture.
  58. var table = document.AddTable( 1, 2 );
  59. table.Design = TableDesign.LightListAccent3;
  60. table.AutoFit = AutoFit.Window;
  61. table.Rows[ 0 ].Cells[ 0 ].Paragraphs[ 0 ].AppendLine().AppendLine().Append( "Simple Vanilla Cupcakes Recipe" ).FontSize( 20 ).Font( new Font( "Comic Sans MS" ) );
  62. table.Rows[ 0 ].Cells[ 1 ].Paragraphs[ 0 ].AppendPicture( picture );
  63. // Create a numbered list for the directions.
  64. var recipeList = document.AddList( "Preheat oven to 375F and fill muffin cups with papers.", 0, ListItemType.Numbered, 1 );
  65. document.AddListItem( recipeList, "Mix butter and sugar until light and fluffy." );
  66. document.AddListItem( recipeList, "Beat in the eggs, one at a time.", 1 );
  67. document.AddListItem( recipeList, "Add the flour, baking powder and salt, alternate with milk and beat well." );
  68. document.AddListItem( recipeList, "Add in vanilla.", 1 );
  69. document.AddListItem( recipeList, "Divide in the pans and bake for 18 minutes." );
  70. document.AddListItem( recipeList, "Let cool 5 minutes an eat.", 1 );
  71. // Insert the data in page.
  72. document.InsertTable( table );
  73. var paragraph = document.InsertParagraph();
  74. paragraph.AppendLine();
  75. paragraph.AppendLine();
  76. paragraph.AppendLine( "Ingredients" ).FontSize( 15 ).Bold().Color(Color.BlueViolet);
  77. document.InsertList( bulletsList );
  78. var paragraph2 = document.InsertParagraph();
  79. paragraph2.AppendLine();
  80. paragraph2.AppendLine( "Directions" ).FontSize( 15 ).Bold().Color( Color.BlueViolet );
  81. document.InsertList( recipeList );
  82. var paragraph3 = document.InsertParagraph();
  83. paragraph3.AppendLine();
  84. paragraph3.AppendLine( "Reference: " ).AppendHyperlink( hyperlink ).Color( Color.Blue ).UnderlineColor( Color.Blue ).Append( "." );
  85. // Save this document to disk.
  86. document.Save();
  87. Console.WriteLine( "\tCreated: CreateRecipe.docx\n" );
  88. }
  89. }
  90. /// <summary>
  91. /// Create a document and add headers/footers with tables and pictures, paragraphs and charts.
  92. /// </summary>
  93. public static void CompanyReport()
  94. {
  95. Console.WriteLine( "\tCompanyReport()" );
  96. // Create a new document.
  97. using( DocX document = DocX.Create( MiscellaneousSample.MiscellaneousSampleOutputDirectory + @"CompanyReport.docx" ) )
  98. {
  99. // Add headers and footers.
  100. document.AddHeaders();
  101. document.AddFooters();
  102. // Define the pages header's picture in a Table. Odd and even pages will have the same headers.
  103. var oddHeader = document.Headers.Odd;
  104. var headerFirstTable = oddHeader.InsertTable( 1, 2 );
  105. headerFirstTable.Design = TableDesign.ColorfulGrid;
  106. headerFirstTable.AutoFit = AutoFit.Window;
  107. var upperLeftParagraph = oddHeader.Tables[ 0 ].Rows[ 0 ].Cells[ 0 ].Paragraphs[ 0 ];
  108. var logo = document.AddImage( MiscellaneousSample.MiscellaneousSampleResourcesDirectory + @"Phone.png" );
  109. upperLeftParagraph.AppendPicture( logo.CreatePicture( 30, 100 ) );
  110. upperLeftParagraph.Alignment = Alignment.left;
  111. // Define the pages header's text in a Table. Odd and even pages will have the same footers.
  112. var upperRightParagraph = oddHeader.Tables[ 0 ].Rows[ 0 ].Cells[ 1 ].Paragraphs[ 0 ];
  113. upperRightParagraph.Append( "Toms Telecom Annual report" ).Color( Color.White );
  114. upperRightParagraph.SpacingBefore( 5d );
  115. upperRightParagraph.Alignment = Alignment.right;
  116. // Define the pages footer's picture in a Table.
  117. var oddFooter = document.Footers.Odd;
  118. var footerFirstTable = oddFooter.InsertTable( 1, 2 );
  119. footerFirstTable.Design = TableDesign.ColorfulGrid;
  120. footerFirstTable.AutoFit = AutoFit.Window;
  121. var lowerRightParagraph = oddFooter.Tables[ 0 ].Rows[ 0 ].Cells[ 1 ].Paragraphs[ 0 ];
  122. lowerRightParagraph.AppendPicture( logo.CreatePicture( 30, 100 ) );
  123. lowerRightParagraph.Alignment = Alignment.right;
  124. // Define the pages footer's text in a Table
  125. var lowerLeftParagraph = oddFooter.Tables[ 0 ].Rows[ 0 ].Cells[ 0 ].Paragraphs[ 0 ];
  126. lowerLeftParagraph.Append( "Toms Telecom 2016" ).Color( Color.White );
  127. lowerLeftParagraph.SpacingBefore( 5d );
  128. // Define Data in first page : a Paragraph.
  129. var paragraph = document.InsertParagraph();
  130. paragraph.AppendLine( "Toms Telecom Annual report\n2016" ).Bold().FontSize( 35 ).SpacingBefore( 150d );
  131. paragraph.Alignment = Alignment.center;
  132. paragraph.InsertPageBreakAfterSelf();
  133. // Define Data in second page : a Bar Chart.
  134. document.InsertParagraph("").SpacingAfter( 150d );
  135. var barChart = new BarChart();
  136. var sales = CompanyData.CreateSales();
  137. var salesSeries = new Series( "Sales Per Month" );
  138. salesSeries.Color = Color.GreenYellow;
  139. salesSeries.Bind( sales, "Month", "Sales" );
  140. barChart.AddSeries( salesSeries );
  141. document.InsertChart( barChart );
  142. document.InsertParagraph("Sales were 11% greater in 2016 compared to 2015, with the usual drop during spring time.").SpacingBefore(35d).InsertPageBreakAfterSelf();
  143. // Define Data in third page : a Line Chart.
  144. document.InsertParagraph( "" ).SpacingAfter( 150d );
  145. var lineChart = new LineChart();
  146. var calls = CompanyData.CreateCallNumber();
  147. var callSeries = new Series( "Call Number Per Month" );
  148. callSeries.Bind( calls, "Month", "Calls" );
  149. lineChart.AddSeries( callSeries );
  150. document.InsertChart( lineChart );
  151. document.InsertParagraph( "The number of calls received was much lower in 2016 compared to 2015, by 31%. Winter is still the busiest time of year." ).SpacingBefore( 35d );
  152. // Save this document to disk.
  153. document.Save();
  154. Console.WriteLine( "\tCreated: CompanyReport.docx\n" );
  155. }
  156. }
  157. /// <summary>
  158. /// Load a document containing a templated invoice with Custom properties, tables, paragraphs and a picture.
  159. /// Set the values of those Custom properties, modify the picture, and fill the details table.
  160. /// </summary>
  161. public static void CreateInvoice()
  162. {
  163. Console.WriteLine( "\tCreateInvoice()" );
  164. // Load the templated invoice.
  165. var templateDoc = DocX.Load( MiscellaneousSample.MiscellaneousSampleResourcesDirectory + @"TemplateInvoice.docx" );
  166. if( templateDoc != null )
  167. {
  168. // Create the invoice from the templated invoice.
  169. var invoice = MiscellaneousSample.CreateInvoiceFromTemplate( templateDoc );
  170. invoice.SaveAs( MiscellaneousSample.MiscellaneousSampleOutputDirectory + @"CreateInvoice.docx" );
  171. Console.WriteLine( "\tCreated: CreateInvoice.docx\n" );
  172. }
  173. }
  174. #endregion
  175. #region Private Methods
  176. private static DocX CreateInvoiceFromTemplate( DocX templateDoc )
  177. {
  178. // Fill in the document custom properties.
  179. templateDoc.AddCustomProperty( new CustomProperty( "InvoiceNumber", 1355 ) );
  180. templateDoc.AddCustomProperty( new CustomProperty( "InvoiceDate", new DateTime( 2016, 10, 15 ).ToShortDateString() ) );
  181. templateDoc.AddCustomProperty( new CustomProperty( "CompanyName", "Toms Telecoms" ) );
  182. templateDoc.AddCustomProperty( new CustomProperty( "CompanySlogan", "Always with you" ) );
  183. templateDoc.AddCustomProperty( new CustomProperty( "ClientName", "James Doh" ) );
  184. templateDoc.AddCustomProperty( new CustomProperty( "ClientStreetName", "123 Main street" ) );
  185. templateDoc.AddCustomProperty( new CustomProperty( "ClientCity", "Springfield, Ohio" ) );
  186. templateDoc.AddCustomProperty( new CustomProperty( "ClientZipCode", "54789" ) );
  187. templateDoc.AddCustomProperty( new CustomProperty( "ClientPhone", "438-585-9636" ) );
  188. templateDoc.AddCustomProperty( new CustomProperty( "ClientMail", "abc@gmail.com" ) );
  189. templateDoc.AddCustomProperty( new CustomProperty( "CompanyStreetName", "1458 Thompson Road" ) );
  190. templateDoc.AddCustomProperty( new CustomProperty( "CompanyCity", "Los Angeles, California" ) );
  191. templateDoc.AddCustomProperty( new CustomProperty( "CompanyZipCode", "90210" ) );
  192. templateDoc.AddCustomProperty( new CustomProperty( "CompanyPhone", "1-965-434-5786" ) );
  193. templateDoc.AddCustomProperty( new CustomProperty( "CompanySupport", "support@tomstelecoms.com" ) );
  194. // Remove the default logo and add the new one.
  195. var paragraphWithDefaultLogo = MiscellaneousSample.GetParagraphContainingPicture( templateDoc );
  196. if( paragraphWithDefaultLogo != null )
  197. {
  198. paragraphWithDefaultLogo.Pictures.First().Remove();
  199. var newLogo = templateDoc.AddImage( MiscellaneousSample.MiscellaneousSampleResourcesDirectory + @"Phone.png" );
  200. paragraphWithDefaultLogo.AppendPicture( newLogo.CreatePicture( 60, 180 ) );
  201. }
  202. // Fill the details table.
  203. MiscellaneousSample.FillDetailsTable( ref templateDoc );
  204. return templateDoc;
  205. }
  206. private static Paragraph GetParagraphContainingPicture( DocX doc )
  207. {
  208. foreach( var p in doc.Paragraphs )
  209. {
  210. var picture = p.Pictures.FirstOrDefault();
  211. if( picture != null )
  212. {
  213. return p;
  214. }
  215. }
  216. return null;
  217. }
  218. private static void FillDetailsTable( ref DocX templateDoc )
  219. {
  220. // The datas that will fill the details table.
  221. var datas = MiscellaneousSample.GetDetailsData();
  222. // T he table from the templated invoice.
  223. var detailsTable = templateDoc.Tables.LastOrDefault();
  224. if( detailsTable == null )
  225. return;
  226. // Remove all rows of the details table, except the header one.
  227. while( detailsTable.Rows.Count > 1 )
  228. {
  229. detailsTable.RemoveRow();
  230. }
  231. // Loop through each data rows and use them to add new rows in the detailsTable.
  232. foreach( DataRow data in datas.Rows )
  233. {
  234. var newRow = detailsTable.InsertRow();
  235. newRow.Cells.First().InsertParagraph( data.ItemArray.First().ToString() );
  236. newRow.Cells.Last().InsertParagraph( data.ItemArray.Last().ToString() );
  237. }
  238. // Calculate the total amount.
  239. var amountStrings = detailsTable.Rows.Select( r => r.Cells.Last().Paragraphs.Last().Text.Remove(0,1) ).ToList();
  240. amountStrings.RemoveAt( 0 ); // remove the header
  241. var totalAmount = amountStrings.Select( s => double.Parse( s ) ).Sum();
  242. // Add a Total row in the details table.
  243. var totalRow = detailsTable.InsertRow();
  244. totalRow.Cells.First().InsertParagraph( "TOTAL:" );
  245. totalRow.Cells.Last().InsertParagraph( string.Format( "${0}", totalAmount ) );
  246. }
  247. private static DataTable GetDetailsData()
  248. {
  249. // Create Data to fill the invoice details table.
  250. var dataTable = new DataTable();
  251. dataTable.Columns.AddRange( new DataColumn[] { new DataColumn( "Description" ), new DataColumn( "Amount" ) } );
  252. dataTable.Rows.Add( "Explorer 8698HD Terminal", "$149.95" );
  253. dataTable.Rows.Add( "MultiSwitch TV Connector", "$24.95" );
  254. dataTable.Rows.Add( "50 feets cable wires", "$22.49" );
  255. dataTable.Rows.Add( "Transit A449 Phone Modem", "$59.95" );
  256. dataTable.Rows.Add( "Toms Wi-Fi router", "$79.95" );
  257. dataTable.Rows.Add( "Toms Protect2000 Antivirus software", "$39.95" );
  258. dataTable.Rows.Add( "Installation (3h30)", "$154.49" );
  259. return dataTable;
  260. }
  261. #endregion
  262. #region Private Classes
  263. private class CompanyData
  264. {
  265. public string Month
  266. {
  267. get;
  268. set;
  269. }
  270. public int Sales
  271. {
  272. get;
  273. set;
  274. }
  275. public int Calls
  276. {
  277. get;
  278. set;
  279. }
  280. internal static List<CompanyData> CreateSales()
  281. {
  282. var sales = new List<CompanyData>();
  283. sales.Add( new CompanyData() { Month = "Jan", Sales = 2500 } );
  284. sales.Add( new CompanyData() { Month = "Fev", Sales = 3000 } );
  285. sales.Add( new CompanyData() { Month = "Mar", Sales = 2850 } );
  286. sales.Add( new CompanyData() { Month = "Apr", Sales = 1050 } );
  287. sales.Add( new CompanyData() { Month = "May", Sales = 1200 } );
  288. sales.Add( new CompanyData() { Month = "Jun", Sales = 2900 } );
  289. sales.Add( new CompanyData() { Month = "Jul", Sales = 3450 } );
  290. sales.Add( new CompanyData() { Month = "Aug", Sales = 3800 } );
  291. sales.Add( new CompanyData() { Month = "Sep", Sales = 2900 } );
  292. sales.Add( new CompanyData() { Month = "Oct", Sales = 2600 } );
  293. sales.Add( new CompanyData() { Month = "Nov", Sales = 3000 } );
  294. sales.Add( new CompanyData() { Month = "Dec", Sales = 2500 } );
  295. return sales;
  296. }
  297. internal static List<CompanyData> CreateCallNumber()
  298. {
  299. var calls = new List<CompanyData>();
  300. calls.Add( new CompanyData() { Month = "Jan", Calls = 1200 } );
  301. calls.Add( new CompanyData() { Month = "Fev", Calls = 1400 } );
  302. calls.Add( new CompanyData() { Month = "Mar", Calls = 400 } );
  303. calls.Add( new CompanyData() { Month = "Apr", Calls = 50 } );
  304. calls.Add( new CompanyData() { Month = "May", Calls = 220 } );
  305. calls.Add( new CompanyData() { Month = "Jun", Calls = 400 } );
  306. calls.Add( new CompanyData() { Month = "Jul", Calls = 880 } );
  307. calls.Add( new CompanyData() { Month = "Aug", Calls = 220 } );
  308. calls.Add( new CompanyData() { Month = "Sep", Calls = 550 } );
  309. calls.Add( new CompanyData() { Month = "Oct", Calls = 790 } );
  310. calls.Add( new CompanyData() { Month = "Nov", Calls = 990 } );
  311. calls.Add( new CompanyData() { Month = "Dec", Calls = 1300 } );
  312. return calls;
  313. }
  314. }
  315. #endregion
  316. }
  317. }