0

我正在使用 PDF 小丑库来编辑预先存在的 PDF(根据用户输入批量自动填充它们),并且在尝试运行下面的代码时我无法更改表单字段(特别是文本字段)值鉴于此错误在下面的代码示例中突出显示

“ System.InvalidCastException:'无法将'org.pdfclown.objects.PdfName'类型的对象转换为'org.pdfclown.objects.IPdfNumber'。'”

我遇到问题的代码块是一个循环,它遍历每个字段并尝试找到与名称匹配的字段,然后更改该字段的值以匹配硬编码的字符串

    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();
            

        }
    }
}

抱歉,如果我在提出问题时犯了任何错误,这是我的第一篇文章,而且我是在教程环境之外编程的新手。

4

0 回答 0