5 Replies Latest reply on Nov 3, 2016 9:53 AM by SpydeyNinja

    How can I combine multiple files into single files with javascript?

    SpydeyNinja

      I really hope I have this in the correct sub-forum.

       

      I will try and make this short and sweet.

       

      Any and all help is very very very much appreciated!

       

      I have two folders with PDFs in them.

       

      The first folder has the PDF files named: ABC_001, ABC_002, ABC_003, etc etc.

      The second folder has the PDF files named: XYZ_001, XYZ_002, XYZ_003, etc etc.

       

      I am running Adobe Acrobat 8 Professional. I do not have the opportunity to upgrade to a newer version of Adobe Pro so that is what I have to work with.

       

      I need to combine the PDFs in the second folder with the PDFs in the first folder and save the resulting new PDF in a third folder. The new PDF also needs to maintain the naming scheme of the PDF from the first folder.

       

      For example: XYZ_001 needs to be combined with and placed after ABC_001. The resulting new PDF should be saved in the third folder as ABC_001. If I were to open that new PDF, I would see the contents ABC_001 and then XYZ_001. Please note, it needs to be a single PDF, not a package.

       

      So basically I need to merge/combine each ABC & XYZ PDF with the same ending 3 digit number into a new single PDF.

       

      If I have 25 ABC PDFs (ABC_001 .... ABC_025) and 25 XYZ PDFs (XYZ_001 .... XYZ_025), I should have 25 PDFs at the end of it all, each PDF containing the ABC & XYZ with same ending 3 digits. I hope that makes sense.

       

      I could use the combine button, but then I have to do each file individually. Sometimes I will have 3 files to generate, sometimes I Will have 500 files. Doing this manually with 50+ files is too time consuming. I would prefer to have it done automatically but am unsure on how to implement it. I am not strong at coding. If code is given to me, I can dissect it, understand it, and successfully manipulate it; but coming up with it on my own and having it work is an ongoing struggle for me.

       

      Any ideas? Thank you again everyone for your assistance!

        • 1. Re: How can I combine multiple files into single files with javascript?
          try67 MVP & Adobe Community Professional

          Are these folders located under the same parent folder? If not, do they both have fixed and known paths?

          Doing it would involve using a combination of a batch sequence to collect the file-paths of the files in the folders (unless the file-paths are always the same) as well as a custom-made script to make the actual merge. It's not a very simple task, though.

          • 2. Re: How can I combine multiple files into single files with javascript?
            SpydeyNinja Level 1

            The file paths to each folder is fixed and known.

             

            I am already using batch sequence for a number of things with these same files.

             

            I actually start out with several different word documents, spread sheets, and other PDFs. After doing mail merges, combing files, and splitting out files, all via javascript in batch sequence, I end up with the files ABC and XYZ. The last step is combing the same-numbered files of ABC and XYZ to make one single file. That is where I am stuck. I can't figure out how to do it wit javascript and/or any of the other functions.

             

            I can work in batch sequence with javascript. It is just the actual coding that I am no good at. I understand how things work and if given to me, I can manipulate it. I am just not familiar with which calls to use and how they all function.

             

            Complicated is fine.

            • 3. Re: How can I combine multiple files into single files with javascript?
              try67 MVP & Adobe Community Professional

              In your batch sequence you can use the insertPages command. Run it on the folder with the ABC files and then change the path of the file to insert to point to the other folder, and change the file name itself from ABC to XYZ.

              So let's say the ABC files are under C:\ABC\ and the XYZ files are under C:\XYZ\. You can use this code in the batch that you run on the former:

               

              this.insertPages({nPage: this.numPages-1, cPath: "/C/XYZ/" + this.documentFileName.replace(/ABC/, "XYZ")});

               

              Then just set the output folder and you're done.

              • 4. Re: How can I combine multiple files into single files with javascript?
                SpydeyNinja Level 1

                Thanks try67! I am going to give it a whirl and see what I come up with. I will report back shortly. Thanks again for your assistance.

                 

                I may have more questions as I go about testing this and figuring it out.

                • 5. Re: How can I combine multiple files into single files with javascript?
                  SpydeyNinja Level 1

                  @ try67

                   

                  So I tried the code you gave me. I was not successful.

                   

                  I think it didn't worked for a few reasons:

                   

                  1. I believe that you were indicating to run it in conjunction with my other batch process, right? However, I don't think this will work because of how my first batch process is coded and what I have to do between my first batch process and the combining of the two files (ABC + XYZ). So I ran the code you provided separately in its own batch process script. I did not get the results I was hoping for even after tinkering around with it a bit.

                  2. If I am understanding the code correctly, it looks to me like it will have to be run each time for each file. Is that correct?

                   

                  My goal is to be able to run a single script/batch process where independent of the number of PDFs there are, the corresponding XYZ_xxx PDF is merged (combined/inserted) to the end of the corresponding ABC_xxx PDF. So XYZ_001 is merged/combined/inserted to the end of ABC_001, XYZ_002 to the end of ABC_002, XYZ_003 to the end of ABC_003, .... XYZ_761 to the end of ABC_761, etc etc.

                   

                  Perhaps if I gave more detail (a lot, sorry) as to what I do prior to the current dilemma, that would help us solve the current issue.

                   

                  I start with an excel spreadsheet containing records with specific data columns and data points.

                   

                  I have a Word Document Template (ABC) that I have already formatted with mail merge fields that match the different data points from my excel data spreadsheet.

                   

                  I merge the data from the spreadsheet via the Word Document Template (ABC) to get a single large word document. This word document contains letters to be mailed out, based off of the data from the spreadsheet. Every 5 pages is a new letter but they are all contained in a single word document. I need it split so that each individual letter is it's own PDF file. I do this via a batch process javascript in Adobe Pro 8.

                   

                  I save the single large Word document (post-merge) as a PDF to:

                  C:\ABC\Merge\ (that is not the true location but for ease of this conversation and reference, it makes things simpler)

                   

                  Then using Javascript code (below) via a batch process in Adobe Pro 8, I extract every 5 pages and save it as: ABC_xxx. The "xxx" representing the letter order found within the single large "ABC" PDF. i.e. ABC_001, ABC_002, ABC_003, etc etc., which also relates to the 1st, 2nd, 3rd, ..... etc etc., record found within my excel data spreadsheet.

                   

                  The number of letters will vary from day to day based upon the amount of data in my excel spreadsheet. Sometimes I will have 10 letters, sometimes 500. So the number of "ABC" files will vary once the batch process below is run.

                   

                  Please note that I have to use javascript because of using adobe pro 8. Pro 9 has functions already built in where you can extract the files every x number of pages. Pro 8 does not, so I have to resort to using javascript to accomplish the same thing. I do not have the option to upgrade to anything newer than Adobe pro 8.

                   

                  Here is the code I have been using (please note, I did not originally write this. I found it somewhere on the forum, manipulated it here and there to get results I wanted and adjusted a few things):

                   

                  ===BEGIN===

                   

                  /* ABC - Extract */

                  var filename = "ABC";

                  var nihe=1;

                  try {

                  for ( var i = 0; i < this.numPages; i+=5)

                  this.extractPages

                  ({

                  nStart: i, nEnd: i + 4,

                  cPath: "/C/ABC/Singles/"+filename+"_"+util.printf("%03d",i/5+1)+".pdf"

                  });

                  } catch (e) { console.println("Aborted: " + e) }

                   

                  ===END===

                   

                  When I run this batch process script, it prompts me for the single large "ABC" PDF file that I want to split out. I navigate to it at C:\ABC\Merge\, select it, and within a few seconds, it has spit out the needed results to C:\ABC\Singles\, with the naming scheme as indicated in the script. When I open C:\ABC\Singles\, I can see all my new single ABC PDFs, split/extracted from my original merged ABC PDF.

                   

                  I then have a single generic PDF that I combine/insert to the end of each ABC PDF found in C:\ABC\Singles\. I accomplish this via an insert batch process. I do not have to use javascript to do this as Adobe pro 8 has an insert function in the batch process list, I just have to configure what I want the batch process to do via the provided options the first time I set it up. Then every time I run the batch process, it prompts me for the ABC PDFs that I want to use and it inserts the generic PDF at the end of those selected ABC PDFs. Depending on which generic PDF I use, the ending merged result can be either 20 pages or 21 pages (5 from a single ABC PDF and 15 or 16 from my generic PDF insert). The resulting PDFs are saved to: C:\ABC\Combined\. I still have individual ABC PDFs, but now with the generic PDF inserted at the end of each single ABC PDF.

                   

                  Once I have combined the generic PDF to each of my ABC PDFs, I then move onto generating the XYZ PDFs.

                   

                  I have a Word Document Template (XYZ) that I have already formatted with mail merge fields which match the different data points from my excel data spreadsheet; the same excel data spread sheet that I used for the ABC PDFs.

                   

                  I merge the data from the spreadsheet via the Word Document Template (XYZ) to get a single large word document. This word document contains letters to be inserted to the end of the ABC PDFs found at C:\ABC\Combined\ (ABC letter + Generic PDF), based off of the data from the spreadsheet. Every 3 pages is a new letter but they are all contained in a single word document. I need them extracted so that each individual letter is it's own file and thus can be inserted to the corresponding ABC letter. I split/extract them via a batch process javascript in Adobe Pro 8 (see below).

                   

                  I save the single large Word document (post-merge) as a PDF to:

                  C:\XYZ\Merge\ (that is not the true location but for ease of this conversation and reference, it makes things simpler)

                   

                  Then using Javascript code (below) via a batch process in Adobe Pro 8, I extract every 3 pages and save it as: XYZ_xxx. The "xxx" representing the PDF order found within the single large "XYZ" PDF. i.e. XYZ_001, XYZ_002, XYZ_003, etc etc. The order of the XYZ PDFs matches the order of the ABC PDFs. This is to facilitate (hopefully) the insertion of the XYZ PDFs to the ABC PDFs and keep things in order.

                   

                  The number of letters will vary from day to day based upon the amount of data in my excel spreadsheet. Sometimes I will have 10 letters, sometimes 500. So the number of "XYZ" PDFs will vary once the batch process below is run. However, the number of ABC and XYZ PDFs will always match on any given day.

                   

                  Here is the code I have been using (please note, I did not originally write this. I found it somewhere on the forum, manipulated it here and there to get results I wanted and adjusted a few things):

                   

                  ===BEGIN===

                   

                  /* XYZ - Extract */

                  var filename = "XYZ";

                  var nihe=1;

                  try {

                  for ( var i = 0; i < this.numPages; i+=3)

                  this.extractPages

                  ({

                  nStart: i, nEnd: i + 2,

                  cPath: "/C/XYZ/Singles/"+filename+"_"+util.printf("%03d",i/3+1)+".pdf"

                  });

                  } catch (e) { console.println("Aborted: " + e) }

                   

                  ===END===

                   

                  When I run this batch process script, it prompts me for the single large "XYZ" PDF file that I want to split out. I navigate to it at C:\XYZ\Merge\, select it, and within a few seconds, it has spit out the needed results to C:\XYZ\Singles\, with the naming scheme as indicated in the script. When I open C:\XYZ\Singles\, I can see all my new single XYZ PDFs, split/extracted from my original merged XYZ PDF.

                   

                  All of the above leads me to my current issue which I am trying to resolve via this discussion.

                   

                  I need to take the single XYZ PDFs and insert them to the end of the corresponding ABC PDFs.

                   

                  Every time an insert happens, the resulting file needs to be saved to: C:\ABC\Final\ with a file name matching the ABC file.

                   

                  Meaning: XYZ_001 needs to be inserted to the end of ABC_001 and saved as ABC_001 to C:\ABC\Final\, XYZ_002 to the end of ABC_002 and saved as ABC_002, ..... XYZ_783 to the end of ABC_783 and saved as ABC_783, etc etc.

                   

                  I hope that we can do this via a new and separate batch process javascript. I would prefer a new one instead of adding it to an existing one as new code. Having a separate one will allow me to go in and make any needed changes to the ABC & XYZ PDFs after I split them but before I insert them together.

                   

                  I was thinking that maybe we could do something like this (this is just an idea and I have no idea if it will work or not):

                   

                  When the script is run, have it count the number of PDFs in the ABC\Combined\ folder and the XYZ\Singles folder to make sure they contain the same number of PDFs.

                   

                  If the number of PDFs in each folder is the same, then based upon the number of PDFs in the count, insert the XYZ_xxx to the end of ABC_xxx and save to C:\ABC\Final\ with a file name of ABC_xxx. The "xxx" will have to go up consecutively each time until it reaches the end of the PDF count.

                   

                  I hope that all makes sense and I sincerely apologize for the SUPER long explanation. It is a very long read but I hope that it contains all the needed information so that we can work on finding a resolution to my issue.

                   

                  I really deeply appreciate the time and effort that anyone is putting into the to help me resolve the problem. Thank you.