Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在c中查找pdf文件中字符串的页码#_C#_.net_Itextsharp - Fatal编程技术网

C# 在c中查找pdf文件中字符串的页码#

C# 在c中查找pdf文件中字符串的页码#,c#,.net,itextsharp,C#,.net,Itextsharp,我正在开发一个pdf阅读器。我想在pdf中找到任何字符串,并知道相应的页码。我正在使用iTextSharp 像这样的方法应该会奏效: // add any string you want to match on Regex regex = new Regex("the", RegexOptions.IgnoreCase | RegexOptions.Compiled ); PdfReader reader = new PdfReader(pdfPath); PdfReaderConten

我正在开发一个pdf阅读器。我想在pdf中找到任何字符串,并知道相应的页码。我正在使用iTextSharp

像这样的方法应该会奏效:

// add any string you want to match on
Regex regex = new Regex("the", 
  RegexOptions.IgnoreCase | RegexOptions.Compiled 
);
PdfReader reader = new PdfReader(pdfPath);
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
for (int i = 1; i <= reader.NumberOfPages; i++) {
  ITextExtractionStrategy strategy = parser.ProcessContent(
    i, new SimpleTextExtractionStrategy()
  );
  if ( regex.IsMatch(strategy.GetResultantText()) ) {
    // do whatever with corresponding page number i...
  }
}
//添加任何要匹配的字符串
Regex Regex=新的Regex(“the”,
RegexOptions.IgnoreCase | RegexOptions.Compiled
);
PdfReader reader=新的PdfReader(pdfPath);
PdfReaderContentParser=新的PdfReaderContentParser(读取器);

对于(int i=1;i要使用
Itextsharp
您可以使用
Acrobat.dll
查找当前页码。首先打开pdf文件并使用

Acroavdoc.open("Filepath","Temperory title") 

如果在此pdf文件中找到字符串,则光标将移动到特定页面,搜索的字符串将突出显示。现在,我们使用
Acroavpageview.GetPageNum()
获取当前页码

Dim AcroXAVDoc As CAcroAVDoc
Dim Acroavpage As AcroAVPageView
Dim AcroXApp As CAcroApp

AcroXAVDoc = CType(CreateObject("AcroExch.AVDoc"), Acrobat.CAcroAVDoc)
AcroXApp = CType(CreateObject("AcroExch.App"), Acrobat.CAcroApp)
AcroXAVDoc.Open(TextBox1.Text, "Original document")
AcroXAVDoc.FindText("String is to searched", True, True, False)
Acroavpage = AcroXAVDoc.GetAVPageView()

Dim x As Integer = Acroavpage.GetPageNum
MsgBox("the string found in page number" & x) 

您需要从每个页面提取文本,请查看PDFTextractor,
Dim AcroXAVDoc As CAcroAVDoc
Dim Acroavpage As AcroAVPageView
Dim AcroXApp As CAcroApp

AcroXAVDoc = CType(CreateObject("AcroExch.AVDoc"), Acrobat.CAcroAVDoc)
AcroXApp = CType(CreateObject("AcroExch.App"), Acrobat.CAcroApp)
AcroXAVDoc.Open(TextBox1.Text, "Original document")
AcroXAVDoc.FindText("String is to searched", True, True, False)
Acroavpage = AcroXAVDoc.GetAVPageView()

Dim x As Integer = Acroavpage.GetPageNum
MsgBox("the string found in page number" & x)