C# 在c中查找pdf文件中字符串的页码#
我正在开发一个pdf阅读器。我想在pdf中找到任何字符串,并知道相应的页码。我正在使用iTextSharp 像这样的方法应该会奏效: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
// 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)