C# 如何从pdf文件中获取字段名称?
我在iframe中有一个pdf文件,我想检查pdf文件以了解字段的名称,从而允许用户从文本框中填充此字段 这是我的iframe和文本框,例如:C# 如何从pdf文件中获取字段名称?,c#,html,pdf,iframe,itext,C#,Html,Pdf,Iframe,Itext,我在iframe中有一个pdf文件,我想检查pdf文件以了解字段的名称,从而允许用户从文本框中填充此字段 这是我的iframe和文本框,例如: <iframe id="frmDoc" runat="server" style="width:800px;height:1200px;"></iframe> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 请帮我解决这
<iframe id="frmDoc" runat="server" style="width:800px;height:1200px;"></iframe>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
请帮我解决这个问题。您想做的不是简单的编程任务。PDF文件格式非常复杂,因此您几乎肯定需要使用第三方代码库来读取/修改PDF的内容。我以前使用过一个名为iTextSharp的库,效果很好,但它需要花钱
找到PDF操纵库并阅读如何使用它后,可以使用它在PDF中搜索输入字段,并在文件中读取/写入这些字段的值。但是,您可能需要自己编写所有web代码来获取用户输入并将其传递到PDF代码库。您可以查看Spire.PDF。它提供了丰富的功能来操作.NET应用程序中的PDF文件。它既有商业上的,也有商业上的。 检查下面的代码,看看它是否有帮助,我假设您提到的字段类型是文本框字段
//Load the PDF document
PdfDocument document = new PdfDocument("Input.pdf");
//Load the existing forms
PdfFormWidget loadedForm = document.Form as PdfFormWidget;
//Go through the forms
for (int i = 0; i < loadedForm.FieldsWidget.List.Count; i++)
{
PdfField field = loadedForm.FieldsWidget.List[i] as PdfField;
//Fill textbox form field
if (field is PdfTextBoxFieldWidget)
{
PdfTextBoxFieldWidget textField = field as PdfTextBoxFieldWidget;
//Get the field name and fill with content
switch (textField.Name)
{
case "fieldName":
textField.Text = "text";
break;
//```
}
}
}
//Save and close
document.SaveToFile("Output.pdf");
document.Close();
//加载PDF文档
pdf文档=新的pdf文档(“Input.pdf”);
//加载现有表单
PdfFormWidget loadedForm=document.Form作为PdfFormWidget;
//检查表格
对于(int i=0;i
注意:我是Spire的员工。在本例中,您的文本框不在iframe中。在对回答的评论中,您澄清了:“我已经有了iTextSharp,但我不知道如何使用它来搜索我的pdf文件中的输入字段。”。如果您使用iTextSharp 5.x,可能会对您有所帮助。我尝试了您的代码,但它给出了以下错误:1'Spire.Pdf.PdfDocument'不包含采用1 argumentsPdfDocument document=new PdfDocument()的构造函数;document.LoadFromFile(“Input.pdf”);我已经有了iTextSharp,但我不知道如何使用它来搜索pdf中的输入字段file@MahmoudAbdo请在问题中添加重要信息,如您选择的PDF库,并相应地更新标签。请指明您使用的iText版本。
//Load the PDF document
PdfDocument document = new PdfDocument("Input.pdf");
//Load the existing forms
PdfFormWidget loadedForm = document.Form as PdfFormWidget;
//Go through the forms
for (int i = 0; i < loadedForm.FieldsWidget.List.Count; i++)
{
PdfField field = loadedForm.FieldsWidget.List[i] as PdfField;
//Fill textbox form field
if (field is PdfTextBoxFieldWidget)
{
PdfTextBoxFieldWidget textField = field as PdfTextBoxFieldWidget;
//Get the field name and fill with content
switch (textField.Name)
{
case "fieldName":
textField.Text = "text";
break;
//```
}
}
}
//Save and close
document.SaveToFile("Output.pdf");
document.Close();