PDF clown field value editing

399 Views Asked by At

I am using the PDF clown library to edit a pre-existing PDF (mass autofilling them based on user input) and i'm having trouble changing the form fields (specifically the textfields) value, when attempting to run the code below i will be given this error which is highlighted on the code sample below

" System.InvalidCastException: 'Unable to cast object of type 'org.pdfclown.objects.PdfName' to type 'org.pdfclown.objects.IPdfNumber'.'"

the block of code im having trouble with is a loop that runs through each field and attempts to locate the field matching the name and then altering that fields Value to match the hardcoded string

    using org.pdfclown.documents;
using org.pdfclown.documents.contents.composition;
using org.pdfclown.documents.contents.entities;
using org.pdfclown.documents.contents.fonts;
using org.pdfclown.documents.contents.xObjects;
using org.pdfclown.documents.interaction.annotations;
using org.pdfclown.documents.interaction.forms;
using org.pdfclown.documents.files;
using org.pdfclown.objects;
using org.pdfclown.files;
using files = org.pdfclown.files;


using System;
using System.Collections.Generic;
using System.Drawing;



namespace PDFedit
{
    class PDFLoader
    {
        
        public static void load (string path )
        {
            string filepath = path;
            File file = new File(filepath);
            Document document = file.Document;
            Form form = document.Form;
            Fields fields = form.Fields;
            string value = "william";
            
            foreach (Field field in form.Fields.Values)
            {
                
                if (field.Name == "Testtext") 
                {
                    
                    string typeName = field.GetType().name;
                    
                    field.value = "data to be written into the field"    // this is the line that throws 
                                                                            the error
                    
                    Console.WriteLine("    Type: " + typeName);
                    Console.WriteLine("    Value: " + field.Value);
                    Console.WriteLine("    Data: " + field.BaseDataObject.ToString());
                    
                } 
                

            

            };

            file.Save();
            

        }
    }
}

Apologies if i've committed any fauxpas in asking a question, this is my first post and i'm new to programming outside of a tutorial environment.

0

There are 0 best solutions below