We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Make FormField Visible

Hi All,

I've been working with the creation of PDFs and then loading them into the PdfViewer, but there is something that we want to do we haven't figure it out yet, maybe you guys can help us.


When we create a Pdf we want to show a date field, so we went and create an PdfTextBoxField and add values to it as it look like a date field, but we make it visible = false, since we don't want to show it yet. So on the PdfViewer we want that a person once it sign on an SignatureField shows the date as a readonly. We have almost all cover except that the formfield that is a date field is not visible unless we create it as visible = true, when we create the PDf and that make the DateFormField clickable and they can type inside of it, and we don't want that.
My Question is, is there any way to make the Pdfviewer.FormField visible?

Thaks for all your help


This is the code we use to add the datefield into the PDF as Visible = false:

private static void AddDateFormField(PdfLoadedForm form,
PdfPageBase page, FormFieldDto field)
{
RectangleF bounds = GetBounds(field);
PdfTextBoxField textBoxField =
new(page, field.FormFieldName)
{
BorderWidth = 0,
Bounds = bounds,
ForeColor = new PdfColor(Color.DarkGray),
BackColor = new PdfColor(Color.Transparent),
//ReadOnly = field.IsReadOnly,
TabIndex = Convert.ToInt32(field.SequenceNo),
TextAlignment = PdfTextAlignment.Left,
ToolTip = field.ToolTip,
DefaultValue = DateTime.Today.ToString(DocumentConstants.YearMonthDayValue, CultureInfo.InvariantCulture),
//Visible = true
Visible = false
};


This is the code we use to update the date and show it:

protected virtual async Task OnSignature_Added(AddSignatureEventArgs args)
{
//Thread.Sleep(100);


if (ViewerFieldCatalog.ContainsKey(args.Id))
{
FormField viewerField = ViewerFieldCatalog[args.Id];
//await PdfViewerElement.ClearSelectionAsync();
//await PdfViewerElement.UpdateFormFieldsAsync(viewerField);


FormFieldDto currentField = GetTemplateField(viewerField.Id);
if (currentField?.DateFormFieldName is not null)
await UpdateAssociatedDate(currentField);


//FormFieldDto nextTemplateField = GetNextTemplateField(currentField);
//if (nextTemplateField is not null)
// await ScrollToNextField(nextTemplateField);
}


await PdfViewerElement.ClearSelectionAsync();
}


protected async Task UpdateAssociatedDate(FormFieldDto templateField)
{
var dateFieldInstaceId = GetIdFromName(templateField.DateFormFieldName);
if (templateField?.DateFormFieldName is not null)
if (ViewerFieldCatalog.TryGetValue(dateFieldInstaceId, out FormField viewerField))
{
viewerField.Value = DateTime.Today.ToString(DocumentConstants.YearMonthDayValue, CultureInfo.InvariantCulture);
//viewerField.Value = DateTime.Now.ToString("MMMM-d, yyyy");
viewerField.IsReadOnly = true;
await PdfViewerElement.UpdateFormFieldsAsync(viewerField);
}
}


Screenshot before and after signing with visible = false :

BEFORE


AFTER



The code to make it visible: the only thing that change is the visible property

private static void AddDateFormField(PdfLoadedForm form,
PdfPageBase page, FormFieldDto field)
{
RectangleF bounds = GetBounds(field);
PdfTextBoxField textBoxField =
new(page, field.FormFieldName)
{
BorderWidth = 0,
Bounds = bounds,
ForeColor = new PdfColor(Color.DarkGray),
BackColor = new PdfColor(Color.Transparent),
//ReadOnly = field.IsReadOnly,
TabIndex = Convert.ToInt32(field.SequenceNo),
TextAlignment = PdfTextAlignment.Left,
ToolTip = field.ToolTip,
DefaultValue = DateTime.Today.ToString(DocumentConstants.YearMonthDayValue, CultureInfo.InvariantCulture),
Visible = true
//Visible = false
};

And this screenshot is when we make visible = true


BEFORE




AFTER





3 Replies 1 reply marked as answer

VV Visvesvar Venkatesan Syncfusion Team March 3, 2023 09:01 AM UTC

We have provided a sample to make the text box field visible and read-only and we have checked that the text box field background color gets removed when we set the property to read only and it is a behavior of the product.


Code Snippet:


PdfLoadedDocument document = new PdfLoadedDocument(fileStream);

                    PdfLoadedPage page = document.Pages[0] as PdfLoadedPage;          

                   

                    //Get the loaded form.

                    PdfLoadedForm loadedForm = document.Form;

                   

                    PdfTextBoxField textBoxField =new(page, "Date")

{

       BorderWidth = 0,

       Bounds = new Syncfusion.Drawing.RectangleF(100, 100, 50, 50),

       ForeColor = new PdfColor(Color.DarkGray),

       BackColor = new PdfColor(Color.Transparent),

       //ReadOnly = field.IsReadOnly,

       //   TabIndex = Convert.ToInt32(field.SequenceNo),

       TextAlignment = PdfTextAlignment.Left,

       ToolTip = "Date",

       Text="25-12-2022",

       DefaultValue = "25-12-2202",

       Visibility = PdfFormFieldVisibility.Visible,

      

      

};

 

                    document.Form.Fields.Add(textBoxField);

 

            

                    //Set the form as read only.

                    loadedForm.SetDefaultAppearance(true);

                    loadedForm.ReadOnly = false;

                   

                    MemoryStream ms = new MemoryStream();         

                    //Save and close the document.            

                    document.Save(ms);

                    ms.Position = 0;

                    File.WriteAllBytes("Output_1.pdf", ms.ToArray());

                    document.Close(true);

             }

       }


You can change the visibility and read-only from the above-highlighted property.


Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/Net5.0Sample_-_latest-1207997154.zip


Kindly try the above sample and let us know if you have further queries.



LC Luis Campuzano March 3, 2023 06:23 PM UTC

Hi, 

Thanks for the response.


Yes, it works for me, the only think is I did the readOnly on the Field and not on the whole document, but you gave me an idea and it worked.


Thanks


Marked as answer

VV Visvesvar Venkatesan Syncfusion Team March 6, 2023 08:28 AM UTC

We are glad that your issue is resolved and we are closing the forum. Kindly revert to us if you have any other queries. 


Loader.
Up arrow icon