替换存储在字符串中的文本
Replace text stored in string
使用 iText 5.5,我正在尝试将列表中的值分配给变量。有热心帮助业余爱好者的专业人士吗?
我正在解析的文件:https://slicedinvoices.com/pdf/wordpress-pdf-invoice-plugin-sample.pdf
这是我的代码:
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);
}
}
}
}
我需要 match
才能 INV-3337
目前看来是 Invoice Number INV-3337
。为什么这个命令:
match.Replace("Invoice number", "").Trim();
?
不执行替换。
我的输出:
它不起作用,因为匹配区分大小写(大写 N 与小写 n)。
match = match.Replace("Invoice Number", "").Trim();
而不是写作:
match.Replace("Invoice number", "").Trim();
使用:
match = match.Replace("Invoice Number", "").Trim();
使用 iText 5.5,我正在尝试将列表中的值分配给变量。有热心帮助业余爱好者的专业人士吗?
我正在解析的文件:https://slicedinvoices.com/pdf/wordpress-pdf-invoice-plugin-sample.pdf
这是我的代码:
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);
}
}
}
}
我需要 match
才能 INV-3337
目前看来是 Invoice Number INV-3337
。为什么这个命令:
match.Replace("Invoice number", "").Trim();
?
不执行替换。
我的输出:
它不起作用,因为匹配区分大小写(大写 N 与小写 n)。
match = match.Replace("Invoice Number", "").Trim();
而不是写作:
match.Replace("Invoice number", "").Trim();
使用:
match = match.Replace("Invoice Number", "").Trim();