如何用DevExpress的UI组件创建节日邀请函?这里有答案!

春节临近,你还不会制作电子贺卡、电子邀请函?OUT了!DevExpress的文档类控件可以帮你轻松实现!赶紧点击查看~

本文为大家揭秘如何使用

向模板添加字段

在本文中,我们将使用一下文档模板:

如何用DevExpress的UI组件创建节日邀请函里有答案!

我们将使用 DevExpress Word Processing Document API 将文档模板中的静态字符串(“party date”、“time”、“response date”和“manager”)替换为字段。

注意:您需要调用FieldCollection.Create方法来添加新字段。此方法允许将文本转换为字段,或在指定的文档位置插入字段。

您可以添加代码开关来定义数字、文本、日期和时间字段的格式。下面的代码示例创建一个 DOCPROPERTY 字段(用于插入派对日期)和一个 H:mm am/pm 格式的 TIME 字段:

private static void CreateFields(Document document){// Create a “Party Date” custom document propertydocument.CustomProperties["Party Date"] = new DateTime(2021, 12, 23);// Find the “Party Date” phraseDocumentRange[] dateRanges = document.FindAll("Party Date", SearchOptions.WholeWord);DocumentPosition datePosition = dateRanges[0].End;// Delete the phrasedocument.Delete(dateRanges[0]);// Create a field at the phrase’s positiondocument.Fields.Create(datePosition, @"DOCPROPERTY ""Party Date""");// Find the word “Time”DocumentRange[] timeRanges = document.FindAll("Time", SearchOptions.WholeWord);DocumentPosition timePosition = timeRanges[0].End;// Delete the phrasedocument.Delete(timeRanges[0]);// Create a field at the phrase’s positiondocument.Fields.Create(timePosition, @" TIME @ ""H:mm AM/PM""");}
DOCVARIABLE 字段(文档变量)

DOCVARIABLE 字段允许您将任何类型的内容插入到文档中——从简单的变量到来自数据库或其他文档的动态数据。变量可以存储在文档中或在CalculateDocumentVariable事件中计算,在本示例中,我们创建了一个响应日期变量来计算响应截止日期。

private static void CalculateResponseDate(Document document){// Obtain the Party Date property valueDateTime date = (DateTime)document.CustomProperties["Party Date"];// Specify the interval between the response and party datesvar responseBefore = new TimeSpan(7, 0, 0, 0);// Calculate the response dateDateTime responseDate = date.Subtract(responseBefore);// Create a new document variabledocument.Variables.Add("Response Date", responseDate);}

您可以向文档的任何部分添加字段:正文、页眉、页脚、文本框、脚注或尾注,下面的代码示例将“响应日期”变量和 DOCPROPERTY 字段(插入经理的姓名)添加到文档页脚:

private static void CreateFieldsInFooter(Document document){// Start the footer updatevar footer = document.Sections[0].BeginUpdateFooter(HeaderFooterType.Primary);// Find the word “Manager”DocumentRange[] managerRanges = footer.FindAll("Manager", SearchOptions.WholeWord);// Convert the found text to a fieldField managerField = footer.Fields.Create(managerRanges[0]);// Insert the field namefooter.InsertText(managerField.CodeRange.Start, "DOCPROPERTY ");// Find the “Response Date” phraseDocumentRange[] responseRanges =footer.FindAll("Response Date", SearchOptions.WholeWord);DocumentPosition responsePosition = responseRanges[0].End;// Remove the phrasefooter.Delete(responseRanges[0]);// Create a field at the phrase’s positionfooter.Fields.Create(responsePosition, @" DOCVARIABLE ""Response Date""");// Finalize the footer updatedocument.Sections[0].EndUpdateFooter(footer);

新的 UpdateFieldOptions.DocVariable 属性(在 v21.2 中实现)允许您在加载文档时保留 DOCVARIABLE 字段值。例如,如果您需要更改聚会日期,同时保持响应日期不变,在 BeforeImport 事件处理程序中将 UpdateFieldOptions.DocVariable 属性设置为 false,如下所示。

using DevExpress.XtraRichEdit;using DevExpress.XtraRichEdit.Import;wordProcessor.BeforeImport += WordProcessor_BeforeImport;private static void WordProcessor_BeforeImport(object sender, BeforeImportEventArgs e){if (e.DocumentFormat == DocumentFormat.OpenXml){((OpenXmlDocumentImporterOptions)e.Options).UpdateField.DocVariable = false;}}

如果您现在重新加载文档,更改聚会日期并保存结果,将看到 DOCVARIABLE 字段仍显示相同的日期:

如何用DevExpress的UI组件创建节日邀请函里有答案!
更新字段并保存结果

插入字段后,更新它们来插入实际的字段,然后保存文档。在以前的版本中,您必须为每个单独的文档部分(标题、正文、脚注等)调用 FieldCollection.Update 方法。在 v21.2 中添加了 Document.UpdateAllFields 方法来同时更新所有文档字段。

下面的代码示例利用前面示例中创建的所有方法,更新所有文档字段,并保存结果:

using DevExpress.XtraRichEdit;using DevExpress.XtraRichEdit.API.Native;using (var wordProcessor = new RichEditDocumentServer()){// Load a documentwordProcessor.LoadDocument(“party invitation.docx”);Document document = wordProcessor.Document;// Create fieldsCreateFields(document);CalculateResponseDate(document);CreateFieldsInFooter(document);// Update all fieldsdocument.UpdateAllFields();// Save the resultwordProcessor.SaveDocument(“party invitation_docx.docx”, DocumentFormat.OpenXml);}

切换到WinForms 的富文本编辑器并查看结果,您可以通过富文本编辑器的UI添加和修改字段。按 CTRL+F9 插入字段,按 ALT+F9(或单击上下文菜单中的切换字段代码)修改字段。

如何用DevExpress的UI组件创建节日邀请函里有答案!

DevExpress WinForm | 下载试用

DevExpress WinForm拥有180+组件和UI库,能为Windows Forms平台创建具有影响力的业务解决方案。DevExpress WinForms能完美构建流畅、美观且易于使用的应用程序,无论是Office风格的界面,还是分析处理大批量的业务数据,它都能轻松胜任!

更多产品正版授权详情及优惠,欢迎咨询在线客服>>


DevExpress技术交流群5:742234706      欢迎一起进群讨论

更多DevExpress线上公开课、中文教程资讯请上中文网获取

DevExpress企业定制服务
标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年1月13日
下一篇 2022年1月13日

相关推荐

发表回复

登录后才能评论