|
1 | | -using DevExpress.XtraRichEdit; |
2 | | -using DevExpress.XtraRichEdit.API.Native; |
3 | | -using System; |
4 | | -using System.Collections.Generic; |
5 | | -using System.Diagnostics; |
6 | | -using System.Drawing; |
7 | | -using System.Linq; |
8 | | -using System.Text; |
9 | | -using System.Threading.Tasks; |
10 | | - |
11 | | -namespace WordProcessingFileAPI_CalcDocumentVariable |
12 | | -{ |
13 | | - class Program |
14 | | - { |
15 | | - static void Main(string[] args) |
16 | | - { |
17 | | - RichEditDocumentServer wordProcessor = new RichEditDocumentServer(); |
18 | | - wordProcessor.LoadDocument("Docs\\invitation.docx", DocumentFormat.OpenXml); |
19 | | - |
20 | | - Document document = wordProcessor.Document; |
21 | | - //Lock the first field to prevent updates |
22 | | - document.Fields[0].Locked = true; |
23 | | - |
24 | | - // Handle the CalculateDocumentVariable event |
25 | | - document.CalculateDocumentVariable += Document_CalculateDocumentVariable; |
26 | | - |
27 | | - // Adjust mail-merge options |
28 | | - MailMergeOptions myMergeOptions = document.CreateMailMergeOptions(); |
29 | | - myMergeOptions.MergeMode = MergeMode.NewSection; |
30 | | - myMergeOptions.DataSource = new SampleData(); |
31 | | - |
32 | | - // Handle mail-merge events |
33 | | - wordProcessor.MailMergeRecordStarted += wordProcessor_MailMergeRecordStarted; |
34 | | - wordProcessor.MailMergeRecordFinished += wordProcessor_MailMergeRecordFinished; |
35 | | - |
36 | | - // Mail-merge the document |
37 | | - document.MailMerge(myMergeOptions, "Result.docx", DocumentFormat.OpenXml); |
38 | | - |
39 | | - Process.Start("Result.docx"); |
40 | | - } |
41 | | - |
42 | | - private static void Document_CalculateDocumentVariable(object sender, CalculateDocumentVariableEventArgs e) |
43 | | - { |
44 | | - if (e.Arguments.Count > 0) |
45 | | - { |
46 | | - string location = e.Arguments[0].Value.ToString(); |
47 | | - if ((location.Trim() == String.Empty) || (location.Contains("<"))) |
48 | | - { |
49 | | - e.Value = " "; |
50 | | - e.Handled = true; |
51 | | - return; |
52 | | - } |
53 | | - switch (e.VariableName) |
54 | | - { |
55 | | - case "Weather": |
56 | | - Conditions conditions = new Conditions(); |
57 | | - conditions = Weather.GetCurrentConditions(location); |
58 | | - e.Value = String.Format("Weather for {0}: \nConditions: {1}\nTemperature (C) :{2}\nHumidity: {3}\nWind: {4}\n", |
59 | | - location, conditions.Condition, conditions.TempC, conditions.Humidity, conditions.Wind); |
60 | | - break; |
61 | | - case "LOCATION": |
62 | | - if (location == "DO NOT CHANGE!") e.Value = DocVariableValue.Current; |
63 | | - break; |
64 | | - default: |
65 | | - e.Value = "LOCKED FIELD UPDATED"; |
66 | | - break; |
67 | | - } |
68 | | - } |
69 | | - else |
70 | | - { |
71 | | - e.Value = "LOCKED FIELD UPDATED"; |
72 | | - } |
73 | | - e.Handled = true; |
74 | | - } |
75 | | - |
76 | | - private static void wordProcessor_MailMergeRecordStarted(object sender, MailMergeRecordStartedEventArgs e) |
77 | | - { |
78 | | - DocumentRange insertedRange = e.RecordDocument.InsertText(e.RecordDocument.Range.Start, String.Format("Created on {0:G}\n\n", DateTime.Now)); |
79 | | - CharacterProperties cp = e.RecordDocument.BeginUpdateCharacters(insertedRange); |
80 | | - cp.FontSize = 8; |
81 | | - cp.ForeColor = Color.Red; |
82 | | - e.RecordDocument.EndUpdateCharacters(cp); |
83 | | - } |
84 | | - |
85 | | - private static void wordProcessor_MailMergeRecordFinished(object sender, MailMergeRecordFinishedEventArgs e) |
86 | | - { |
87 | | - e.RecordDocument.AppendDocumentContent("Docs\\bungalow.docx", DocumentFormat.OpenXml); |
88 | | - } |
89 | | - } |
90 | | -} |
| 1 | +using DevExpress.XtraRichEdit; |
| 2 | +using DevExpress.XtraRichEdit.API.Native; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.Drawing; |
| 7 | +using System.Linq; |
| 8 | +using System.Text; |
| 9 | +using System.Threading.Tasks; |
| 10 | + |
| 11 | +namespace WordProcessingFileAPI_CalcDocumentVariable |
| 12 | +{ |
| 13 | + class Program |
| 14 | + { |
| 15 | + static void Main(string[] args) |
| 16 | + { |
| 17 | + RichEditDocumentServer wordProcessor = new RichEditDocumentServer(); |
| 18 | + wordProcessor.LoadDocument("Docs\\invitation.docx", DocumentFormat.OpenXml); |
| 19 | + |
| 20 | + Document document = wordProcessor.Document; |
| 21 | + //Lock the first field to prevent updates |
| 22 | + document.Fields[0].Locked = true; |
| 23 | + |
| 24 | + // Handle the CalculateDocumentVariable event |
| 25 | + document.CalculateDocumentVariable += Document_CalculateDocumentVariable; |
| 26 | + |
| 27 | + // Adjust mail-merge options |
| 28 | + MailMergeOptions myMergeOptions = document.CreateMailMergeOptions(); |
| 29 | + myMergeOptions.MergeMode = MergeMode.NewSection; |
| 30 | + myMergeOptions.DataSource = new SampleData(); |
| 31 | + |
| 32 | + // Handle mail-merge events |
| 33 | + wordProcessor.MailMergeRecordStarted += wordProcessor_MailMergeRecordStarted; |
| 34 | + wordProcessor.MailMergeRecordFinished += wordProcessor_MailMergeRecordFinished; |
| 35 | + |
| 36 | + // Mail-merge the document |
| 37 | + document.MailMerge(myMergeOptions, "Result.docx", DocumentFormat.OpenXml); |
| 38 | + var p = new Process(); |
| 39 | + p.StartInfo = new ProcessStartInfo(@"Result.docx") |
| 40 | + { |
| 41 | + UseShellExecute = true |
| 42 | + }; |
| 43 | + p.Start(); |
| 44 | + } |
| 45 | + |
| 46 | + private static void Document_CalculateDocumentVariable(object sender, CalculateDocumentVariableEventArgs e) |
| 47 | + { |
| 48 | + if (e.Arguments.Count > 0) |
| 49 | + { |
| 50 | + string location = e.Arguments[0].Value.ToString(); |
| 51 | + if ((location.Trim() == String.Empty) || (location.Contains("<"))) |
| 52 | + { |
| 53 | + e.Value = " "; |
| 54 | + e.Handled = true; |
| 55 | + return; |
| 56 | + } |
| 57 | + switch (e.VariableName) |
| 58 | + { |
| 59 | + case "Weather": |
| 60 | + Conditions conditions = new Conditions(); |
| 61 | + conditions = Weather.GetCurrentConditions(location); |
| 62 | + e.Value = String.Format("Weather for {0}: \nConditions: {1}\nTemperature (C) :{2}\nHumidity: {3}\nWind: {4}\n", |
| 63 | + location, conditions.Condition, conditions.TempC, conditions.Humidity, conditions.Wind); |
| 64 | + break; |
| 65 | + case "LOCATION": |
| 66 | + if (location == "DO NOT CHANGE!") e.Value = DocVariableValue.Current; |
| 67 | + break; |
| 68 | + default: |
| 69 | + e.Value = "LOCKED FIELD UPDATED"; |
| 70 | + break; |
| 71 | + } |
| 72 | + } |
| 73 | + else |
| 74 | + { |
| 75 | + e.Value = "LOCKED FIELD UPDATED"; |
| 76 | + } |
| 77 | + e.Handled = true; |
| 78 | + } |
| 79 | + |
| 80 | + private static void wordProcessor_MailMergeRecordStarted(object sender, MailMergeRecordStartedEventArgs e) |
| 81 | + { |
| 82 | + DocumentRange insertedRange = e.RecordDocument.InsertText(e.RecordDocument.Range.Start, String.Format("Created on {0:G}\n\n", DateTime.Now)); |
| 83 | + CharacterProperties cp = e.RecordDocument.BeginUpdateCharacters(insertedRange); |
| 84 | + cp.FontSize = 8; |
| 85 | + cp.ForeColor = Color.Red; |
| 86 | + e.RecordDocument.EndUpdateCharacters(cp); |
| 87 | + } |
| 88 | + |
| 89 | + private static void wordProcessor_MailMergeRecordFinished(object sender, MailMergeRecordFinishedEventArgs e) |
| 90 | + { |
| 91 | + e.RecordDocument.AppendDocumentContent("Docs\\bungalow.docx", DocumentFormat.OpenXml); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments