1

我有一个 pdf 文档,我想从中提取内容。我遇到的问题是……我搜索 IMEI 关键字,它找到了它,但我需要实际的 IMEI 值,它是循环中的下一项。

在 PDF 中,值如下所示:IMEI 90289393092

通过以下脚本返回值:-0.1 -8.8 9.8 -0.1 446.7 403.9 Tm (IMEI:) Tj

我只想拥有价值:90289393092

我正在使用的脚本:

Add-Type -Path .\itextsharp.dll
$reader = New-Object iTextSharp.text.pdf.pdfreader -ArgumentList "$pwd\PDF\DOC001.pdf"

for ($page = 1; $page -le $reader.NumberOfPages; $page++) {
 $lines = [char[]]$reader.GetPageContent($page) -join "" -split "`n"
 foreach ($line in $lines) {
  if ($line -match "IMEI") { 
   $line = $line -replace "\\([\S])", $matches[1]
   $line -replace "^\[\(|\)\]TJ$", "" -split "\)\-?\d+\.?\d*\(" -join ""

  }
 }
}
4

1 回答 1

4

this is the way for using itextsharp.dll and read a pdf as plain text:

Add-Type -Path .\itextsharp.dll
$reader = New-Object iTextSharp.text.pdf.pdfreader -ArgumentList c:\ps\a.pdf        

for ($page = 1; $page -le $reader.NumberOfPages; $page++)
{
    $strategy = new-object  'iTextSharp.text.pdf.parser.SimpleTextExtractionStrategy'            
    $currentText = [iTextSharp.text.pdf.parser.PdfTextExtractor]::GetTextFromPage($reader, $page, $strategy);
    [string[]]$Text += [system.text.Encoding]::UTF8.GetString([System.Text.ASCIIEncoding]::Convert( [system.text.encoding]::default  , [system.text.encoding]::UTF8, [system.text.Encoding]::Default.GetBytes($currentText)));      
}
$Reader.Close();

And this can be the regex you need but I haven't tested it

[regex]::matches( $text, '(?<=IMEI\s+)(\d+)(?=\s+)' ) | select -expa value
于 2013-03-28T15:50:01.173 回答