iTextSharp无法读取使用C#.Net进行数字签名的预填充PDF表单文件

时间:2018-05-03 03:45:50

标签: c# itext pdfbox itext7

我一直在尝试阅读使用iTextSharp 5.5.13.0在C#.Net上进行数字签名的预填充PDF表单文件

我尝试了很多方法,但能够获取字段名称,但值是空白的。

我已经使用了所有可能的方法,如ParsePdf,GetFormFieldNames& GetFormFieldNamesWithValues,ReadPDFformDataPageWise,ExtractXML但无法读取值。

我想读的文件:Pre-Filled PDF Form File

       using iTextSharp.text;
       using iTextSharp.text.pdf;
       using iTextSharp.text.pdf.parser;
       using org.apache.pdfbox.pdmodel;
       using org.apache.pdfbox.util;
       using System;
       using System.Collections;
       using System.Collections.Generic;
       using System.ComponentModel;
       using System.Data;
       using System.Drawing;
       using System.IO;
       using System.Linq;
       using System.Text;
       using System.Threading.Tasks;
       using System.Windows.Forms;
       using System.Xml;

       private static string GetFormFieldNames(PdfReader pdfReader)
    {
        return string.Join("\r\n", pdfReader.AcroFields.Fields
                                       .Select(x => x.Key).ToArray());
    }

    private static string GetFormFieldNamesWithValues(PdfReader pdfReader)
    {
        return string.Join("\r\n", pdfReader.AcroFields.Fields
                                       .Select(x => x.Key + "=" +
                                        pdfReader.AcroFields.GetField(x.Key))
                                       .ToArray());
    }

    public string ParsePdf(string fileName)
    {
        if (!File.Exists(fileName))
            throw new FileNotFoundException("fileName");
        using (PdfReader reader = new PdfReader(fileName))
        {
            StringBuilder sb = new StringBuilder();

            ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
            for (int page = 0; page < reader.NumberOfPages; page++)
            {
                string text = PdfTextExtractor.GetTextFromPage(reader, page + 1, strategy);
                if (!string.IsNullOrEmpty(text))
                {
                    sb.Append(Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(text))));
                }
            }

            return sb.ToString();
        }
    }
       public bool ExtractXML(string filename)
    {
        try
        {
            PdfReader reader = new PdfReader(filename);
            AcroFields afields = reader.AcroFields;
            var result = new XmlDocument();
            result.LoadXml(string.Format("<{0}/>", "xml"));
            foreach (string keyname in afields.Fields.Keys)
            {
                AcroFields.Item item = afields.GetFieldItem(keyname);
                XmlElement elt = result.CreateElement(keyname);
                elt.InnerXml = "<![CDATA[" + afields.GetField(keyname) + "]]>\";";
                result.DocumentElement.AppendChild(elt);
            }

            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
    //Read all 'Form values/keys' from an existing multi-page PDF document
    public void ReadPDFformDataPageWise(string PDFFileName)
    {
        PdfReader reader = new PdfReader(PDFFileName);
        AcroFields form = reader.AcroFields;
        try
        {
            for (int page = 1; page <= reader.NumberOfPages; page++)
            {
                foreach (KeyValuePair<string, AcroFields.Item> kvp in form.Fields)
                {
                    switch (form.GetFieldType(kvp.Key))
                    {
                        case AcroFields.FIELD_TYPE_CHECKBOX:
                        case AcroFields.FIELD_TYPE_COMBO:
                        case AcroFields.FIELD_TYPE_LIST:
                        case AcroFields.FIELD_TYPE_RADIOBUTTON:
                        case AcroFields.FIELD_TYPE_NONE:
                        case AcroFields.FIELD_TYPE_PUSHBUTTON:
                        case AcroFields.FIELD_TYPE_SIGNATURE:
                        case AcroFields.FIELD_TYPE_TEXT:
                            int fileType = form.GetFieldType(kvp.Key);
                            string fieldValue = form.GetField(kvp.Key);
                            string translatedFileName = form.GetTranslatedFieldName(kvp.Key);

                            textBox1.Text = textBox1.Text + fileType + " " + fieldValue + " " + translatedFileName + ".....";
                            break;
                    }
                }
            }
        }
        catch
        {
        }
        finally
        {
            reader.Close();
        }
    }

0 个答案:

没有答案