使用 PowerShell、Regex、itextsharp.dll 在 PDF 中查找特定字段
Find specific fields in a PDF using PowerShell, Regex, itextsharp.dll
我是 RegEx 的新手,但在过去的几个小时里一直在尝试弄清楚如何使用 PowerShell 和 itextsharp.dll 解析 PDF 中的一些数据。我打算在 itextsharp 论坛中访问 post,但我实际上并没有在那里看到可以寻求帮助的地方。对于已经很好地理解 RegEx 的人来说,只是一堆操作方法。
PDF table 如下所示:
itextsharp.dll 输出如下所示:
Selection Criteria Report parameters
Select all Bottles where
Date Loaded - Date/Time (Bottle) is after or equal to '11/20/2015 15:50'
AND
Date Loaded - Date/Time (Bottle) is before or equal to '11/20/2015
16:10'
N/A
Unit # Status Determined Bottle ID Time to Find Cell
=W00000000000001 Negative 11/25/2015 16:08 AAAACNSJ 5 2D55
=W00000000000002 Negative 11/25/2015 16:08 AAAACNSA 5 2D56
1291231 Negative 11/25/2015 16:08 AAAACNB 5 2D57
=W00000000000003 Positive 11/25/2015 16:08 AAAACNS9 5 2D58
1981231 Negative 11/25/2015 16:09 AAAACNSG 5 2D59
=W00000000000004 Negative 11/25/2015 16:10 AAAACNS7 5 2D60
Report
Reviewed By: Printed for manual signature
Page 1 of 1 11/25/2015 16:15
我一直在使用以下代码和各种不同的 RegEx 表达式来尝试仅解析 table 数据并将每一列设置为一个变量。我已经省略了我尝试过的所有不同的东西,因为有太多的东西而且我真的不知道我在做什么,因为数据是这样的。
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)));
$Line = $text -Split "`n"
$i = 0
Do {
If ($Line[$i] -match '(?m)^(?<unit_id>=?\w+)\s+(?<status>\w+)\s+(?<determined>\d{2}\/\d{2}\/\d{4}\s+\d{2}:\d{2})\s+(?<bottle_id>\w+)\s+(?<time_to_find>\d)+\s+(?<cell>\w+)$') {
Write-Host $Line[$i]
}
$i = $i + 1
}
While ($Line[$i])
}
$Reader.Close();
有没有人可以帮助我将所有这些列正确设置为变量?任何帮助将不胜感激。谢谢!
这是一个示例正则表达式,应该可以很好地解析 1 行字符串:
$text = '=W03651532551000 Negative 11/25/2015 16:08 PAGYCNQ6 5 2D56'
$text -match '^(?<unit_id>=?\w+)\s+(?<status>\w+)\s+(?<determined>[\/\d\s:]+)\s+(?<bottle_id>\w+)\s+(?<time_to_find>\d+)\s+(?<cell>\w+)$'
$matches
输出:
Name Value
---- -----
determined 11/25/2015 16:08
cell 2D56
status Negative
bottle_id PAGYCNQ6
time_to_find 5
unit_id =W03651532551000
0 =W03651532551000 Negative 11/25/2015 16:08 PAGYCNQ6 5 2D56
这里是更复杂的一个:
$objcol = @()
$text = "=W03651532551000 Negative 11/25/2015 16:08 PAGYCNQ6 5 2D56`nLW03651532551000 Positive 11/25/2015 16:08 PAGYCNQ6 5 2D56"
$res = $text.Split("`n") | where {
$_ -match '(?<unit_id>=?\w+)\s+(?<status>\w+)\s+(?<determined>\d{2}\/\d{2}\/\d{4}\s+\d{2}:\d{2})\s+(?<bottle_id>\w+)\s+(?<time_to_find>\d+)\s+(?<cell>\w+)'
} | foreach {
$obj = new-object PSObject –prop @{
unitId=$matches['unit_id']; status=$matches['status'];
Determined=$matches['determined']; bottleId=$matches['bottle_id'];
timeToFind=$matches['time_to_find']
}
$objcol += $obj
}
Write-Output $objcol
结果:
bottleId : PAGYCNQ6
timeToFind : 5
Determined : 11/25/2015 16:08
unitId : =W03651532551000
status : Negative
bottleId : PAGYCNQ6
timeToFind : 5
Determined : 11/25/2015 16:08
unitId : LW03651532551000
status : Positive
非常感谢。您的问题对我自己的代码有所贡献。
这是我向前支付的(我的代码将 $results 导出到 Excel 电子表格以进行进一步的数据分析):
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);
$Line = $currentText -Split "`n"
$i = 0
Do {
If ($Line[$i] -match '[0-9]{2}[A-Z]{2}[0-9]{4}') {
$matched1 = [regex]::Match($Line[$i],'[0-9]{2}[A-Z]{2}[0-9]{4}').Value
Write-Host $Line[$i]
$response = @{
File = $pdf.FullName
Keyword1 = $matched1
Line = $Line[$i]
Page = $page
BaseName = $pdf.Name
Folder = $pdf.Directory
}
$results += New-Object PSObject -Property $response
}
$i = $i + 1
}
While ($Line[$i])
}
$Reader.Close();
$matched1 变量确保我有在每个文档中找到哪个关键字的记录。在我的例子中,任何关键字两位数+两位字母+四位数字!例如:11XX1111
我希望这可以帮助那里的人。
这绝对不是一个精炼的代码,但它做了我想要的。
此致,
我是 RegEx 的新手,但在过去的几个小时里一直在尝试弄清楚如何使用 PowerShell 和 itextsharp.dll 解析 PDF 中的一些数据。我打算在 itextsharp 论坛中访问 post,但我实际上并没有在那里看到可以寻求帮助的地方。对于已经很好地理解 RegEx 的人来说,只是一堆操作方法。
PDF table 如下所示:
itextsharp.dll 输出如下所示:
Selection Criteria Report parameters
Select all Bottles where
Date Loaded - Date/Time (Bottle) is after or equal to '11/20/2015 15:50'
AND
Date Loaded - Date/Time (Bottle) is before or equal to '11/20/2015
16:10'
N/A
Unit # Status Determined Bottle ID Time to Find Cell
=W00000000000001 Negative 11/25/2015 16:08 AAAACNSJ 5 2D55
=W00000000000002 Negative 11/25/2015 16:08 AAAACNSA 5 2D56
1291231 Negative 11/25/2015 16:08 AAAACNB 5 2D57
=W00000000000003 Positive 11/25/2015 16:08 AAAACNS9 5 2D58
1981231 Negative 11/25/2015 16:09 AAAACNSG 5 2D59
=W00000000000004 Negative 11/25/2015 16:10 AAAACNS7 5 2D60
Report
Reviewed By: Printed for manual signature
Page 1 of 1 11/25/2015 16:15
我一直在使用以下代码和各种不同的 RegEx 表达式来尝试仅解析 table 数据并将每一列设置为一个变量。我已经省略了我尝试过的所有不同的东西,因为有太多的东西而且我真的不知道我在做什么,因为数据是这样的。
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)));
$Line = $text -Split "`n"
$i = 0
Do {
If ($Line[$i] -match '(?m)^(?<unit_id>=?\w+)\s+(?<status>\w+)\s+(?<determined>\d{2}\/\d{2}\/\d{4}\s+\d{2}:\d{2})\s+(?<bottle_id>\w+)\s+(?<time_to_find>\d)+\s+(?<cell>\w+)$') {
Write-Host $Line[$i]
}
$i = $i + 1
}
While ($Line[$i])
}
$Reader.Close();
有没有人可以帮助我将所有这些列正确设置为变量?任何帮助将不胜感激。谢谢!
这是一个示例正则表达式,应该可以很好地解析 1 行字符串:
$text = '=W03651532551000 Negative 11/25/2015 16:08 PAGYCNQ6 5 2D56'
$text -match '^(?<unit_id>=?\w+)\s+(?<status>\w+)\s+(?<determined>[\/\d\s:]+)\s+(?<bottle_id>\w+)\s+(?<time_to_find>\d+)\s+(?<cell>\w+)$'
$matches
输出:
Name Value
---- -----
determined 11/25/2015 16:08
cell 2D56
status Negative
bottle_id PAGYCNQ6
time_to_find 5
unit_id =W03651532551000
0 =W03651532551000 Negative 11/25/2015 16:08 PAGYCNQ6 5 2D56
这里是更复杂的一个:
$objcol = @()
$text = "=W03651532551000 Negative 11/25/2015 16:08 PAGYCNQ6 5 2D56`nLW03651532551000 Positive 11/25/2015 16:08 PAGYCNQ6 5 2D56"
$res = $text.Split("`n") | where {
$_ -match '(?<unit_id>=?\w+)\s+(?<status>\w+)\s+(?<determined>\d{2}\/\d{2}\/\d{4}\s+\d{2}:\d{2})\s+(?<bottle_id>\w+)\s+(?<time_to_find>\d+)\s+(?<cell>\w+)'
} | foreach {
$obj = new-object PSObject –prop @{
unitId=$matches['unit_id']; status=$matches['status'];
Determined=$matches['determined']; bottleId=$matches['bottle_id'];
timeToFind=$matches['time_to_find']
}
$objcol += $obj
}
Write-Output $objcol
结果:
bottleId : PAGYCNQ6
timeToFind : 5
Determined : 11/25/2015 16:08
unitId : =W03651532551000
status : Negative
bottleId : PAGYCNQ6
timeToFind : 5
Determined : 11/25/2015 16:08
unitId : LW03651532551000
status : Positive
非常感谢。您的问题对我自己的代码有所贡献。 这是我向前支付的(我的代码将 $results 导出到 Excel 电子表格以进行进一步的数据分析):
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);
$Line = $currentText -Split "`n"
$i = 0
Do {
If ($Line[$i] -match '[0-9]{2}[A-Z]{2}[0-9]{4}') {
$matched1 = [regex]::Match($Line[$i],'[0-9]{2}[A-Z]{2}[0-9]{4}').Value
Write-Host $Line[$i]
$response = @{
File = $pdf.FullName
Keyword1 = $matched1
Line = $Line[$i]
Page = $page
BaseName = $pdf.Name
Folder = $pdf.Directory
}
$results += New-Object PSObject -Property $response
}
$i = $i + 1
}
While ($Line[$i])
}
$Reader.Close();
$matched1 变量确保我有在每个文档中找到哪个关键字的记录。在我的例子中,任何关键字两位数+两位字母+四位数字!例如:11XX1111 我希望这可以帮助那里的人。 这绝对不是一个精炼的代码,但它做了我想要的。
此致,