|
| 1 | +@page "/" |
| 2 | + |
| 3 | +@using Syncfusion.Blazor.Buttons; |
| 4 | +@using Syncfusion.Blazor.DocumentEditor; |
| 5 | +@using System.Text.Json |
| 6 | +@using Syncfusion.Blazor.Inputs |
| 7 | +@using Syncfusion.Blazor.Popups |
| 8 | + |
| 9 | +<div class="control-section"> |
| 10 | + <div> |
| 11 | + <div> |
| 12 | + <SfDocumentEditorContainer UseDefaultEditor="false" Width="1000px" Height="590px"> |
| 13 | + <SfDocumentEditor @ref="documentEditor"> |
| 14 | + <DocumentEditorEvents Created="LoadDocument" BeforeFormFieldFill="BeforeFormFill"></DocumentEditorEvents> |
| 15 | + </SfDocumentEditor> |
| 16 | + </SfDocumentEditorContainer> |
| 17 | + </div> |
| 18 | + </div> |
| 19 | +</div> |
| 20 | + |
| 21 | +<SfDialog AllowPrerender="true" IsModal="true" @bind-Visible="showSignatureDialog" AllowDragging=true CloseOnEscape="true" Height="300px" Width="600px"> |
| 22 | + <DialogTemplates> |
| 23 | + <Header> |
| 24 | + Signature |
| 25 | + </Header> |
| 26 | + <Content> |
| 27 | + @if(isOpen) { |
| 28 | + <SfSignature @ref="signature" IsReadOnly="false" style="height:100%; width: 100%;"></SfSignature> |
| 29 | + } |
| 30 | + </Content> |
| 31 | + </DialogTemplates> |
| 32 | + <DialogButtons> |
| 33 | + <DialogButton CssClass="e-flat" Content="OK" IsPrimary="true" OnClick="@InsertSignature"></DialogButton> |
| 34 | + <DialogButton CssClass="e-flat" Content="Cancel" IsPrimary="true" OnClick="@CloseDialog"></DialogButton> |
| 35 | + </DialogButtons> |
| 36 | + <DialogEvents Opened="@OnOpenHandler" Closed="@DialogClosed"></DialogEvents> |
| 37 | +</SfDialog> |
| 38 | + |
| 39 | +@code { |
| 40 | + SfDocumentEditor? documentEditor; |
| 41 | + |
| 42 | + SfSignature? signature; |
| 43 | + |
| 44 | + bool isOpen = false; |
| 45 | + |
| 46 | + bool showSignatureDialog = false; |
| 47 | + |
| 48 | + public async void InsertSignature() |
| 49 | + { |
| 50 | + await documentEditor.Selection.SelectFieldAsync(); |
| 51 | + string image = await signature.GetSignatureAsync(SignatureFileType.Png); |
| 52 | + await documentEditor.Editor.InsertImageAsync(image, 150, 30); |
| 53 | + showSignatureDialog = false; |
| 54 | + StateHasChanged(); |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + public void BeforeFormFill(FormFieldFillEventArgs args) |
| 59 | + { |
| 60 | + // Check whether form field name match the signature field. |
| 61 | + if (args.FieldName == "signature") |
| 62 | + { |
| 63 | + //Cancel form field fill. |
| 64 | + args.IsCanceled = true; |
| 65 | + showSignatureDialog = true; |
| 66 | + StateHasChanged(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private void OnOpenHandler(Syncfusion.Blazor.Popups.OpenEventArgs args) |
| 71 | + { |
| 72 | + isOpen = true; |
| 73 | + } |
| 74 | + |
| 75 | + private void DialogClosed(CloseEventArgs args) |
| 76 | + { |
| 77 | + isOpen = false; |
| 78 | + } |
| 79 | + |
| 80 | + private void CloseDialog() |
| 81 | + { |
| 82 | + showSignatureDialog = false; |
| 83 | + StateHasChanged(); |
| 84 | + } |
| 85 | + |
| 86 | + private async void LoadDocument() |
| 87 | + { |
| 88 | + string filePath = "wwwroot/data/Form Fields.docx"; |
| 89 | + using (FileStream fileStream = new FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)) |
| 90 | + { |
| 91 | + WordDocument document = WordDocument.Load(fileStream, ImportFormatType.Docx); |
| 92 | + string json = JsonSerializer.Serialize(document); |
| 93 | + document.Dispose(); |
| 94 | + await documentEditor.OpenAsync(json); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
0 commit comments