ComplexFields.vb
  1. ''
  2. '' This code is part of Document Solutions for Word demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports System.Collections.Generic
  8. Imports System.Linq
  9. Imports GrapeCity.Documents.Word
  10.  
  11. '' This sample demonstrates the use of complext fields.
  12. '' It adds a complex field that tests whether the current date
  13. '' Is the New Year day, And modifies the text in the document
  14. '' accordingly. See also DateAndTime sample.
  15. Public Class ComplexFields
  16. Function CreateDocx() As GcWordDocument
  17. Dim doc = New GcWordDocument()
  18.  
  19. '' Add a title
  20. doc.Body.Paragraphs.Add("Testing whether it is the New Year yet").Style = doc.Styles(BuiltInStyleId.Title)
  21.  
  22. '' Add a paragraph And get its range:
  23. Dim rng = doc.Body.Paragraphs.Add().GetRange()
  24. '' Add a static text:
  25. rng.Runs.Add("Today is ")
  26.  
  27. '' Add a complex field with "IF" instruction. We also provide
  28. '' a pre-calculated value for the sake of PDF export, as DsWord
  29. '' does Not yet support field calculation - see DateAndTime.
  30. '' Note also that because the code field will Not be calculated
  31. '' in the PDF export, the "NOT " won't be bold in it:
  32. Dim val As String
  33. If Util.TimeNow().DayOfYear = 1 Then
  34. val = ""
  35. Else
  36. val = "NOT "
  37. End If
  38. Dim f = rng.ComplexFields.Add("IF ", val)
  39. '' Add a complex field with "DATE" instruction:
  40. f.GetCodeRange().ComplexFields.Add(" DATE \@ ""M-d"" ")
  41. '' Add additional instruction to the "IF" field to compare the nested
  42. '' DATE field result with "1-1" And return "NOT " if it Is true,
  43. '' also make the "NOT " bold if visible:
  44. f.CodeFields.Add("<> ""1-1"" ""Not """).ParentRun.Font.Bold = True
  45.  
  46. '' Add a static text
  47. rng.Runs.Add("New Year's Day.")
  48.  
  49. '' Done
  50. Return doc
  51. End Function
  52. End Class
  53.