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

本文关键字:数据 空值 表单 pdf iTextSharp 读取 使用 | 更新日期: 2023-09-27 18:18:02

我正在使用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: 


安德里亚

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

这段代码将为您提供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();
            }

这样的工作将改变以适应您的代码这将为您创建的pdf创建更多的侦听器。

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