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


    本文为大家揭秘如何使用DevExpress .NET Word Processing API和UI组件创建节日邀请函!

    获取工具下载 - DevExpress v21.2

    向模板添加字段

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

    如何用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 property
    document.CustomProperties["Party Date"] = new DateTime(2021, 12, 23);
    
    // Find the “Party Date” phrase
    DocumentRange[] dateRanges = document.FindAll("Party Date", SearchOptions.WholeWord);
    DocumentPosition datePosition = dateRanges[0].End;
    
    // Delete the phrase
    document.Delete(dateRanges[0]);
    
    // Create a field at the phrase’s position
    document.Fields.Create(datePosition, @"DOCPROPERTY ""Party Date""");
    
    // Find the word “Time”
    DocumentRange[] timeRanges = document.FindAll("Time", SearchOptions.WholeWord);
    DocumentPosition timePosition = timeRanges[0].End;
    
    // Delete the phrase
    document.Delete(timeRanges[0]);
    
    // Create a field at the phrase’s position
    document.Fields.Create(timePosition, @" TIME \@ ""H:mm AM/PM""");
    }
    DOCVARIABLE 字段(文档变量)

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

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

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

    private static void CreateFieldsInFooter(Document document)
    {
    // Start the footer update
    var footer = document.Sections[0].BeginUpdateFooter(HeaderFooterType.Primary);
    
    // Find the word “Manager”
    DocumentRange[] managerRanges = footer.FindAll("Manager", SearchOptions.WholeWord);
    
    // Convert the found text to a field
    Field managerField = footer.Fields.Create(managerRanges[0]);
    
    // Insert the field name
    footer.InsertText(managerField.CodeRange.Start, "DOCPROPERTY ");
    
    // Find the “Response Date” phrase
    DocumentRange[] responseRanges =
    footer.FindAll("Response Date", SearchOptions.WholeWord);
    DocumentPosition responsePosition = responseRanges[0].End;
    
    // Remove the phrase
    footer.Delete(responseRanges[0]);
    
    // Create a field at the phrase’s position
    footer.Fields.Create(responsePosition, @" DOCVARIABLE ""Response Date""");
    
    // Finalize the footer update
    document.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 document
    wordProcessor.LoadDocument(“party invitation.docx”);
    Document document = wordProcessor.Document;
    
    // Create fields
    CreateFields(document);
    CalculateResponseDate(document);
    CreateFieldsInFooter(document);
    
    // Update all fields
    document.UpdateAllFields();
    
    // Save the result
    wordProcessor.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线上公开课、中文教程资讯请上中文网获取

  • 相关阅读:
    C语言II博客作业04
    C语言II博客作业03
    C语言II博客作业02
    C语言II博客作业01
    学期总结
    C语言I博客作业08
    C语言I博客作业07
    C语言I博客作业06
    C语言I博客作业05
    C语言I博客作业04
  • 原文地址:https://www.cnblogs.com/AABBbaby/p/15816386.html
Copyright © 2020-2023  润新知