【问题标题】:How to get the fields name from a pdf file?如何从pdf文件中获取字段名称?
【发布时间】:2017-03-21 21:56:50
【问题描述】:

我在 iframe 中有一个 pdf 文件,我想检查 pdf 文件以了解字段的名称,以允许用户从文本框中填写此字段。

这是我的 iframe 和文本框,例如:

 <iframe id="frmDoc" runat="server"  style="width:800px;height:1200px;"></iframe>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

这是在 iframe 中显示 pdf 的代码:

protected void Page_Load(object sender, EventArgs e)
        {
            frmDoc.Attributes["src"] = "~/admin/minufia/6   نموذج إحصائي رقم.pdf";
        }

请帮我解决这个问题。

【问题讨论】:

  • 在本例中,您的文本框不在 iframe 中。
  • 在对您澄清的答案的评论中:“我已经有 iTextSharp,但我不知道如何使用它来搜索我的 pdf 文件中的输入字段” .如果您使用 iTextSharp 5.x,How do I enumerate all the fields in a PDF file in ITextSharp 可能会对您有所帮助。

标签: c# html pdf iframe itext


【解决方案1】:

您想要做的并不是一项简单的编程任务。 PDF 文件格式非常复杂,因此您几乎可以肯定需要使用第三方代码库来读取/修改 PDF 的内容。我之前使用过一个名为 iTextSharp 的库,效果很好,但是要花钱。

一旦您找到了一个 PDF 操作库并阅读了如何使用它,您就可以使用它在 PDF 中搜索输入字段,并在文件中读取/写入这些字段的值。但是,您可能必须自己编写所有 Web 代码来获取用户输入并将它们传递到您的 PDF 代码库中。

【讨论】:

  • 我已经有 iTextSharp 但我不知道如何使用它来搜索我的 pdf 文件中的输入字段
  • @MahmoudAbdo 请将您选择的 PDF 库等重要信息添加到问题中,并相应地更新标签。请注明您使用的 iText 版本。
【解决方案2】:

您可以查看 Spire.PDF。它提供了丰富的功能来在 .NET 应用程序中操作 PDF 文件。它有商业和free versions。 检查下面的代码是否有帮助,我假设您提到的字段类型是文本框字段。

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

注意:我是 Spire 的员工。

【讨论】:

  • 我试过你的代码,但它给了我这个错误错误 1 ​​'Spire.Pdf.PdfDocument' 不包含带 1 个参数的构造函数
  • PdfDocument 文档 = new PdfDocument(); document.LoadFromFile("Input.pdf");
猜你喜欢
  • 1970-01-01
  • 2017-09-09
  • 2018-05-16
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-08
相关资源
最近更新 更多