c# - Find certain text in PDF, then return page number found on to another section NOTE: Uses Docotic.pdf -
in following code, user input search string (barcodedata). string truncated first 5 characters, , used jobnumber. jobnumber, name of pdf scanning barcodedata.
what like, 'find it' button execute below code, return value found startpagedata.
i can't tell if program isn't scanning pdf search string, or if value isn't returning program.
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); (int = 0; < 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> /// substring of first n characters. /// </summary> public static string truncate(string source, int length) { if (source.length > length) { source = source.substring(0, length); } return source; } /// <summary> /// substring of first n characters. [slow] /// </summary> public static string truncate2(string source, int length) { return source.substring(0, math.min(length, source.length)); } } }
what problem code? if findit_click not executed on click don't associate "find it" button.
the code in findit_click looks quite correct except several things:
- it may not expect. returns the last index in text of last page search string found. may expect the index of last page search string found?
- you may use pagetext.lastindexof method find laststartindex.
- don't forget dispose pdfdocument instance. e.g. use using keyword:
using (pdfdocument pdf = new pdfdocument(pathtofile)) { ... }
Comments
Post a Comment