使用iTextSharp读取pdf表单数据:空值

时间:2011-12-15 15:55:38

标签: c# .net itextsharp

我正在使用iTextSharp阅读PDF模块,它的工作非常好。我不知道为什么但是,当我尝试使用GetField()方法读取PDF字段时,正确填写PDF文件的某些字段将返回为空(value =“”)。

你知道为什么吗?还有其他方法可以从PDF字段中提取值吗?

PdfReader pdfReader = new PdfReader(new RandomAccessFileOrArray(fileName), null);
pdfFormFields = pdfReader.AcroFields;

foreach (var field in pdfFormFields.Fields) {
     _logger.Error("Field: " + field.Key + " - Value: " + pdfFormFields.GetField(field.Key.ToString()));
}

这是输出:

PDFReader.PDFRead - Field: modulo1[0].BASALE_PAG1[0].SITO_TUMORE_PRIMITIVO[0] - Value: Stomaco
PDFReader.PDFRead - Field: modulo1[0].BASALE_PAG1[0].ALTRO_SITO_TUMORE_PRIMITIVO[0] - Value: cervice
PDFReader.PDFRead - Field: modulo1[0].BASALE_PAG1[0].IST_PRIM_SECOND[0] - Value: su secondario
PDFReader.PDFRead - Field: modulo1[0].BASALE_PAG1[0].STADIAZIONE[0] - Value: IIa
PDFReader.PDFRead - Field: modulo1[0].BASALE_PAG2[0].OCTREOSCAN_Ga-PET[0] - Value: Negativo
PDFReader.PDFRead - Field: modulo1[0].BASALE_PAG2[0].FUNZIONANTE[0] - Value: No
PDFReader.PDFRead - Field: modulo1[0].BASALE_PAG2[0].CgA_INIZIO_TERAPIA[0] - Value: 1.00000000
PDFReader.PDFRead - Field: modulo1[0].BASALE_PAG2[0].CgA_VAL_RIFERIMENTO[0] - Value: 
PDFReader.PDFRead - Field: modulo1[0].BASALE_PAG2[0].ACIDO5_VAL_RIFERIMENTO[0] - Value: 

tnx,
安德烈

2 个答案:

答案 0 :(得分:1)

此代码应该为您提供C#中所有字段的内容:

   public string ReadPdfFile(string fileName)
            {
                StringBuilder text = new StringBuilder();

                if (File.Exists(fileName))
                {
                    PdfReader pdfReader = new PdfReader(fileName);

                    for (int page = 1; page <= pdfReader.NumberOfPages; page++)
                    {
                        ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
                        string currentText = PdfTextExtractor.GetTextFromPage(pdfReader, page, strategy);

                        currentText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(currentText)));
                        text.Append(currentText);
                    }
                    pdfReader.Close();
                }
                return text.ToString();
            }
            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());
            }
            private void Button_Click_1(object sender, RoutedEventArgs e)
            {
               var reader = new PdfReader(@"Direction_to_your.pdf");
               AcroFields form = reader.AcroFields;
               txtBox1.Text = GetFormFieldNamesWithValues(reader); 
               reader.Close();
            }

答案 1 :(得分:0)

这样的工作会改变以适合您的代码   这会为你创建的pdf创建更多的Listner。

PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outputFileName, FileMode.Create));