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.

Program.cs 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using Novacode;
  7. namespace CustomPropertyTestApp
  8. {
  9. // This class represents a user
  10. class User
  11. {
  12. public string forname, username, freeGift, HomeAddress;
  13. public DateTime joined;
  14. public bool RecieveFurtherMail;
  15. public User()
  16. { }
  17. }
  18. class Program
  19. {
  20. static void Main(string[] args)
  21. {
  22. // A list which contains three new users
  23. List<User> newUsers = new List<User>
  24. {
  25. new User
  26. {
  27. forname = "John", username = "John87",
  28. freeGift = "toaster", joined = DateTime.Now,
  29. HomeAddress = "21 Hillview, Naas, Co. Kildare",
  30. RecieveFurtherMail = true
  31. },
  32. new User
  33. {
  34. forname = "James", username = "KingJames",
  35. freeGift = "kitchen knife", joined = DateTime.Now,
  36. HomeAddress = "37 Mill Lane, Maynooth, Co. Meath",
  37. RecieveFurtherMail = false
  38. },
  39. new User
  40. {
  41. forname = "Mary", username = "McNamara1",
  42. freeGift = "microwave", joined = DateTime.Now,
  43. HomeAddress = "110 Cherry Orchard Drive, Navan, Co. Roscommon", RecieveFurtherMail= true
  44. }
  45. };
  46. // Foreach of the three new user create a welcome document based on template.docx
  47. foreach (User newUser in newUsers)
  48. {
  49. // Copy template.docx so that we can customize it for this user
  50. string filename = string.Format(@"{0}.docx", newUser.username);
  51. File.Copy(@"Template.docx", filename, true);
  52. /*
  53. * Load the document to be manipulated and set the custom properties to this
  54. * users specific data
  55. */
  56. DocX doc = DocX.Load(filename);
  57. doc.SetCustomProperty("Forname", CustomPropertyType.Text, newUser.forname);
  58. doc.SetCustomProperty("Username", CustomPropertyType.Text, newUser.username);
  59. doc.SetCustomProperty("FreeGift", CustomPropertyType.Text, newUser.freeGift);
  60. doc.SetCustomProperty("HomeAddress", CustomPropertyType.Text, newUser.HomeAddress);
  61. doc.SetCustomProperty("PleaseWaitNDays", CustomPropertyType.NumberInteger, 4);
  62. doc.SetCustomProperty("GiftArrivalDate", CustomPropertyType.Date, newUser.joined.AddDays(4).ToUniversalTime());
  63. doc.SetCustomProperty("RecieveFurtherMail", CustomPropertyType.YesOrNo, newUser.RecieveFurtherMail);
  64. // File will be saved to \CustomPropertyTestApp\bin\Debug
  65. doc.Save();
  66. }
  67. }
  68. }
  69. }