C#提取PDF表單數(shù)據(jù)的實(shí)現(xiàn)流程
引言
PDF表單是一種常見的數(shù)據(jù)收集工具,廣泛應(yīng)用于調(diào)查問卷、業(yè)務(wù)合同等場景。憑借出色的跨平臺(tái)兼容性和標(biāo)準(zhǔn)化特點(diǎn),PDF表單在各行各業(yè)中得到了廣泛應(yīng)用。然而,當(dāng)需要整合、分析或?qū)氪罅恳烟顚懙谋韱螖?shù)據(jù)時(shí),傳統(tǒng)的手動(dòng)處理方式不僅耗時(shí),而且容易出錯(cuò)。因此,掌握自動(dòng)提取PDF表單數(shù)據(jù)的方法,不僅能大幅提高工作效率,還能確保數(shù)據(jù)處理的準(zhǔn)確性。本文將探討如何使用C# 實(shí)現(xiàn)自動(dòng)化PDF表單數(shù)據(jù)提取流程。
- 使用工具
- C# 提取多個(gè)PDF表單域的數(shù)據(jù)
- C# 提取特定PDF表單域的數(shù)據(jù)
使用工具
要使用C# 提取PDF表單的數(shù)據(jù),需要用到合適的PDF文檔處理庫。本文所使用的是Spire.PDF for .NET庫。該庫主要用于在 .NET 應(yīng)用程序中創(chuàng)建、讀取、編輯、轉(zhuǎn)換 和打印PDF 文檔。
安裝 Spire.PDF for .NET
你可以在 NuGet 包管理器中運(yùn)行以下命令安裝 Spire.PDF for .NET:
PM> Install-Package Spire.PDF
如果你已經(jīng)安裝了該庫并希望升級到最新版本,可以使用以下命令:
PM> Update-Package Spire.PDF
C# 提取多個(gè)PDF表單域的數(shù)據(jù)
PDF 表單可能包含多種類型的域,例如文本框、列表框、下拉框、單選按鈕和復(fù)選框。每種域類型需要采用不同的方法來提取其數(shù)據(jù)。以下是提取這些類型的域的數(shù)據(jù)時(shí)所使用的關(guān)鍵屬性:
- 文本框(Text Boxes)
通過 PdfTextBoxFieldWidget 對象的 Name 和 Text 屬性,獲取文本框的名稱及其對應(yīng)的值。 - 列表框(List Boxes)
通過 PdfListBoxFieldWidget 對象的 Name、Values 和 SelectedValue 屬性,提取列表框的名稱、所有選項(xiàng)及選定的選項(xiàng)。 - 下拉框(Combo Boxes)
通過 PdfComboBoxFieldWidget 對象的 Name、Values 和 SelectedValue 屬性,獲取下拉框的名稱、所有選項(xiàng)及選定的選項(xiàng)。 - 單選按鈕(Radio Buttons)
通過 PdfRadioButtonListFieldWidget 對象的 Name 和 SelectedValue 屬性,獲取單選按鈕的名稱和選定的值。 - 復(fù)選框(Checkboxes)
通過 PdfCheckBoxFieldWidget 對象的 Name 和 Checked 屬性,提取復(fù)選框的名稱及其狀態(tài)(是否被選中)。
以下代碼展示了如何使用 C# 從多個(gè) PDF 表單域中提取數(shù)據(jù):
using Spire.Pdf;
using Spire.Pdf.Fields;
using Spire.Pdf.Widget;
using System.Collections.Generic;
using System.IO;
namespace ExtractPdfFormData
{
internal class Program
{
static void Main(string[] args)
{
// 初始化 PdfDocument 類實(shí)例,用于加載和處理 PDF 文件
using (PdfDocument doc = new PdfDocument())
{
// 加載包含表單域的 PDF 文件
doc.LoadFromFile("表單.pdf");
// 創(chuàng)建列表存儲(chǔ)提取的域名稱及其值
List<string> content = new List<string>();
// 獲取 PDF 文檔的表單對象
PdfFormWidget formWidget = doc.Form as PdfFormWidget;
// 檢查表單對象中是否包含表單域
if (formWidget?.FieldsWidget.Count > 0)
{
// 遍歷文檔中的所有表單域
for (int i = 0; i < formWidget.FieldsWidget.List.Count; i++)
{
// 獲取當(dāng)前表單域
PdfField field = formWidget.FieldsWidget[i];
// 如果當(dāng)前表單域?yàn)榭?,跳過該域
if (field == null) continue;
// 提取當(dāng)前表單域的內(nèi)容(名稱和值)
List<string> currentFieldContent = ExtractFieldContent(field);
// 如果提取到域內(nèi)容,則將其添加到 content 列表中
if (currentFieldContent.Count > 0)
{
content.AddRange(currentFieldContent);
// 如果不是最后一個(gè)表單域,添加一個(gè)空行用于分隔不同域的內(nèi)容
if (i < formWidget.FieldsWidget.List.Count - 1)
{
content.Add(""); // 添加空行分隔不同域的內(nèi)容
}
}
}
}
// 將提取的內(nèi)容逐行寫入文本文件
File.WriteAllLines("提取域數(shù)據(jù).txt", content);
}
}
/// <summary>
/// 提取單個(gè) PDF 表單域的內(nèi)容(域名和域值)
/// 根據(jù)不同的表單域類型(文本框、列表框、下拉框、單選按鈕、復(fù)選框)提取相應(yīng)的值
/// </summary>
/// <param name="field">當(dāng)前的 PDF 表單域?qū)ο?lt;/param>
/// <returns>包含域內(nèi)容的字符串列表</returns>
private static List<string> ExtractFieldContent(PdfField field)
{
// 初始化列表來存儲(chǔ)當(dāng)前表單域的內(nèi)容
List<string> fieldContent = new List<string>();
// 檢查該域是否為文本框
if (field is PdfTextBoxFieldWidget textBoxField)
{
fieldContent.Add($"文本框名稱:{textBoxField.Name}");
fieldContent.Add($"文本框值:{textBoxField.Text}");
}
// 檢查該域是否為列表框
else if (field is PdfListBoxWidgetFieldWidget listBoxField)
{
fieldContent.Add($"列表框名稱:{listBoxField.Name}");
fieldContent.Add("列表框選項(xiàng):");
// 遍歷并提取列表框中的所有選項(xiàng)
foreach (PdfListWidgetItem item in listBoxField.Values)
{
fieldContent.Add($"{item.Value}");
}
fieldContent.Add($"列表框選中項(xiàng):{listBoxField.SelectedValue}");
}
// 檢查該域是否為下拉框
else if (field is PdfComboBoxWidgetFieldWidget comboBoxField)
{
fieldContent.Add($"下拉框名稱:{comboBoxField.Name}");
fieldContent.Add("下拉框選項(xiàng):");
// 遍歷并提取下拉框中的所有選項(xiàng)
foreach (PdfListWidgetItem item in comboBoxField.Values)
{
fieldContent.Add($"{item.Value}");
}
fieldContent.Add($"下拉框選中項(xiàng):{comboBoxField.SelectedValue}");
}
// 檢查該域是否為單選按鈕
else if (field is PdfRadioButtonListFieldWidget radioBtnField)
{
fieldContent.Add($"單選按鈕名稱:{radioBtnField.Name}");
fieldContent.Add($"單選按鈕選中項(xiàng):{radioBtnField.SelectedValue}");
}
// 檢查該域是否為復(fù)選框
else if (field is PdfCheckBoxWidgetFieldWidget checkBoxField)
{
fieldContent.Add($"復(fù)選框名稱:{checkBoxField.Name}");
fieldContent.Add($"復(fù)選框狀態(tài):{(checkBoxField.Checked ? "選中" : "未選中")}");
}
// 返回當(dāng)前表單域的內(nèi)容
return fieldContent;
}
}
}C# 提取特定PDF表單域的數(shù)據(jù)
如果你需要從特定的表單域中提取數(shù)據(jù),可以通過該表單域的名稱直接訪問它,然后通過判斷其類型對應(yīng)地獲取其內(nèi)容。
以下代碼展示了如何使用C# 從名為 “國家” 的PDF表單域中提取數(shù)據(jù):
using Spire.Pdf;
using Spire.Pdf.Fields;
using Spire.Pdf.Widget;
using System.Collections.Generic;
using System.IO;
namespace ExtractSpecificFormData
{
internal class Program
{
static void Main(string[] args)
{
// 初始化 PdfDocument 類實(shí)例,用于加載和處理 PDF 文件
using (PdfDocument doc = new PdfDocument())
{
// 加載包含表單域的 PDF 文件
doc.LoadFromFile("表單.pdf");
// 創(chuàng)建列表來存儲(chǔ)提取的表單域名稱及其值
List<string> content = new List<string>();
// 獲取 PDF 文檔的表單對象
PdfFormWidget formWidget = doc.Form as PdfFormWidget;
// 指定域名稱
string fieldName = "國家";
// 檢查表單對象中是否包含表單域
if (formWidget?.FieldsWidget.Count > 0)
{
// 通過名稱訪問特定表單域
PdfField specificField = formWidget.FieldsWidget[fieldName];
// 確保域存在再進(jìn)行處理
if (specificField != null)
{
// 提取特定表單域的內(nèi)容(名稱和值)
List<string> specificFieldContent = ExtractFieldContent(specificField);
// 如果提取到內(nèi)容,則將其添加到 content 列表中
if (specificFieldContent.Count > 0)
{
content.AddRange(specificFieldContent);
}
}
else
{
content.Add($"未找到域 '{fieldName}'");
}
}
else
{
content.Add("PDF 表單中未找到任何域");
}
// 將提取的內(nèi)容逐行寫入文本文件
File.WriteAllLines("提取特定域數(shù)據(jù).txt", content);
}
}
/// <summary>
/// 提取單個(gè) PDF 表單域的內(nèi)容(名稱和值)
/// 處理不同類型的表單域,如文本框、列表框、下拉框、單選按鈕和復(fù)選框
/// </summary>
/// <param name="field">當(dāng)前 PDF 表單域?qū)ο?lt;/param>
/// <returns>包含表單域內(nèi)容的字符串列表</returns>
private static List<string> ExtractFieldContent(PdfField field)
{
// 初始化列表來存儲(chǔ)當(dāng)前表單域的內(nèi)容
List<string> fieldContent = new List<string>();
// 檢查該域是否為文本框
if (field is PdfTextBoxFieldWidget textBoxField)
{
// 將文本框的名稱和值添加到列表中
fieldContent.Add($"文本框名稱:{textBoxField.Name}");
fieldContent.Add($"文本框值:{textBoxField.Text}");
}
// 檢查該域是否為列表框
else if (field is PdfListBoxWidgetFieldWidget listBoxField)
{
fieldContent.Add($"列表框名稱:{listBoxField.Name}");
fieldContent.Add("列表框選項(xiàng):");
foreach (PdfListWidgetItem item in listBoxField.Values)
{
fieldContent.Add($"{item.Value}");
}
fieldContent.Add($"列表框選中項(xiàng):{listBoxField.SelectedValue}");
}
// 檢查該域是否為下拉框
else if (field is PdfComboBoxWidgetFieldWidget comboBoxField)
{
fieldContent.Add($"下拉框名稱:{comboBoxField.Name}");
fieldContent.Add("下拉框選項(xiàng):");
foreach (PdfListWidgetItem item in comboBoxField.Values)
{
fieldContent.Add($"{item.Value}");
}
fieldContent.Add($"下拉框選中項(xiàng):{comboBoxField.SelectedValue}");
}
// 檢查該域是否為單選按鈕
else if (field is PdfRadioButtonListFieldWidget radioBtnField)
{
fieldContent.Add($"單選按鈕名稱:{radioBtnField.Name}");
fieldContent.Add($"單選按鈕選中項(xiàng):{radioBtnField.SelectedValue}");
}
// 檢查該域是否為復(fù)選框
else if (field is PdfCheckBoxWidgetFieldWidget checkBoxField)
{
fieldContent.Add($"復(fù)選框名稱:{checkBoxField.Name}");
fieldContent.Add($"復(fù)選框狀態(tài):{(checkBoxField.Checked ? "選中" : "未選中")}");
}
// 返回當(dāng)前表單域的內(nèi)容列表
return fieldContent;
}
}
}以上就是使用C# 讀取PDF表單域數(shù)據(jù)的全部內(nèi)容。
到此這篇關(guān)于C#提取PDF表單數(shù)據(jù)的實(shí)現(xiàn)流程的文章就介紹到這了,更多相關(guān)C#提取PDF表單數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#使用TcpListener及TcpClient開發(fā)一個(gè)簡單的Chat工具實(shí)例
下面小編就為大家分享一篇C#使用TcpListener及TcpClient開發(fā)一個(gè)簡單的Chat工具實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12
c#使用linq把多列的List轉(zhuǎn)化為只有指定列的List
這篇文章主要介紹了c#使用linq把多列的List轉(zhuǎn)化為只有指定列的List,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
深入理解C#實(shí)現(xiàn)快捷鍵(系統(tǒng)熱鍵)響應(yīng)的方法
本篇文章是對使用C#實(shí)現(xiàn)快捷鍵(系統(tǒng)熱鍵)響應(yīng)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
visio二次開發(fā)--判斷文檔是否已發(fā)生變化(變化就加星號*)
最近做一個(gè)故障樹診斷的項(xiàng)目,用visio二次開發(fā),可以同時(shí)打開多個(gè)繪制的故障樹圖形文檔。項(xiàng)目中需要實(shí)現(xiàn)判斷文檔是否發(fā)生變化,這是很多編輯軟件的基本功能,變化了就加個(gè)星號*2013-04-04

