在下面的代码中,用户将输入一个搜索字符串(barcodedata)。然后该字符串将被截断为前 5 个字符,并用作作业编号。作业编号也是我将扫描条形码数据的 pdf 的名称。
我想要的是“查找”按钮执行以下代码,然后将找到的值返回给 startpagedata。
我无法判断程序是否实际上并未扫描 PDF 以查找搜索字符串,或者该值是否根本没有返回到程序。
using BitMiracle.Docotic.Pdf;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Windows.Forms;
using Acrobat;
namespace BarCodeReader
{
public partial class Form1 : Form
{
public string stringsToFind;
public string pathtofile;
public Form1()
{
InitializeComponent();
}
private void barcodedata_TextChanged(object sender, EventArgs e)
{
stringsToFind=barcodedata.Text;
pathtofile = "C:\\" + StringTool.Truncate(barcodedata.Text, 5) + ".pdf";
jobnumberdata.Text = StringTool.Truncate(barcodedata.Text, 5);
}
private void label4_Click(object sender, EventArgs e)
{
}
private void jobnumberdata_TextChanged(object sender, EventArgs e)
{
jobnumberdata.Text = jobnumberdata.Text.TrimStart('0');
Console.WriteLine(jobnumberdata.Text);
}
private void startpagedata_TextChanged(object sender, EventArgs e)
{
Console.WriteLine(startpagedata.Text);
}
private void piecesdata_TextChanged(object sender, EventArgs e)
{
}
private void FindIt_Click(object sender, EventArgs e)
{
PdfDocument pdf = new PdfDocument(pathtofile);
for (int i = 0; i < pdf.Pages.Count; i++)
{
string pageText = pdf.Pages[i].GetText();
int count = 0;
int lastStartIndex = pageText.IndexOf(stringsToFind, 0, StringComparison.CurrentCultureIgnoreCase);
while (lastStartIndex != -1)
{
count++;
lastStartIndex = pageText.IndexOf(stringsToFind, lastStartIndex + 1, StringComparison.CurrentCultureIgnoreCase);
}
if (count != 0)
startpagedata.Text = Convert.ToString(lastStartIndex);
}
}
}
public static class StringTool
{
/// <summary>
/// Get a substring of the first N characters.
/// </summary>
public static string Truncate(string source, int length)
{
if (source.Length > length)
{
source = source.Substring(0, length);
}
return source;
}
/// <summary>
/// Get a substring of the first N characters. [Slow]
/// </summary>
public static string Truncate2(string source, int length)
{
return source.Substring(0, Math.Min(length, source.Length));
}
}
}