C# 在PDF中查找某些文本,然后将在另一节中找到的页码返回注:Uses Docotic.PDF
在下面的代码中,用户将输入一个搜索字符串(barcodedata)。然后,该字符串将被截断为前5个字符,并用作jobnumber。jobnumber,也是我将扫描条形码数据的pdf的名称 我想让“Find it”按钮执行下面的代码,然后将找到的值返回给startpagedata 我无法判断程序是否实际上没有扫描PDF以查找搜索字符串,或者该值是否没有返回到程序C# 在PDF中查找某些文本,然后将在另一节中找到的页码返回注:Uses Docotic.PDF,c#,.net,C#,.net,在下面的代码中,用户将输入一个搜索字符串(barcodedata)。然后,该字符串将被截断为前5个字符,并用作jobnumber。jobnumber,也是我将扫描条形码数据的pdf的名称 我想让“Find it”按钮执行下面的代码,然后将找到的值返回给startpagedata 我无法判断程序是否实际上没有扫描PDF以查找搜索字符串,或者该值是否没有返回到程序 using BitMiracle.Docotic.Pdf; using System; using System.Collections
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));
}
}
}
使用BitMiracle.Docotic.Pdf;
使用制度;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统诊断;
使用System.IO;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Web;
使用System.Windows.Forms;
使用杂技演员;
命名空间条形码阅读器
{
公共部分类Form1:Form
{
公共字符串字符串查找;
公共字符串pathtofile;
公共表格1()
{
初始化组件();
}
私有void条码数据_TextChanged(对象发送方,事件参数e)
{
stringsToFind=barcodedata.Text;
pathtofile=“C:\\”+StringTool.Truncate(barcodedata.Text,5)+“.pdf”;
jobnumberdata.Text=StringTool.Truncate(barcodedata.Text,5);
}
私有无效标签4_单击(对象发送方,事件参数e)
{
}
private void jobnumberdata_TextChanged(对象发送方,事件参数e)
{
jobnumberdata.Text=jobnumberdata.Text.TrimStart('0');
Console.WriteLine(jobnumberdata.Text);
}
私有void startpagedata_TextChanged(对象发送方,事件参数e)
{
Console.WriteLine(startpagedata.Text);
}
私有无效片段Data_TextChanged(对象发送方,事件参数e)
{
}
私有void FindIt_单击(对象发送方,事件参数e)
{
PdfDocument pdf=新PdfDocument(路径文件);
对于(int i=0;iLength)
{
source=source.Substring(0,长度);
}
返回源;
}
///
///获取前N个字符的子字符串。[慢速]
///
公共静态字符串截断2(字符串源,整数长度)
{
返回source.Substring(0,Math.Min(长度,source.length));
}
}
}
此代码有什么问题?如果单击时未执行FindIt_Click,则可能不会将其与“Find it”按钮关联
FindIt_Click中的代码看起来非常正确,除了以下几点:
- 它可能做不到你所期望的。它返回搜索字符串所在的最后一页文本中的最后一个索引。但是,您是否期望找到搜索字符串的最后一页的索引
- 您可以使用方法快速查找lastStartIndex
- 不要忘记处理PdfDocument实例。例如,使用关键字:
这个代码有什么问题?如果单击时未执行FindIt_Click,则可能不会将其与“Find it”按钮关联 FindIt_Click中的代码看起来非常正确,除了以下几点:
- 它可能做不到你所期望的。它返回搜索字符串所在的最后一页文本中的最后一个索引。但是,您是否期望找到搜索字符串的最后一页的索引
- 您可以使用方法快速查找lastStartIndex
- 不要忘记处理PdfDocument实例。例如,使用关键字:
要使用
Docotic.pdf
可以使用Acrobat.dll
查找当前页码。首先,打开pdf文件并使用
Acroavdoc.open("Filepath","Temperory title")
及
如果在此pdf文件中找到字符串,则光标将移动到特定页面中,搜索的字符串将突出显示。现在我们使用Acroavpageview.GetPageNum()
获取当前页码
Dim AcroXAVDoc As CAcroAVDoc
Dim Acroavpage As AcroAVPageView
Dim AcroXApp As CAcroApp
AcroXAVDoc = CType(CreateObject("AcroExch.AVDoc"), Acrobat.CAcroAVDoc)
AcroXApp = CType(CreateObject("AcroExch.App"), Acrobat.CAcroApp)
AcroXAVDoc.Open("File path", "Original document")
AcroXAVDoc.FindText("String is to searched", True, True, False)
Acroavpage = AcroXAVDoc.GetAVPageView()
Dim x As Integer = Acroavpage.GetPageNum
MsgBox("the string found in page number" & x)
要使用
Docotic.pdf
可以使用Acrobat.dll
查找当前页码。首先,打开pdf文件并使用
Acroavdoc.open("Filepath","Temperory title")
及
如果在此pdf文件中找到字符串,则光标将移动到特定页面中,搜索的字符串将突出显示。现在我们使用Acroavpageview.GetPageNum()
获取当前页码
Dim AcroXAVDoc As CAcroAVDoc
Dim Acroavpage As AcroAVPageView
Dim AcroXApp As CAcroApp
AcroXAVDoc = CType(CreateObject("AcroExch.AVDoc"), Acrobat.CAcroAVDoc)
AcroXApp = CType(CreateObject("AcroExch.App"), Acrobat.CAcroApp)
AcroXAVDoc.Open("File path", "Original document")
AcroXAVDoc.FindText("String is to searched", True, True, False)
Acroavpage = AcroXAVDoc.GetAVPageView()
Dim x As Integer = Acroavpage.GetPageNum
MsgBox("the string found in page number" & x)
当我点击“查找”时,程序会运行,然后显示“您正在使用试用版…”对话框,但实际上不会显示除此之外的任何内容(当我将该部分代码取出并作为控制台应用程序运行时)。试用限制规定只扫描pdf的前半部分,因此为了安全起见,我从前半部分页面抓取了搜索字符串。我希望导出实际的页面麻木