Fill dynamic data to a field in a pdf and make it readonly


#1

Hi team,
I have used aspose.pdf to fill data to the pdf field. After the data is filled in the pdf, and I downloaded the pdf the rates shown in the filed is editable, but it should be readonly. How do I make that field readonly while writing data in it. I have other filed called "Contact me today for
more information"on the same pdf which should be writeable after downloading.

In short: How to make only specific filed of dynamic pdf readonly after writing data in it. Can you please help me with this asap? Thank you.

Here is the code I have used:
public byte[] FillForm(string IPAddress, string SessionID, string UserName, string pdfPath, IDictionary<string, string> values)
{
var document = GetPdfDocument(_fileSystem.GetFullPath(pdfPath));
if (document.Form.Fields.Any())
{
int count = 0;
foreach (var field in document.Form.Fields.Where(x => !string.IsNullOrEmpty(x.FullName)))
{
var rateMapperSplittedValue = values.ElementAt(count).Value.Split(’;’);
count++;
int index = rateMapperSplittedValue.Length - 1;
string labelValue = rateMapperSplittedValue[index];
field.Value = labelValue;
}
}
var ms = new MemoryStream();
document.Save(ms);
return ms.GetBuffer();
}

Here is the downloaded pdf. RatesFilledDownloadedPdf.PDF (1.4 MB)


#2

@santosh.g2047

Thank you for contacting support.

We are afraid that the PDF document attached by you is corrupted one so please share a valid PDF document which is generated in your environment. Corrupted.JPG

Moreover, please try to make the fields read only with below code snippet and Aspose.PDF for .NET 19.9. You may avoid flattening of specific fields with continue statement in foreach loop if fieldName matches name of your field.

Document doc = new Document(dataDir + "dynamic_form_original.pdf");
foreach (string fieldName in doc.Form.XFA.FieldNames)
{
    try
    {
        XmlNode template = doc.Form.XFA.GetFieldTemplate(fieldName);
        XmlAttribute access = template.OwnerDocument.CreateAttribute("access");
        access.Value = "readOnly";
        template.Attributes.Append(access);
    }
    catch (Exception ex)
    {
        continue;
    }
}
doc.Save(dataDir + "xfa_form_original_flattened_19.2.pdf");

#3

Thank you Farhan for the reply. Actually I found a way to make the certain field only to be ReadOnly. Here is the modified code from my original code.

public byte[] FillForm(string IPAddress, string SessionID, string UserName, string pdfPath, IDictionary<string, string> values)
{
var document = GetPdfDocument(_fileSystem.GetFullPath(pdfPath));

        if (document.Form.Fields.Any())
        {

            int count = 0;
            foreach (var field in document.Form.Fields.Where(x => !string.IsNullOrEmpty(x.FullName)))
            {
                var rateMapperSplittedValue = values.ElementAt(count).Value.Split(';');
                count++;
                int index = rateMapperSplittedValue.Length - 1;
                string labelValue = rateMapperSplittedValue[index];
                field.Value = labelValue;
                if (field.FullName != "undefined" && field.FullName != "Information" && field.FullName != "Text1")
                    field.ReadOnly = true;
            }
        }
        var ms = new MemoryStream();
        document.Save(ms);
        return ms.GetBuffer();
    }

#4

@santosh.g2047

Thank you for your valuable feedback.

We are glad to know that you have been able to achieve your requirements and explaining it here will certainly help other community members as well.

Please feel free to contact us if you need any further assistance.