GlossaryDoc.cs
  1. //
  2. // This code is part of Document Solutions for Word demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Xml;
  11. using GrapeCity.Documents.Word;
  12.  
  13. namespace DsWordWeb.Demos
  14. {
  15. // This sample demonstrates how to add building blocks to a document's glossary.
  16. public class GlossaryDoc
  17. {
  18. public GcWordDocument CreateDocx()
  19. {
  20. var doc = new GcWordDocument();
  21.  
  22. doc.Body.Paragraphs.Add("This sample demonstrates adding building blocks (custom headers and footers in this case) " +
  23. "to the document's glossary. They do not show in the generated document. To use the building blocks, " +
  24. "open the document in MS Word and explore the document's glossary.");
  25.  
  26. // Add header and footer building blocks to the document's glossary:
  27. AddHeaderBuildingBlockToGlossary(doc);
  28. AddFooterBuildingBlockToGlossary(doc);
  29.  
  30. // Now the document has two building blocks in the glossary.
  31. // Note that when the document is loaded into MS Word, they will not be visible.
  32. // To use them, open the document in MS Word and explore the document glossary.
  33. return doc;
  34. }
  35.  
  36. // Add header building block:
  37. private void AddHeaderBuildingBlockToGlossary(GcWordDocument doc)
  38. {
  39. GlossaryDocument glossary = doc.GlossaryDocument;
  40. BuildingBlockCollection buildingBlocks = glossary.BuildingBlocks;
  41. BuildingBlock buildingBlock = buildingBlocks.Add("New cool header", "2019 collection", BuildingBlockGallery.CustomHeaders);
  42. var bbBody = buildingBlock.Body;
  43. //here we can modify body as we want
  44. var p = bbBody.Paragraphs.Add("New cool building block neader");
  45. p.Style.Font.Color.RGB = Color.Blue;
  46. }
  47.  
  48. // Add footer building block:
  49. private void AddFooterBuildingBlockToGlossary(GcWordDocument doc)
  50. {
  51. GlossaryDocument glossary = doc.GlossaryDocument;
  52. BuildingBlockCollection buildingBlocks = glossary.BuildingBlocks;
  53. BuildingBlock buildingBlock = buildingBlocks.Add("New cool footer", "2019 collection", BuildingBlockGallery.CustomFooter);
  54. var bbBody = buildingBlock.Body;
  55. // Here we can modify the body as we want:
  56. var p = bbBody.Paragraphs.Add("New cool building block footer");
  57. p.Style.Font.Color.RGB = Color.Pink;
  58. }
  59. }
  60. }
  61.