Open existing Pdf document and add pages to the document.

I am trying to open an existing pdf document and add Page(s) to it. Below is just a line of my code, but I need to add another page and create my fields on this new page. Adding a Syncfusion.Pdf.PdfLoadedPage seems to be different from adding a Syncfusion.pdf.pdfpage. 


    //Load the page


            PdfLoadedPage loadedPage = loadedDocument.Pages[0] as PdfLoadedPage;


I downloaded a sample project using this link. How to add form fields in an existing PDF file using C# and VB.NET | Syncfusion. I am developing in .Net Maui on windows so not sure if this sample will work for me. However, it does open a Pdf document, but the problem is that it overlays the page on the existing documents page as oppossed to writing on a new page. 


1 Reply

RB Ravikumar Baladhandapani Syncfusion Team September 29, 2023 04:07 PM UTC

We can add a form field to the newly inserted page in the existing pdf document. Please refer the below code snippet,

           PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);

            //Insert a new page in the beginning of the document.

            loadedDocument.Pages.Insert(0);

            if (loadedDocument.Form == null)

                loadedDocument.CreateForm();

            //Load the page

            PdfPage page = loadedDocument.Pages[0] as PdfPage;

 

            //Set the standard font

            PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 16);

            //Draw the string     

            page.Graphics.DrawString("Job Application", font, PdfBrushes.Black, new PointF(250, 30));

            font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);

 

            page.Graphics.DrawString("Name", font, PdfBrushes.Black, new PointF(30, 50));

            //Create a text box field for name

            PdfTextBoxField textBoxField1 = new PdfTextBoxField(page, "Name");

            textBoxField1.Bounds = new RectangleF(30, 70, 200, 20);

            textBoxField1.ToolTip = "Name";

            loadedDocument.Form.Fields.Add(textBoxField1);

 

            page.Graphics.DrawString("Email address", font, PdfBrushes.Black, new PointF(30, 110));

            //Create a text box field for email address

            PdfTextBoxField textBoxField3 = new PdfTextBoxField(page, "Email address");

            textBoxField3.Bounds = new RectangleF(30, 130, 200, 20);

            textBoxField3.ToolTip = "Email address";

            loadedDocument.Form.Fields.Add(textBoxField3);

 

            page.Graphics.DrawString("Gender", font, PdfBrushes.Black, new PointF(30, 180));

            //Create radio button for gender

            PdfRadioButtonListField employeesRadioList = new PdfRadioButtonListField(page, "Gender");

            loadedDocument.Form.Fields.Add(employeesRadioList);

            page.Graphics.DrawString("Male", font, PdfBrushes.Black, new PointF(60, 200));

            PdfRadioButtonListItem radioButtonItem1 = new PdfRadioButtonListItem("Male");

            radioButtonItem1.Bounds = new RectangleF(30, 200, 20, 20);

            page.Graphics.DrawString("Female", font, PdfBrushes.Black, new PointF(160, 200));

            PdfRadioButtonListItem radioButtonItem2 = new PdfRadioButtonListItem("Female");

            radioButtonItem2.Bounds = new RectangleF(130, 200, 20, 20);

            employeesRadioList.Items.Add(radioButtonItem1);

            employeesRadioList.Items.Add(radioButtonItem2);


Please refer the below documentation for further reference.

https://help.syncfusion.com/file-formats/pdf/working-with-pages#inserting-pages-in-a-document

https://github.com/SyncfusionExamples/PDF-Examples/tree/master/Pages/Insert-pages-in-a-PDF-document/

https://help.syncfusion.com/file-formats/pdf/working-with-forms?cs-save-lang=1&cs-lang=csharp

https://github.com/SyncfusionExamples/PDF-Examples/tree/master/Forms/Add-the-textbox-to-an-existing-PDF-document


Kindly try the above code snippet on your end and let us know , if you need any further assistance on this.


Loader.
Up arrow icon