Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using Novacode;
  7. using System.Text.RegularExpressions;
  8. using System.Drawing;
  9. namespace Examples
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. StringReplaceExample();
  16. CustomPropertiesExample();
  17. CreateDocumentOnTheFly();
  18. ImageExample1();
  19. //ImageExample2();
  20. }
  21. private static void StringReplaceExample()
  22. {
  23. File.Copy(@"..\..\Input\StringReplace.docx", @"..\..\Output\StringReplace.docx", true);
  24. // Load the document that you want to manipulate
  25. DocX document = DocX.Load(@"..\..\Output\StringReplace.docx");
  26. // Loop through the paragraphs in the document
  27. foreach (Paragraph p in document.Paragraphs)
  28. {
  29. /*
  30. * Replace each instance of the string pear with the string banana.
  31. * Specifying true as the third argument informs DocX to track the
  32. * changes made by this replace. The fourth argument tells DocX to
  33. * ignore case when matching the string pear.
  34. */
  35. p.Replace("pear", "banana", true, RegexOptions.IgnoreCase);
  36. }
  37. // File will be saved to ..\..\Output\
  38. document.Save();
  39. }
  40. private static void CustomPropertiesExample()
  41. {
  42. // A list which contains three new users
  43. List<User> newUsers = new List<User>
  44. {
  45. new User
  46. {
  47. forname = "John", username = "John87",
  48. freeGift = "toaster", joined = DateTime.Now,
  49. HomeAddress = "21 Hillview, Naas, Co. Kildare",
  50. RecieveFurtherMail = true
  51. },
  52. new User
  53. {
  54. forname = "James", username = "KingJames",
  55. freeGift = "kitchen knife", joined = DateTime.Now,
  56. HomeAddress = "37 Mill Lane, Maynooth, Co. Meath",
  57. RecieveFurtherMail = false
  58. },
  59. new User
  60. {
  61. forname = "Mary", username = "McNamara1",
  62. freeGift = "microwave", joined = DateTime.Now,
  63. HomeAddress = "110 Cherry Orchard Drive, Navan, Co. Roscommon", RecieveFurtherMail= true
  64. }
  65. };
  66. // Foreach of the three new user create a welcome document based on template.docx
  67. foreach (User newUser in newUsers)
  68. {
  69. // Copy template.docx so that we can customize it for this user
  70. string filename = string.Format(@"..\..\Output\{0}.docx", newUser.username);
  71. File.Copy(@"..\..\Input\Template.docx", filename, true);
  72. /*
  73. * Load the document to be manipulated and set the custom properties to this
  74. * users specific data
  75. */
  76. DocX doc = DocX.Load(filename);
  77. doc.SetCustomProperty("Forname", CustomPropertyType.Text, newUser.forname);
  78. doc.SetCustomProperty("Username", CustomPropertyType.Text, newUser.username);
  79. doc.SetCustomProperty("FreeGift", CustomPropertyType.Text, newUser.freeGift);
  80. doc.SetCustomProperty("HomeAddress", CustomPropertyType.Text, newUser.HomeAddress);
  81. doc.SetCustomProperty("PleaseWaitNDays", CustomPropertyType.NumberInteger, 4);
  82. doc.SetCustomProperty("GiftArrivalDate", CustomPropertyType.Date, newUser.joined.AddDays(4).ToUniversalTime());
  83. doc.SetCustomProperty("RecieveFurtherMail", CustomPropertyType.YesOrNo, newUser.RecieveFurtherMail);
  84. // File will be saved to ..\..\Output\
  85. doc.Save();
  86. doc.Dispose();
  87. }
  88. }
  89. private static void CreateDocumentOnTheFly()
  90. {
  91. // Create a new .docx file
  92. DocX d = DocX.Create(@"..\..\Output\Hello.docx");
  93. // Add a new paragraph to this document
  94. Paragraph one = d.AddParagraph();
  95. one.Alignment = Alignment.both;
  96. // Create a text formatting called f1
  97. Formatting f1 = new Formatting();
  98. f1.FontFamily = new FontFamily("Agency FB");
  99. f1.Size = 28;
  100. f1.Bold = true;
  101. f1.FontColor = Color.RoyalBlue;
  102. f1.UnderlineStyle = UnderlineStyle.doubleLine;
  103. f1.UnderlineColor = Color.Red;
  104. // Insert some new text, into the new paragraph, using the created formatting f1
  105. one.Insert(0, "I've got style!", f1, false);
  106. // Create a text formatting called f2
  107. Formatting f2 = new Formatting();
  108. f2.FontFamily = new FontFamily("Colonna MT");
  109. f2.Size = 36.5;
  110. f2.Italic = true;
  111. f2.FontColor = Color.SeaGreen;
  112. // Insert some new text, into the new paragraph, using the created formatting f2
  113. one.Insert(one.Value.Length, " I have a different style.", f2, false);
  114. // Save the document
  115. d.Save();
  116. d.Dispose();
  117. }
  118. private static void ImageExample1()
  119. {
  120. File.Copy(@"..\..\Input\Image.docx", @"..\..\Output\Image.docx", true);
  121. // Load a .docx file
  122. DocX document = DocX.Load(@"..\..\Output\Image.docx");
  123. // Add an image to the docx file
  124. Novacode.Image img = document.AddImage(@"..\..\Input\Donkey.jpg");
  125. // Decide which paragraph to add the image to
  126. Paragraph p = document.Paragraphs.Last();
  127. #region pic1
  128. // Create a picture, a picture is a customized view of an image
  129. Picture pic1 = new Picture(img.Id, "Donkey", "Taken on Omey island");
  130. // Set the pictures shape
  131. pic1.SetPictureShape(BasicShapes.cube);
  132. // Rotate the picture clockwise by 30 degrees
  133. pic1.Rotation = 30;
  134. // Insert the picture at the end of this paragraph
  135. p.InsertPicture(pic1, p.Value.Length);
  136. #endregion
  137. #region pic2
  138. // Create a picture, a picture is a customized view of an image
  139. Picture pic2 = new Picture(img.Id, "Donkey", "Taken on Omey island");
  140. // Set the pictures shape
  141. pic2.SetPictureShape(CalloutShapes.cloudCallout);
  142. // Flip the picture horizontal
  143. pic2.FlipHorizontal = true;
  144. // Insert the picture at the end of this paragraph
  145. p.InsertPicture(pic2, p.Value.Length);
  146. #endregion
  147. // Save the docx file
  148. document.Save();
  149. document.Dispose();
  150. }
  151. private static void ImageExample2()
  152. {
  153. // Load the document that you want to manipulate
  154. DocX document = DocX.Load(@"..\..\Output\Image.docx");
  155. foreach (Paragraph p in document.Paragraphs)
  156. {
  157. foreach (Picture pi in p.Pictures)
  158. {
  159. pi.Rotation = 30;
  160. }
  161. }
  162. // File will be saved to ..\..\Output\
  163. document.Save();
  164. document.Dispose();
  165. }
  166. }
  167. // This class is used in the CustomPropertiesExample()
  168. class User
  169. {
  170. public string forname, username, freeGift, HomeAddress;
  171. public DateTime joined;
  172. public bool RecieveFurtherMail;
  173. public User()
  174. { }
  175. }
  176. }