• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Code not working as expected (search and extract)

New Here ,
Mar 14, 2023 Mar 14, 2023

Copy link to clipboard

Copied

Hello.

I'm really newbie in this theme. But I'm trying to run this script but don't why Is not working as expected:

Use case:
Appear a box that I can insert a list of words to be searched in the PDF File like this
Test,Apple,Banana
Most search for each word indenpendently and if it encounters should appear the list box in order to perform different scenarios depending on the user choice.

Problem:
This scenarios are not working as expected it is not ending the function as it should be 

 // If cResponse = "" and Ok button pressed appear app.alert "Please insert the words to be searched"
// If cResponse <> "" and Ok button pressed execute app.execDialog(dialog3);
// If cResponse <> and Cancel button pressed exit the function
// If cResponse = "" and Cancel button pressed exit the function
 
And also this options are not working
    // Extract all pages together (+1 option selected and user select Ok) to the path of the original file with name: Original file_word searched
    // Extract all pages together but separately regarding each word (+2 option selected and user select Ok)  to the path of the original file with name: Original file_word searched
//Extract first page (+3 option selected and user select Ok) to the path of the original file with name: Original file_word searched

 

 

function searchAndExtract() {
    // Get the current document
    var currentDoc = this;
  
    // Get the user's input from the response prompt
    var cResponse = app.response({
      cQuestion: "Please specify the word(s) that you would like to search for.",
      cTitle: "Search word(s) and extract page(s)",
      type: "ok_cancel",
      ok_name: "Ok",
      cancel_name: "Cancel"
    });
  
    // Check if the Ok button was pressed and the user entered a search query
    if (cResponse && cResponse.length > 0) {
      var dialogResult = app.execDialog(dialog3);
      // Check if the Ok button was pressed in the sub-dialog and a valid option was selected
      if (dialogResult === "ok" && dialog3.store()["subl"] !== undefined) {
        extractPages(currentDoc, cResponse, dialog3.store()["subl"]);
      } else {
        // If the sub-dialog was cancelled or an invalid option was selected, show an alert and end the function
        app.alert("Please select a valid option");
        return;
      }
    } else if (cResponse === "") {
      // Show an alert if the user did not enter anything and end the function
      app.alert("Please insert the words to be searched");
      return;
    }
  }
  
  function extractPages(doc, searchTerm, subType) {
    // Find all the pages that contain the search term
    var matchingPages = [];
    for (var i = 0; i < doc.numPages; i++) {
      var pageText = doc.getPageText(i);
      if (pageText.indexOf(searchTerm) !== -1) {
        matchingPages.push(i);
      }
    }
  
    // Extract the pages based on the selected subType
    switch (subType) {
      case +1:
        // Extract all pages together
        var newDoc = new Document();
        for (var i = 0; i < matchingPages.length; i++) {
          newDoc.insertPages({
            nPage: i,
            cPath: doc.path.replace(/\.pdf$/i, "_" + searchTerm + ".pdf"),
            nStart: matchingPages[i],
            nEnd: matchingPages[i]
          });
        }
        newDoc.save();
        break;
      case -2:
        // Extract all pages together but separately for each word
        var newDocs = {};
        for (var i = 0; i < matchingPages.length; i++) {
          var pageText = doc.getPageText(matchingPages[i]);
          var words = pageText.split(/\s+/);
          for (var j = 0; j < words.length; j++) {
            if (words[j].indexOf(searchTerm) !== -1) {
              var newDoc = newDocs[words[j]];
              if (!newDoc) {
                newDoc = new Document();
                newDocs[words[j]] = newDoc;
              }
              newDoc.insertPages({
                nPage: newDoc.numPages,
                cPath: doc.path.replace(/\.pdf$/i, "_" + words[j] + ".pdf"),
                nStart: matchingPages[i],
                nEnd: matchingPages[i]
              });
            }
          }
        }
        for (var word in newDocs) {
          newDocs[word].save();
        }
        break;
      case -3:

      var pageNumbers = {};
      var searchTextArray = [searchTerm];
      for (var i = 0; i < matchingPages.length; i++) {
        var pageText = doc.getPageText(matchingPages[i]);
        for (var j = 0; j < searchTextArray.length; j++) {
          var searchText = searchTextArray[j];
          var index = pageText.indexOf(searchText);
          if (index !== -1) {
            // Store the page number for the search text
            var pageNumber = matchingPages[i] + 1; // getPageText uses 0-based indexing, so add 1 to get the actual page number
            if (pageNumbers[searchText] === undefined) {
              pageNumbers[searchText] = [pageNumber];
            } else {
              pageNumbers[searchText].push(pageNumber);
            }
          }
        }
      }
      if (pageNumbers[searchTerm] !== undefined) { // Only extract if the search text was found
        var outputFileName = searchTerm + "_page" + pageNumbers[searchTerm][0] + ".pdf"; // Extract the first page only
        var outputFilePath = doc.path.replace(/\.pdf$/, '') + "_" + outputFileName;
        doc.extractPages(pageNumbers[searchTerm][0] - 1, pageNumbers[searchTerm][0] - 1, outputFilePath); // getPageText uses 0-based indexing, so subtract 1 to get the correct page number
        console.println("Page " + pageNumbers[searchTerm][0] + " containing the text \"" + searchTerm + "\" extracted and saved to file: " + outputFilePath);
      }
      break;
    default:
      // If an invalid subType is selected, do nothing
      break;
  }
}

var dialog3 = {
  // This dialog box is called when the dialog box is created
  initialize: function(dialog) {
    this.loadDefaults(dialog);
  },
  // The dialog box is called when the OK button is clicked.
  commit: function(dialog) {
    // See the Dialog object for a description of how dialog.load
    // and dialog.store work.
    var elements = dialog.store()["subl"];
    // Do something with the data.
  },
  // Callback for when the button "butn" is clicked.
  butn: function(dialog) {
    var elements = dialog.store()["subl"]
    for (var i in elements) {
      if (elements[i] > 0) {
        app.alert("You chose \"" + i + "\", which has a value of " + elements[i]);
      }
    }
  },
  loadDefaults: function(dialog) {
    dialog.load({
      subl: {
        "Extract all pages together": +1,
        "Extract all pages together but separately according to each word": -2,
        "Extract the first page": -3
      }
    })
  },

  description: {
    name: "Type of operation to perform", // Title of the dialog box
    elements: [ // Child element array
      {
        type: "view",
        align_children: "align_left",
        elements: [ // Child element array
          {
            type: "cluster",
            name: "Select",
            elements: [ // Child Element Array
              {
                type: "static_text",
                name: "Select the operation",
                font: "default"
              },
              {
                type: "list_box",
                item_id: "subl",
                width: 600,
                height: 100
              },
            ]
          },
          {
            type: "ok_cancel",
            ok_name: "Ok",
            cancel_name: "Cancel"
          }
        ]
      }
    ]
  }
};

// Call the searchAndExtract function to start the process
searchAndExtract();
// Show the sub-dialog box and get the user's input
app.execDialog(dialog3);
​



 

TOPICS
Edit and convert PDFs , How to

Views

888

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 14, 2023 Mar 14, 2023

Copy link to clipboard

Copied

The script will not work in Acrobat Reader.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 14, 2023 Mar 14, 2023

Copy link to clipboard

Copied

Why? I'm trying to apply this into PDF-XChange Editor 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 14, 2023 Mar 14, 2023

Copy link to clipboard

Copied

Acrobat Reader can't extract pages.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 15, 2023 Mar 15, 2023

Copy link to clipboard

Copied

LATEST

We have no idea what the PDF-XChange editor can and can't do. That for the people who make it to say. Adobe don't provide this forum to give free support to their business rivals.

If you have issues running the script in Acrobat Pro, we are qualified to look at that problem, please tell us what it says in the JavaScript Console.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines