Sample JavaScript code for using Apryse SDK to search text on PDF pages using regular expressions. The TextSearch utility class builds on functionality available in TextExtractor to simplify most common search operations. Learn more about our Web SDK and PDF Indexed Search Library.
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6(exports => {
7
8
9
10
11
12
13 exports.runTextSearchTest = () => {
14 const PDFNet = exports.Core.PDFNet;
15
16 const main = async () => {
17 // Relative path to the folder containing test files.
18 const inputURL = '../TestFiles/';
19 const inputFilename = 'credit card numbers.pdf'; // addimage.pdf, newsletter.pdf
20
21 try {
22 const doc = await PDFNet.PDFDoc.createFromURL(inputURL + inputFilename);
23 doc.initSecurityHandler();
24 doc.lock();
25
26 const txtSearch = await PDFNet.TextSearch.create();
27 let mode = PDFNet.TextSearch.Mode.e_whole_word + PDFNet.TextSearch.Mode.e_page_stop; // Uses both whole word and page stop
28 let pattern = 'joHn sMiTh';
29
30 txtSearch.begin(doc, pattern, mode); // searches for the "pattern" in the document while following the inputted modes.
31
32 let step = 0;
33
34 // call Run() iteratively to find all matching instances of the word 'joHn sMiTh'
35 /* eslint-disable-next-line no-constant-condition */
36 while (true) {
37 const result = await txtSearch.run();
38 let hlts;
39 if (result.code === PDFNet.TextSearch.ResultCode.e_found) {
40 if (step === 0) {
41 // Step 0: found "John Smith"
42 // note that, here, 'ambient_str' and 'highlights' are not written to,
43 // as 'e_ambient_string' and 'e_highlight' are not set.
44 console.log(result.out_str + "'s credit card number is: ");
45
46 // now switch to using regular expressions to find John's credit card number
47 mode = await txtSearch.getMode();
48 mode += PDFNet.TextSearch.Mode.e_reg_expression + PDFNet.TextSearch.Mode.e_highlight;
49 txtSearch.setMode(mode);
50 pattern = '\\d{4}-\\d{4}-\\d{4}-\\d{4}'; // or "(\\d{4}-){3}\\d{4}"
51 txtSearch.setPattern(pattern);
52
53 ++step;
54 } else if (step === 1) {
55 // step 1: found John's credit card number
56 console.log(' ' + result.out_str);
57 // note that, here, 'hlts' is written to, as 'e_highlight' has been set.
58 // output the highlight info of the credit card number.
59 hlts = result.highlights;
60 hlts.begin(doc);
61 while (await hlts.hasNext()) {
62 const highlightPageNum = await hlts.getCurrentPageNumber();
63 console.log('The current highlight is from page: ' + highlightPageNum);
64 await hlts.next();
65 }
66 // see if there is an AMEX card number
67 pattern = '\\d{4}-\\d{6}-\\d{5}';
68 txtSearch.setPattern(pattern);
69
70 ++step;
71 } else if (step === 2) {
72 // found an AMEX card number
73 console.log('\nThere is an AMEX card number:\n ' + result.out_str);
74
75 // change mode to find the owner of the credit card; supposedly, the owner's
76 // name proceeds the number
77 mode = await txtSearch.getMode();
78 mode += PDFNet.TextSearch.Mode.e_search_up;
79 txtSearch.setMode(mode);
80 pattern = '[A-z]++ [A-z]++';
81 txtSearch.setPattern(pattern);
82
83 ++step;
84 } else if (step === 3) {
85 // found the owner's name of the AMEX card
86 console.log("Is the owner's name:\n " + result.out_str + '?');
87
88 // add a link annotation based on the location of the found instance
89 hlts = result.highlights;
90 await hlts.begin(doc); // is await needed?
91 while (await hlts.hasNext()) {
92 const curPage = await doc.getPage(await hlts.getCurrentPageNumber());
93 const quadArr = await hlts.getCurrentQuads();
94 for (let i = 0; i < quadArr.length; ++i) {
95 const currQuad = quadArr[i];
96 const x1 = Math.min(Math.min(Math.min(currQuad.p1x, currQuad.p2x), currQuad.p3x), currQuad.p4x);
97 const x2 = Math.max(Math.max(Math.max(currQuad.p1x, currQuad.p2x), currQuad.p3x), currQuad.p4x);
98 const y1 = Math.min(Math.min(Math.min(currQuad.p1y, currQuad.p2y), currQuad.p3y), currQuad.p4y);
99 const y2 = Math.max(Math.max(Math.max(currQuad.p1y, currQuad.p2y), currQuad.p3y), currQuad.p4y);
100
101 const hyperLink = await PDFNet.LinkAnnot.create(doc, await PDFNet.Rect.init(x1, y1, x2, y2));
102 await hyperLink.setAction(await PDFNet.Action.createURI(doc, 'http://www.apryse.com'));
103 await curPage.annotPushBack(hyperLink);
104 }
105 hlts.next();
106 }
107 const docBuffer = await doc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_linearized);
108 saveBufferAsPDFDoc(docBuffer, 'credit card numbers_linked.pdf');
109 break;
110 }
111 } else if (result.code === PDFNet.TextSearch.ResultCode.e_page) {
112 // you can update your UI here, if needed
113 console.log('page end');
114 } else if (result.code === PDFNet.TextSearch.ResultCode.e_done) {
115 break;
116 }
117 }
118 } catch (err) {
119 console.log(err);
120 }
121 };
122 // add your own license key as the second parameter, e.g. PDFNet.runWithCleanup(main, 'YOUR_LICENSE_KEY')
123 PDFNet.runWithCleanup(main);
124 };
125})(window);
126// eslint-disable-next-line spaced-comment
127//# sourceURL=TextSearchTest.js
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales