如何使用c#从pdf中提取特定文本的控件属性

问题描述:

我有pdf文件,我需要提取特定文本(例如:发票号)控制属性,就像其他RPA工具一样

I have pdf files from which I need to extract specific text(ex: invoice no) control attributes like other RPA tools do 

EX:  < wnd app ='acrord32.exe'
cls ='AcrobatSDIWindow'
title ='invoice3.pdf - Adob​​e Acrobat Reader DC' />


< wnd cls ='AVL_AVView'
title ='AVPageView' / >


< ctrl idx ='1'
角色 ='row'小号pan> />

< ctrl name ='发票编号:AT-30411567   '
角色 ='文字' />

EX: <wnd app='acrord32.exe' cls='AcrobatSDIWindow' title='invoice3.pdf - Adobe Acrobat Reader DC' />
<wnd cls='AVL_AVView' title='AVPageView' />
<ctrl idx='1' role='row' />
<ctrl name='Invoice Number: AT-30411567   ' role='text' />

有谁知道请帮我解决这个问题

Is anyone knows please help me to solve this



您好Sunitha_Bist,


Hi Sunitha_Bist,

>>如何提取控件来自pdf的特定文本的属性使用c#

您可以使用 ITextSharp
从PDF文档中提取纯文本。

You can use ITextSharp to extract plain text from PDF documents.

以下代码供您参考。

            string TempsaveFilename = @"D:\hello2.pdf";
            PdfReader pdfReader = new PdfReader(@"D:\hello.pdf");
            PdfStamper stamper = new PdfStamper(pdfReader, new FileStream(TempsaveFilename, FileMode.Create), '\0', true);
            AcroFields fields = stamper.AcroFields;
            AcroFields pdfFormFields = pdfReader.AcroFields;
            foreach (KeyValuePair<string, AcroFields.Item> kvp in fields.Fields)
            {
                string FieldValue = "a";
                if (FieldValue != "")
                {
                    //var oldvalue = pdfReader.AcroFields.GetField(kvp.Key);
                    fields.SetField(kvp.Key, FieldValue);
                }
            }

            stamper.FormFlattening = false;
            stamper.Close();
            pdfReader.Close();



以下文章供您参考。


The following articles for your reference.

使用iTextSharp阅读PDF表单字段:

https://community.dynamics.com/crm/b/crmmitchmilam/archive/2012/11/14/reading-pdf-form-fields-using-itextsharp

Reading PDF form fields using iTextSharp:
https://community.dynamics.com/crm/b/crmmitchmilam/archive/2012/11/14/reading-pdf-form-fields-using-itextsharp

用C#(100%.NET)从PDF中提取文本:

https://www.codeproject.com/Articles/14170/Extract-Text-from-PDF-in-C-NET

Extract Text from PDF in C# (100% .NET):
https://www.codeproject.com/Articles/14170/Extract-Text-from-PDF-in-C-NET



最好的问候,


Best Regards,

Yohann Lu

Yohann Lu