0

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.

nilsK
  • 4,323
  • 2
  • 26
  • 40
Lydess
  • 33
  • 6
  • 1
    Hi, don't know that framework, but the line `foreach (Field field in form.Fields.Values)` made me curious. Shouldn't it be `foreach (Field field in form.Fields)`? Welcome to SO =) – nilsK Feb 19 '21 at 14:01
  • @nilsK I thought the same thing but the sample code provided in the documentation had that block in it, when removing the .Values it fails to compile. – Lydess Feb 20 '21 at 12:52
  • The class `Fields` implements `IDictionary`, mapping field names to the respective field. Thus, iterating over its values makes sense. – mkl Feb 20 '21 at 18:02

0 Answers0