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

Write to form fields in PDF

I am trying to write to a formfield in an exisiting PDF
Added a folder to the Solution with the name Document and in the folder the pdf with the name Boord.pdf

Want to write to a formfield and open the pdf

When starting the debug nothing is happening when i make some text in Entry and push the button , no error nothing.

What am i missing . Think the problem is place of the document maybe ?


 BackgroundColor="LightGray">
    <ContentPage.Content>
        <AbsoluteLayout>
            <button:SfButton x:Name="Bekijken" Text="PDF"  AbsoluteLayout.LayoutBounds=".90,.49,.30,.08" AbsoluteLayout.LayoutFlags="All" FontSize="Medium" TextColor="White" BackgroundColor="Gray" BackgroundImage="Assortiment" HorizontalTextAlignment="Center" Clicked="OnBekijkenButtonClicked"  />


            <Entry x:Name="Plaats"
                   AbsoluteLayout.LayoutBounds=".100,.49,.35,.08" AbsoluteLayout.LayoutFlags="All"
    Placeholder="" />
         

        </AbsoluteLayout>
    </ContentPage.Content>
</ContentPage>


  void OnBekijkenButtonClicked(object sender, EventArgs args)
        {
            Stream docStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Document.Boord.pdf");
            PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
            //Loads the form
            PdfLoadedForm form = loadedDocument.Form;
            //Fills the textbox field by using index
            
            (form.Fields["f1_01(0)"] as PdfLoadedTextBoxField).Text = Plaats.Text;
           
            //Save the PDF document to stream.
            MemoryStream stream = new MemoryStream();
            loadedDocument.Save(stream);
            //Close the document.
            loadedDocument.Close(false);

        }

9 Replies

SL Sowmiya Loganathan Syncfusion Team November 29, 2019 10:36 AM UTC

Hi Boris, 

Thank you for contacting Syncfusion support.  

We have create the sample to fill form fields in an existing PDF document in Xamarin Forms platform. Please find the downloaded link from below, 


Please try the above sample in your end and let us know if you need any further assistance on this.  

Regards, 
Sowmiya Loganathan 



BO Boris Oprit November 29, 2019 01:59 PM UTC

Thanks for the reply and sample .

But it is not working correct , on the pdf is the word Name and a Formfield.

When printing it is only showing the word Name and nothing else , no text in Fromfield.


BO Boris Oprit November 29, 2019 08:17 PM UTC

Well , 

Seams to work but only in Android and not in the IOS simulator.

Don't know why


BO Boris Oprit November 29, 2019 08:49 PM UTC

Jep is working now .

Found this here on the forum , added and works




            loadedDocument.Form.Flatten = true;

            //Flattens the first field //form.Fields[0].Flatten = true;

            //Add the form field to the document

            loadedDocument.Form.Fields.Add(loadedTextBoxField);





BO Boris Oprit November 29, 2019 08:51 PM UTC

Other question,

Why can't i use another pdf ?

When i change the pdf with form fields it is giving an error when i want to write to it.

The pdf is named Boord 

The error is

System.ArgumentNullException
  Message=Value cannot be null.
Parameter name: file


and the button click

  void OnButtonClicked(object sender, EventArgs args)
        {
            //Load PDF 
            Stream docStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("GettingStarted.Assets.Boord.pdf");
            PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);

            //Get the loaded form
            PdfLoadedForm loadedForm = loadedDocument.Form;

            //Get the loaded text box field and fill it
            PdfLoadedTextBoxField loadedTextBoxField = loadedForm.Fields[0] as PdfLoadedTextBoxField;

            loadedTextBoxField.Text = "First Name";



            loadedDocument.Form.Flatten = true;

            //Flattens the first field //form.Fields[0].Flatten = true;

            //Add the form field to the document

            loadedDocument.Form.Fields.Add(loadedTextBoxField);



            //Save the document to the stream
            MemoryStream stream = new MemoryStream();
            loadedDocument.Save(stream);

            //Close the document
            loadedDocument.Close(true);

            //Save the stream as a file in the device and invoke it for viewing
            Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("Output.pdf", "application/pdf", stream);
        }


SL Sowmiya Loganathan Syncfusion Team December 2, 2019 12:28 PM UTC

Hi Boris, 
 
Why can't i use another pdf ? 
 
When i change the pdf with form fields it is giving an error when i want to write to it. 
 
The pdf is named Boord  
 
The error is 
 
System.ArgumentNullException 
  Message=Value cannot be null. 
Parameter name: file 
 
We suspect that the issue occurs due to loading of PDF in incorrect format. While loading the PDF as stream, it should be in Assets folder and be set as “Embedded Resources”.  
 
Please try the below solution to overcome this issue.  
 
Right click on the PDF file in Assets folder > Properties > Set Embedded Resources for Build Action.  
 
 
 
Please try the above solution in your end and let us know the result.  
 
 
Regards, 
Sowmiya Loganathan 



BO Boris Oprit December 2, 2019 07:25 PM UTC

Thanks ,

Is working now .


BO Boris Oprit December 25, 2019 10:11 AM UTC

Hello ,

It is working and showing the document , but i have an error when the Entry name Plaats is empty.


System.ArgumentNullException
  Message=Value cannot be null.
Parameter name: text


How do i change it so it can be empty



 void OnBekijkenButtonClicked(object sender, EventArgs args)
        {
            Stream docStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Document.Boord.pdf");
            PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
            //Loads the form
            PdfLoadedForm form = loadedDocument.Form;
            //Fills the textbox field by using index
            
            (form.Fields["f1_01(0)"] as PdfLoadedTextBoxField).Text = Plaats.Text;
           
            //Save the PDF document to stream.
            MemoryStream stream = new MemoryStream();
            loadedDocument.Save(stream);
            //Close the document.
            loadedDocument.Close(false);

        }


SL Sowmiya Loganathan Syncfusion Team December 30, 2019 12:27 PM UTC

Hi Boris, 

We have analyzed your requirement. If you want to fill the form fields in PDF document without entering the text in Entry (name - Plaats), kindly use the below code snippet in XAML page to overcome the reported issue “Argument null exception”.   

Code snippet (MainPage.xaml):  
 
<AbsoluteLayout> 
     
    <Button x:Name="Bekijken" Text="Generate Document" Clicked="OnButtonClicked" VerticalOptions="Center" HorizontalOptions="Center"/> 
     
    <Entry x:Name="Plaats" AbsoluteLayout.LayoutBounds=".100,.49,.35,.08" AbsoluteLayout.LayoutFlags="All" Placeholder="" Text=""/> 
 
</AbsoluteLayout> 

Please try the above solution in your end and let us know the result.  

Regards, 
Sowmiya Loganathan 


Loader.
Live Chat Icon For mobile
Up arrow icon