翻譯|使用教程|編輯:龔雪|2022-01-17 09:59:38.410|閱讀 200 次
概述:春節臨近,你還不會制作電子賀卡、電子邀請函?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@fc6vip.cn
文章轉載自:慧都網