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

Edit a PDF file and save it.

Hello

I have a PDF file which I want to open, replace some text fields which are containing dots (...) with real values and then save the PDF.

Is this possible?

I only found out how to extract text lines, but can I somehow directly edit text fields and then save the PDF again?

Thanks

Dominik


3 Replies

IJ Irfana Jaffer Sadhik Syncfusion Team March 8, 2023 12:06 PM UTC

Please find the below code snippet to achieve your requirement:

 PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputStream);


        //Get the loaded form.

        PdfLoadedForm loadedForm = loadedDocument.Form;



        //Set the default appearance.

        loadedForm.SetDefaultAppearance(false);


        foreach (PdfLoadedField field in loadedForm.Fields)

        {

            if (field is PdfLoadedTextBoxField)

            {

                PdfLoadedTextBoxField textbox = field as PdfLoadedTextBoxField;

                if (textbox.Text == "...")

                {

                    textbox.Text = "value";

                }

            }

        }


        MemoryStream ms = new MemoryStream();


        //Save the document


        loadedDocument.Save(ms);

        ms.Position = 0;


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


        //Close the document


        loadedDocument.Close();





follow the below links for more information,

https://help.syncfusion.com/file-formats/pdf/working-with-forms#trygetfield

https://help.syncfusion.com/file-formats/pdf/working-with-forms






DR Dominik Reber March 8, 2023 01:04 PM UTC

Hi and thanks a lot.

loadedDocument.Form is null for my document, does this mean that it is not well formated?

I can extract text from the same loadedDocument like this:


            PdfPageBase page = loadedDocument.Pages[2];

            TextLineCollection lineCollection = new TextLineCollection();

            string extractedText = page.ExtractText(out lineCollection);

That does work / returns the values as text, but I cannot access the "Form"


IJ Irfana Jaffer Sadhik Syncfusion Team March 9, 2023 10:34 AM UTC

Thank you for sharing the details. If the loaded form is Null, it seems there are no form fields in the PDF document. The extract text API is to extract the text drawn in the PDF document.  This will not return the values in the Fields.

 If your requirement is to replace the text extracted from the PDF document. Please refer to the below documentation link to achieve the same on you end:

https://www.syncfusion.com/kb/8564/how-to-replace-the-existing-text-in-the-pdf-document

If still you are facing an issue, we request you to share the input document, a simple sample, and steps to replicate the issue on our end. So that we can assist with you further in this


Loader.
Live Chat Icon For mobile
Up arrow icon