• 如何在Windows中使用C#填写和提取PDF表单


    如何在Windows中使用C#填写和提取PDF表单

    PDF表单不仅允许用户填写和提交数据,也允许用户创建各种表单域收集用户的数据,并通过提取表单字段值,将收集和合并提交的数据进一步分析或处理。PDF通过电子方式填写、保存和共享的形式,不仅减少了对纸质和手动数据输入的需求,还方便个人和组织使用。这也是PDF表单广泛应用于填写问卷调查表、注册表单、反馈表单或政府税表等的原因。

    ComPDFKit C#库,允许您轻松快速地将 PDF 功能(如查看、批注、表单填写、签名和文档编辑)集成到 Windows 应用程序中。

    PDF表单可以通过C#在PDF中创建表单域、编辑表单域、填写表单域、提取表单域、删除表单域和拼合PDF表单等。在本文中,我们将探讨以下2个 C# 语言在PDF表单中的使用方法:

    1. 填写PDF表单域

    2. 提取PDF表单填写的信息

    1. 填写PDF表单域

    在C#中使用ComPDFKit SDK填写PDF表单域非常简单。我们将通过填写以下表单中的信息作为示例,来演示如何在PDF文档中填写表单域。

    在 PDF 文档中,您可能会遇到各种类型的表单域,例如文本域、复选框、单选按钮、列表框和组合框(下拉列表)、按钮键。在填写表单字段值之前,确定每个表单字段的特定类型至关重要。识别后,再使用代码填充表单域设置它们的值。请按照以下步骤填写PDF文档中的表单字段:

    (1) 使用CPDFDocument document 类加载创建的PDF文档。

    (2) 使用CPDFPage page类从PDF获取表单。

    (3) 使用CPDFWidget 获取表单字段类型和表单域,如文本框和复选框,然后用必要的信息填充它们。

    (4) 最后,使用保存方法保存填写的PDF文档。

    下面的代码示例演示如何在PDF文档中填写表单域:

     //replace it to real pdf doc path
     string docPath = "";
     List<string> textFillList = new List<string>()
     {
         "ComPDFKit",
         "support@compdf.com",
         "(65)3398 9876"
     };
    
     //init document
     CPDFDocument pdfDoc = CPDFDocument.InitWithFilePath(docPath);
    
     //get all the forms on the first page
     CPDFPage pdfPage = pdfDoc.PageAtIndex(0);
     List<CPDFWidget> widgetList = pdfPage.GetAnnotations().AsEnumerable()
         .Where(x => x.Type == C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET)
         .Cast<CPDFWidget>()
         .ToList();
    
     //get textboxs from above form collection
     List<CPDFTextWidget> textWidgetList = widgetList.AsEnumerable()
         .Where(x => x.WidgeType == C_WIDGET_TYPE.WIDGET_TEXTFIELD)
         .Cast<CPDFTextWidget>()
         .ToList();
    
     //get first radiobutton from above form collection
     CPDFRadioButtonWidget radioButtonWidget = widgetList.AsEnumerable()
         .Where(x => x.WidgeType == C_WIDGET_TYPE.WIDGET_RADIOBUTTON)
         .Cast<CPDFRadioButtonWidget>()
         .First();
    
     //get first checkbox from above form collection
     CPDFCheckBoxWidget checkBoxWidget = widgetList.AsEnumerable()
         .Where(x => x.WidgeType == C_WIDGET_TYPE.WIDGET_CHECKBOX)
         .Cast<CPDFCheckBoxWidget>()
         .First();
    
     //get first combobox from above form collection
     CPDFComboBoxWidget comboboxWidget = widgetList.AsEnumerable()
         .Where(x => x.WidgeType == C_WIDGET_TYPE.WIDGET_COMBOBOX)
         .Cast<CPDFComboBoxWidget>()
         .First();
    
     //set textbox's text value
     for (int i = 0; i < textWidgetList.Count && i < 3; i++)
     {
         CPDFTextWidget textWidget = textWidgetList[i];
         textWidget.SetText(textFillList[i]);
     }
    
     //set radiobutton checked
     radioButtonWidget.SetChecked(true);
    
     ///set checkbox checked
     checkBoxWidget.SetChecked(true);
    
     //set combobox selected index
     comboboxWidget.SelectItem(5);
    
     //save changes to orign document
     pdfDoc.WriteToLoadedPath();
     pdfDoc.Release();
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63

    如果打开表单,您将看到您在C#中使用ComPDFKit SDK已填写的PDF表单字段:

    2. 提取PDF表单填写的信息

    您还可以在C#中使用ComPDFKit SDK从PDF表单中提取表单字段以及字段值。我们将通过提取上述填写在表单中的信息作为示例,来演示如何在PDF文档中提取表单字段值。在提取PDF表单字段值之前,需要浏览所有表单域,再利用其相应的属性来准确提取字段值。具体步骤如下:

    (1) 使用CPDFDocument document 类加载创建的PDF文档。

    (2) 使用CPDFPage page类从PDF获取表单。

    (3) 使用CPDFWidget 获取表单字段类型和表单域,循环访问表单中的所有表单字段和相应的值。

    (4) 最后,将表单域数值写入文本文件中。

    下面的代码示例演示如何在PDF文档中提取表单字段以及字段值:

    //replace it to real pdf doc path
    string docPath = "";
    
    //replace it to real txt path
    string txtPath = "";
    
    //init document
    CPDFDocument pdfDoc = CPDFDocument.InitWithFilePath(docPath);
    
    //get all the forms on the first page
    CPDFPage pdfPage = pdfDoc.PageAtIndex(0);
    List<CPDFWidget> widgetList = pdfPage.GetAnnotations().AsEnumerable()
        .Where(x => x.Type == C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET)
        .Cast<CPDFWidget>()
        .ToList();
    
    //save export fields to txt file
    using (FileStream fs = File.Create(txtPath))
    {
        using (StreamWriter bw = new StreamWriter(fs))
        {
            foreach (CPDFWidget widget in widgetList)
            {
                //export textbox fields
                if (widget.WidgeType == C_WIDGET_TYPE.WIDGET_TEXTFIELD)
                {
                    CPDFTextWidget textWidget = widget as CPDFTextWidget;
                    bw.WriteLine(string.Format("TextBox Name: {0}", textWidget.GetFieldName()));
                    bw.WriteLine(string.Format("TextBox Value: {0}", textWidget.Text));
                    bw.WriteLine();
                }
    
                //export radio button fields
                if (widget.WidgeType == C_WIDGET_TYPE.WIDGET_RADIOBUTTON)
                {
                    CPDFRadioButtonWidget radiobuttonWidget = widget as CPDFRadioButtonWidget;
                    bw.WriteLine(string.Format("Radio Button Name: {0}", radiobuttonWidget.GetFieldName()));
                    bw.WriteLine(string.Format("Radio Button Selected Value: {0}", radiobuttonWidget.GetGroupMemberName()));
                    bw.WriteLine();
                }
    
                //export checkbox fields
                if (widget.WidgeType == C_WIDGET_TYPE.WIDGET_CHECKBOX)
                {
                    CPDFCheckBoxWidget checkboxWidget = widget as CPDFCheckBoxWidget;
                    bw.WriteLine(string.Format("CheckBox Name: {0}", checkboxWidget.GetFieldName()));
                    bw.WriteLine(string.Format("CheckBox Statues: {0}", checkboxWidget.IsChecked()));
                    bw.WriteLine();
                }
    
                //export combobox fields
                if (widget.WidgeType == C_WIDGET_TYPE.WIDGET_COMBOBOX)
                {
                    CPDFComboBoxWidget comboboxWidget = widget as CPDFComboBoxWidget;
                    CWidgetItem[] itemLists = comboboxWidget.LoadWidgetItems();
                    CWidgetItem selectItem = comboboxWidget.GetSelectedItem();
    
                    bw.WriteLine(string.Format("ComboBox Name: {0}", comboboxWidget.GetFieldName()));
                    bw.WriteLine("ComboBox Items:");
    
                    if (itemLists != null && itemLists.Length > 0)
                    {
                        foreach (CWidgetItem item in itemLists)
                        {
                            bw.WriteLine(item.Text);
                        }
                    }
    
                    if (selectItem != null)
                    {
                        bw.WriteLine(string.Format("ComboBox Selected Value: {0}", selectItem.Value));
                    }
                    bw.WriteLine();
                }
            }
        }
    }
    
    pdfDoc.Release();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79

    导出的文本文件内容如下图所示:

    结论

    ComPDFKit SDK 允许C#开发人员在Windows应用程序中集成各种与PDF相关的功能。在本文中,我们学会了利用ComPDFKit SDK使用最简单的代码在C#中填写PDF表单,还了解了在C#中提取PDF表单字段及字段值。

    除了填写和提取PDF表单域值,我们还可以通过C#在PDF中创建表单域、编辑表单域、修改表单域、删除表单域、拼合PDF表单等。查看PDF表单文档,您可以在其中找到其他选项和功能,这些选项和功能都附带代码示例。

    Code Samples

    ComPDFKit PDF API

    Try ComPDFKit Now

  • 相关阅读:
    京东云开发者|mysql基于binlake同步ES积压解决方案
    【一起入门DeepLearning】中科院深度学习_期末总复习
    An overview of IfM Engage
    Unity接入OneStore内购
    Relational Triplet Extraction(RTE)
    【Python刷题篇】——Python入门 09 字典(上)
    使用Python从零实现多分类SVM
    微信小程序业务域名保姆级配置教程
    CPT-mPEG/RGD/cRGD/BSA 甲氧基聚乙二醇/RGD/cRGD多肽/牛血清蛋白修饰顺铂
    快照和镜像
  • 原文地址:https://blog.csdn.net/PDFReaderPro/article/details/132757285