【问题标题】:find page number of a string in pdf file in c#在c#中查找pdf文件中字符串的页码
【发布时间】:2012-04-22 07:04:03
【问题描述】:

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

【问题讨论】:

标签: c# .net itextsharp


【解决方案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...
  }
}

【讨论】:

    【解决方案2】:

    要使用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) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-18
      • 1970-01-01
      • 1970-01-01
      • 2020-08-12
      • 2015-07-02
      • 1970-01-01
      • 2013-06-01
      相关资源
      最近更新 更多