10 Replies Latest reply on Jan 8, 2019 5:21 AM by try67

    Rename PDF based on content?

    Jonas Sinnaeve Level 1

      Is there a Javascript available that automatically renames a PDF based on the content?

      Some fields in the PDF should be reflected in the PDF name.

        • 1. Re: Rename PDF based on content?
          gkaiseril MVP & Adobe Community Professional

          No, since you have the PDF open, you cannot rename it.

           

          What content are you going to use?

           

          It could be saved under a new name using  form fields or the PDF properties of key words, subject, or title if the properties are field in for the PDF. But this type of coding will require some special code in folders.

          • 2. Re: Rename PDF based on content?
            Jonas Sinnaeve Level 1

            All my files have key words on the same place. And saving them under a new correct name is what im looking for. Can I send you an example of the pdf that would need to be renamed?

            • 3. Re: Rename PDF based on content?
              gkaiseril MVP & Adobe Community Professional

              We are here to help you with script problems not write the entire script for you.

               

              https://acrobatusers.com/tutorials/how-save-pdf-acrobat-javascript

              How to Save a PDF with Acrobat JavaScript

              by Thom Parker will get you started with the folder level script for saving using JavaScript. You can use the document object's info.Keywords method to get

              • 4. Re: Rename PDF based on content?
                Jonas Sinnaeve Level 1

                I have just started using the javascript function, so i'm trying to figure everything out.

                So the script below just saves your pdf as "x" in the same location. But how can I change this X into a value/text present on my PDF?

                 

                // Split Path into an array so it is easy to work with

                var aMyPath = this.path.split("/");

                 

                // Remove old file name

                aMyPath.pop();

                 

                // Add new file name

                aMyPath.push("x.pdf");

                 

                // Put path back together and save

                this.saveAs(aMyPath.join("/"));

                • 5. Re: Rename PDF based on content?
                  Dmitry Frolov Level 1

                  I know this is an old thread, but it still shows an unanswered. Renaming of PDF files with JavaScript is doable by very tedious especially if you need to extract text from a specific location(s). There is also a performance issue since working with page content in JavaScript is not fast. Using a plug-in with a user interface can be more convenient especially when dealing with this task on a regular basis. Here is a link to a tutorial that shows how to bulk rename PDF files using text from the document with AutoSplit plug-in: Automatic Renaming of PDF Files . Text can be extracted from specific page location(s) or by text search (using text patterns such as SSN, account numbers and etc.). This is a commercial plug-in for Adobe Acrobat, not a JavaScript.

                  • 6. Re: Rename PDF based on content?
                    try67 MVP & Adobe Community Professional

                    How is renaming PDF files with JS doable, tedious or not?

                    • 7. Re: Rename PDF based on content?
                      Dmitry Frolov Level 1

                      If it is necessary to extract text from a specific location on a page in order to use it in the output file name, then the algorithm is as follows:

                      1. First, use a ruler/grid tool (Use Ctrl+U and Ctrl+R keyboard shortcuts to toggle the tools on/off) to determine the coordinates on the page where the "text of interest" is located. If there are multiple areas, measure each one and record the coordinates.

                      2. In JavaScript code, enumerate all words on the page using this.getPageNthWordQuads() method that returns coordinates for each word as an array of coordinates.

                      3. Check each "quad" returned by the function against the area(s) of interest (determined manually in step 1). Complication: it is necessary to take page rotation and difference in coordinate systems into account when checking each quad. Quads are returned in the page coordinate system (user space, origin is in bottom-left corner), while coordinates on screen are measured in screen coordinate system (device space, origin is in upper-left corner, y-axis is pointing down).

                      4. If the word lies inside an "area of interest", add it to the output text string.

                      5. Once done enumerating the words, save the file using this.SaveAs() method while combining a predefined output path with the extracted text.

                       

                      Drawbacks:

                      1. Coordinates for the text needs to be measured manually.

                      2. If new documents with a different layout are used, then the script needs to be edited with new coordinates.

                      3. Can be a very slow process if there is a lot of text and a lot of files involved.

                       

                      The similar script can be used if the text of interest conforms to a well-defined text pattern (such as SSN, account number, client ID, email and etc.). Then there is no need to measure text coordinates and text can be extracted by performing a text search. In this case, all words need to be enumerated using this.getPageNthWord() method and the resulting text string can be searched using a regular expression.

                      Here is a link to a script that shows how to extracts SSNs from a page text: Acrobat Javascript Samples Scripts. Similar approach can be used to extract text and then use it to save a file with a custom file name. The advantage of this method is its flexibility, since it does not rely on a fixed text location(s) that can be easily changed. This method will be slower than a first one, since all page text needs to be extracted before it can be searched.

                      • 8. Re: Rename PDF based on content?
                        try67 MVP & Adobe Community Professional

                        I know all of that, but it's not what I asked about. I asked specifically about renaming files, which you said is possible.

                        AFAIK, JS can only save a file under a new name; It can't rename an existing file.

                        • 9. Re: Rename PDF based on content?
                          Dmitry Frolov Level 1

                          Sorry, I did not understand your question first. You are absolutely correct, JavaScript cannot rename an existing file, only to create new ones. I was under assumption that creating a new file with a different name is really counts as renaming, since there is no other option available in JS. I do not think that this is actually a bad thing, because it probably saved a lot of user files from being lost, accidentally overwritten or given the same name. In my opinion, automatic file renaming without a user review/confirmation step is kind of dangerous. If input files layout/content have been changed from what the script is designed to do, then script can no longer extract the correct text, and then consequences are grim. By saving the renamed files in a different location, the originals are preserved and can be always used to roll-back the changes or re-running the renaming algorithm. Better safe then sorry.

                          • 10. Re: Rename PDF based on content?
                            try67 MVP & Adobe Community Professional

                            I agree. Just wanted to make sure there isn't some kind of hidden feature I was not aware of that allowed it...