2 Replies Latest reply on Dec 19, 2011 11:43 AM by John Frederick Chionglo

    Reading an external file

    czigany

      I'm new to Acrobat scripting though I've done some for JavaScript for InDesign. It seems very different and I'm having difficulty wrapping my head around it.

       

      I need to open and read an external text file that contains names of PDF files. Then, I need to merge all those PDF file into a single new PDF. First, is this possible?

       

      Second, can anyone point me to a source for some sample scripts that will help get me started?

       

      Thanks.

        • 1. Re: Reading an external file
          Bernd Alheit Adobe Community Professional & MVP

          It is possible with Visual Basic and Acrobat Javascript.

          • 2. Re: Reading an external file
            John Frederick Chionglo Level 1
            The following code is one way to satisfy your requirements.

            A supporting document may be found at

              http://www.aespen.ca/AEnswers/czigany-dec062011.pdf.

            The supporting document contains a step-by-step instruction on what you need to do to use the anonymous function below.

            I do not know how to upload a PDF file to this forum. This is why I am posting the supporting document to a different site.

            I hope this helps.

            Regards,
            John

            /***** Start of code *****/

            (function(parms) {

              var path = parms.path;

              var width = 612;
              if (parms.width) width = parms.width;

              var height = 792;
              if (parms.height) height = parms.height;

              var mFile = "mergedFiles.pdf";
              if (parms.mFile) mFile = parms.mFile;

              var fFile = parms.fFile;
              if (parms.fFile) fFile = parms.fFile;

              var doc = app.newDoc({ nWidth: width, nHeight: height });

              var dobjName = "temp$" + (new Date().valueOf());

              doc.saveAs({ cPath: path + mFile });

              doc.importDataObject({ cName: dobjName, cDIPath: path + fFile });

              var rstr = doc.getDataObjectContents(dobjName);

              var str = util.stringFromStream(rstr);

              var fns = eval(str);

              var i;
              for (i in fns) {
              doc.insertPages({ nPage: doc.numPages-1, cPath: fns[i] });
              }

              doc.deletePages({ nStart: 0, nEnd: 0 });
              doc.removeDataObject(dobjName);

              })({
              path: "/E/Exercises/czigany/Dec062011/",
              mFile: "mergedFiles.pdf",
              pageWidth: 612,
              pageHeight: 792,
              fFile: "Filenames.txt"
            });

            /*****
              *
              * Author: John Chionglo
              * email: john.chionglo@aespen.ca
              * Date: December 2011
              *
              * To use this anonymous function, you must run it as a
              * console event in Acrobat 9 Pro.
              *
              * Anonymous Function Parameters:
              *
              * path
              *
              * A valid directory where the user running the script
              * will save the merged file.
              *
              * mFile
              *
              * The name of the merged file.
              *
              * Default: mergedFiles.pdf.
              *
              * pageWidth
              *
              * The width of a page in pixels.
              *
              * Default: 612
              *
              * pageHeight
              *
              * The height of a page in pixels.
              *
              * Default: 792
              *
              * fFile
              *
              * The file which contains the file names of documents
              * to be merged. File format: array in JSON format.
              *
              * For example,
              *
              * [ "Doc1.pdf", "Doc2.pdf", "Doc3.pdf" ]
              *
              *
              * Summary of Acrobat/JavaScript API used in the code:
              *
              * app.newDoc, doc.saveAs, doc.importDataObject,
              * doc.getDataObjectContents, util.stringFromStream,
              * doc.insertPages, doc.deletePages, doc.removeDataObject
              *
              * References:
              *
              * Adobe Systems Incorporated. (2010). Adobe Acrobat 9 Pro
              * [Version 9.4.6, software]. San Jose, CA: Adobe Systems
              * Incorporated.
              *
              * Adobe Systems Incorporated. (2007). Adobe Acrobat SDK 8.1
              * JavaScript for Acrobat API Reference for Microsoft
              * Windows and Mac OS. Retrieved on Aug. 3, 2011 from
              * http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/js_api_reference.pdf.
              *
              * Flanagan, D. (2006). JavaScript: The Definitive Guide.
              * Sebastopol, CA: O'Reilly Media Inc.
              *
              *****/
              /**** end of code *****/