4

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

4

2 回答 2

1

像这样的东西应该工作:

// 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...
  }
}
于 2012-04-22T15:24:59.460 回答
1

为了使用Itextsharp你可以使用Acrobat.dll来查找当前页码。首先打开pdf文件并使用L搜索字符串

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

Acroavdoc.FindText("String").

如果在此 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) 
于 2013-04-23T04:20:20.597 回答