原創|使用教程|編輯:龔雪|2019-01-14 10:10:18.000|閱讀 1698 次
概述:本文主要介紹如何使用DevExpress Reports和PDF Viewer創建AcroForm Designer,DevExpress WinForms v18.2全新發布,歡迎下載新版試用哦!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
眾所周知,交互式表單(AcroForms)是PDF標準的重要組成部分,AcroForms允許開發者和開發者的用戶創建可填寫的PDF文檔。盡管WinForms和WPF PDF Viewers不支持交互式表單生成,但開發者可以在應用程序中加入AcroForm designer,并提供兩種簡單的解決方法。本文主要為大家介紹如何使用DevExpress Reports和PDF Viewer創建AcroForm Designer。
如果您從頭開始生成文檔或需要將AcroForms與現有報表集成,則可以使用來創建交互式表單,您需要:
要在生成的PDF文檔中啟用編輯,請在設計器中將EditOptions | Enabled屬性設置為true(如下圖所示),或在將控件添加到報表時在代碼中啟用編輯模式:
var designForm = new XRDesignForm(); designForm.DesignMdiController.DesignPanelLoaded += (panel, args) => ((XRDesignPanel)panel).ComponentAdded += (s, componentEventArgs) => { XRLabel label = componentEventArgs.Component as XRLabel; if (label != null) label.EditOptions.Enabled = true; XRCheckBox checkBox = componentEventArgs.Component as XRCheckBox; if (checkBox != null) checkBox.EditOptions.Enabled = true; }; designForm.OpenReport(report); designForm.ShowDialog();
要將這些字段作為交互式表單字段導出為PDF,請通過設計器的UI或代碼啟用將編輯字段導出到AcroForms:
report.ExportToPdf(pdfFileName, new PdfExportOptions { ExportEditingFieldsToAcroForms = true });
DevExpress安裝附帶了WinForms,WPF,ASP和ASP NET.Core平臺的電子表格演示,請注意查看實施細節,點擊立即下載>>
此操作允許開發者為現有PDF文檔(例如掃描的紙質表單)創建交互式表單,它使用PDF Document API將AcroForm子彈添加到WinForms/WPF PDF Viewer中顯示的PDF文檔中。API允許您根據需要修改表單字段外觀和操作。
請按照以下步驟將文本框字段添加到文檔:
1. 使用PdfViewer.GetDocumentPosition方法指定表單字段的邊界及其在頁面上的位置。
使用MouseDown和MouseUp事件繪制文本框字段(基于指定的坐標), GetDocumentPosition方法將鼠標坐標轉換為文檔中的頁面坐標。
int pageNumber; PdfPoint c1, c2; void pdfViewer_MouseDown(object sender, MouseEventArgs e) { var documentPosition = pdfViewer.GetDocumentPosition(e.Location, true); // Obtain the page number where the text box should be placed. pageNumber = documentPosition.PageNumber; // Obtain the page coordinates. c1 = documentPosition.Point; } void pdfViewer_MouseUp(object sender, MouseEventArgs e) { // Obtain the page coordinates. c2 = pdfViewer.GetDocumentPosition(e.Location, true).Point; } // Create the rectangle that specifies the text box's size and location. var fieldBounds = new PdfRectangle(Math.Min(c1.X, c2.X), Math.Min(c2.Y, c2.Y), Math.Max(с1.X, с2.X), Math.Max(с1.Y, с2.Y));
2. 在文檔頁面上創建文本框字段。
var field = new PdfAcroFormTextBoxField(“FieldName”, pageNumber, fieldBounds) { Text = “Initial value”, Multiline = true, Type = PdfAcroFormTextFieldType.PlaneText, TextAlignment = PdfAcroFormStringAlignment.Far, Appearance = Appearance.CreateAcroFormFieldAppearance(), ReadOnly = false, Required = true, Print = true, ToolTip = “Text form field tooltip”; }
3. 修改表單字段外觀。
創建PdfAcroFormFieldAppearance的實例并指定字段的背景和前景顏色,顯示其邊框并配置字段文本的字體屬性。
public PdfAcroFormFieldAppearance CreateAcroFormFieldAppearance() { var acroFormFieldAppearance = new PdfAcroFormFieldAppearance(); acroFormFieldAppearance.BackgroundColor = ToPdfColor(BackgroundColor); acroFormFieldAppearance.ForeColor = ToPdfColor(ForeColor); acroFormFieldAppearance.BorderAppearance = new PdfAcroFormBorderAppearance() { Color = ToPdfColor(BorderColor), Width = BorderWidth, Style = BorderStyle }; Font font = Font; if (font == null) { acroFormFieldAppearance.FontFamily = null; acroFormFieldAppearance.FontSize = 0; acroFormFieldAppearance.FontStyle = PdfFontStyle.Regular; } else { acroFormFieldAppearance.FontFamily = font.FontFamily.Name; acroFormFieldAppearance.FontSize = font.SizeInPoints; acroFormFieldAppearance.FontStyle = (PdfFontStyle)font.Style; } return acroFormFieldAppearance; }
請注意每個PDF顏色分量由范圍(0,1)內的浮點值表示。 將GDI顏色轉換為PDF顏色,如下所示:
static PdfRGBColor ToPdfColor(Color color) { return color.IsEmpty ? null : new PdfRGBColor(color.R / 255.0, color.G / 255.0, color.B / 255.0); }
開發者可以使用相同的方法創建其他表單字段類型,有關交互式表單域的其他信息,請查看以下文檔主題。
演示中使用了此實現。 您可以在DevExpress演示源目錄(C:\ Users \ Public \ Documents \ DevExpress Demos 18.2 \ Components \ Office File API \ ...)中找到。
注意:目前不提供基于Web的PDF Viewer,但是開發者可以使用PDF Document API為PDF Viewer for ASP.NET and MVC創建自定義Viewer,創建完成后,可以使用此處的相同方法將交互式表單域添加到PDF文檔中。
===============================================================
掃描關注DevExpress中文網微信公眾號,及時獲取最新動態及最新資訊
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都控件網