Search code examples
c#pdfsyncfusion

No Form to add fields to on a Syncfusion PDF


I'm using some Syncfusion PDF tools in a couple of my programs. One program reads the fields (and does some record keeping based on some property values) from the PdfLoadedDocument's Form property via a foreach loop. This works fine.

I have another program that takes PDF documents that don't always seem to have a Form property (doc.Form is null). Obviously this is a problem when trying to add a field like this:

PdfLoadedDocument PDF = new PdfLoadedDocument(@"C:\Me\Desktop\test.pdf");
PdfLoadedForm Form = PDF.Form; //Form is null
PdfLoadedPage Page = (PdfLoadedPage)PDF.Pages[0];

//Problem is, form is null for this line
PDF.Form.Fields.Add(new TextField(Page, "TEST_NAME"));

There doesn't seem to be any way to set/create a new Form to add to the document from what I can find in the Syncfusion docs. This makes whatever comes out of this program to not function as expected from the program I mentioned earlier (no Form to read fields from). Am I missing something here or going about this the wrong way?


Solution

  • We have to create the form before add the fields when an existing PDF document has empty form fields.

    Please refer to the following code snippet :

    //Load the existing PDF document.
    PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputfile);
    
    //Create the form if the form does not exist in the loaded document
    if (loadedDocument.Form == null)
        loadedDocument.CreateForm();
    

    Please let us know if you have any concern.