Reputation: 2412
Using iText 5.5, I am trying to assign value from list to variable. Any professional eager to help amateur?
File I am parsing: https://slicedinvoices.com/pdf/wordpress-pdf-invoice-plugin-sample.pdf
Here is my code:
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PDF_file_reader
{
class Program
{
static void Main(string[] args)
{
List<string> InvoiceNumbers = new List<string>();
string filePath = @"C:\temp\parser\Invoice_Template.pdf";
int pagesToScan = 2;
string strText = string.Empty;
try
{
PdfReader reader = new PdfReader(filePath);
for (int page = 1; page <= pagesToScan; page++) //(int page = 1; page <= reader.NumberOfPages; page++) <- for scanning all the pages in A PDF
{
ITextExtractionStrategy its = new LocationTextExtractionStrategy();
strText = PdfTextExtractor.GetTextFromPage(reader, page, its);
strText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(strText)));
//creating the string array and storing the PDF line by line
string[] lines = strText.Split('\n');
foreach (string line in lines)
{
if (line.Contains("Invoice Number"))
{
InvoiceNumbers.Add(line.Trim());
break;
}
}
var match = InvoiceNumbers.FirstOrDefault(stringToCheck => stringToCheck.Contains("Invoice Number"));
match.Replace("Invoice number", "").Trim();
Console.Write(match);
Console.Read();
}
}
catch (Exception ex)
{
Console.Write(ex);
}
}
}
}
I need match
to be INV-3337
currently it seems to be Invoice Number INV-3337
. Why this command:
match.Replace("Invoice number", "").Trim();
?
does not perform Replace.
My output:
Upvotes: 0
Views: 124
Reputation: 691
It doesn't work because the match is case sensitive (capital N vs lower case n).
match = match.Replace("Invoice Number", "").Trim();
Upvotes: 2
Reputation: 125
Instead of writing:
match.Replace("Invoice number", "").Trim();
use:
match = match.Replace("Invoice Number", "").Trim();
Upvotes: 1