没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
翻译|使用教程|编辑:龚雪|2022-01-17 09:59:38.410|阅读 212 次
概述:春节临近,你还不会制作电子贺卡、电子邀请函?OUT了!DevExpress的文档类控件可以帮你轻松实现!赶紧点击查看~
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
相关链接:
本文为大家揭秘如何使用DevExpress .NET Word Processing API和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 字段允许您将任何类型的内容插入到文档中——从简单的变量到来自数据库或其他文档的动态数据。变量可以存储在文档中或在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 字段仍显示相同的日期:
插入字段后,更新它们来插入实际的字段,然后保存文档。在以前的版本中,您必须为每个单独的文档部分(标题、正文、脚注等)调用 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 WinForm拥有180+组件和UI库,能为Windows Forms平台创建具有影响力的业务解决方案。DevExpress WinForms能完美构建流畅、美观且易于使用的应用程序,无论是Office风格的界面,还是分析处理大批量的业务数据,它都能轻松胜任!
更多产品正版授权详情及优惠,欢迎咨询
DevExpress技术交流群5:742234706 欢迎一起进群讨论
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@ke049m.cn
文章转载自:慧都网



注意: Cogent DataHub 软件 v11 包含一些新功能,您的目标操作系统可能不支持这些功能。
本教程主要为大家介绍如何使用DevExpress WinForms数据网格控件进行数据排序的基础知识,欢迎下载最新版组件体验!
在使用Parasoft C/C++test执行BugDetective数据流分析时,可能会遇到用户自定义的资源API,那在这种情况下,若要判断是否存在资源问题,如资源泄露等,则需要手动配置测试配置。
大型SaaS系统的自动化测试常常受制于界面变化快、结构复杂、加载机制多变等因素。从元素识别到脚本管理,SmartBear TestComplete帮助Salesforce建了可靠的自动化测试体系。
相关产品
优秀的界面控件开发包,帮助企业构建卓越应用!
DevExpress DXperience Subscription高性价比的企业级.NET用户界面套包,助力企业创建卓越应用!
DevExpress WinForms Subscription为Windows Forms平台创建具有影响力的业务解决方案,高性价比WinForms界面控件套包。
最新文章 MORE
半岛外围网上直营相关的文章 MORE
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@ke049m.cn
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢
半岛外围网上直营 