Pre-Processing Documents Prior to Splitting using Acrobat JavaScript
- Introduction
- In this tutorial, we'll look at how to use pre-processing scripts during split operations. A pre-processing script is executed on an input document prior to running the splitting operation. Use them to make changes to a document that could assist with splitting, or introduce new content or pages. It uses Acrobat’s JavaScript language - the standard scripting language of Adobe Acrobat and PDF documents.
- Use Pre-processing Scripts to:
-
- Add a cover page with the name of the document
- Add or delete bookmarks to affect a "splitting by bookmarks" operation
- Select a custom splitting profile (instead of the current one) based on a document's content
- Add or delete pages using a custom processing logic
- Save a copy of the input file into another folder
- Add Bates stamps to pages in the input document
- We'll use the first option above to demonstrate adding a pre-processing script to a split operation (adding a cover page to an input document). The code used will create a new page containing a text label, and insert it at the beginning of the input document. Note that it doesn’t matter what splitting settings are used for the cover page to be inserted.
- The split operation will then be carried out after the cover page has been inserted. It will become the first output document if splitting into single pages.
- Prerequisites
- You need a copy of Adobe Acrobat along with the AutoSplit plug-in installed on your computer in order to use this tutorial. Both are available as trial versions.
- Example 1: Inserting a Cover Page
- Step 1 - Open the “Split Document Settings” Menu
- With the file to be processed open in Acrobat®, select “Plug-ins > Split Documents > Split Document…” from the main Acrobat® menu to open the “Split Document Settings” dialog.
- Step 2 - Add a Pre-processing Script
- Check "Apply pre-processing script:" under "Pre-processing Options", then press "Edit..." next to it.
- Step 3 - Enter JavaScript Code
- In the dialog that opens, enter the desired JavaScript code.
- In this example, we will use the following code to generate a new page and insert it before the input document. It will also place a text label into the middle of the page. The page label consists of 2 lines of text. The first line shows "#### Cover Page" and the second line contains the name of the input file (just a filename without a full path).
-
var Rect = this.getPageBox("Crop");
this.newPage(0, Rect[2], Rect[1]);
this.addWatermarkFromText("#### CoverPage\n" + this.documentFileName, 0, font.Helv, 12, color.black, 0, 0);
- Press "OK" once done.
- Step 4 - Confirm the Split Operation
- Configure an output folder and naming scheme. In this example, files will be named "Invoice_" followed by an auto-incrementing number.
- Press “OK” to proceed.
- Press “OK” again to confirm the split.
- Step 5 - Inspect Output Files
- Open the output folder to view the results. In this example, the input document has been split into single-page files. The cover page has been inserted, and became the first output file.
- This text label on the cover page has been generated:
- If the same procedure is carried out with the "Apply pre-processing script:" option unchecked, there is no 4th output file (cover page).
- Example 2: Applying a Custom Splitting Profile
- The following code sets an AutoSplit_SplitDocument_Settings metadata property on the current document. This is a special metadata property that can be used to assign a different splitting profile while splitting the current document. It needs to specify a full path to an existing splitting profile - a settings file with an *.apr file extension. It contains a complete set of settings for splitting PDF documents. Modify the file path to inform the plug-in where the desired *.apr settings file is saved.
-
this.info.AutoSplit_SplitDocument_Settings= "c:\\data\\SplittingProfile.apr";
- You can create settings files by pressing the "Save Profile..." button located on the "Split Settings" dialog. This feature allows you to use different splitting settings based on a custom processing logic and/or document content.
- See the separate tutorial on using document-specific rules to apply splitting profiles based on a text search.
- IMPORTANT
- The pre-processing script is typically used to make changes to the input document. If a splitting operation is executed on the same file multiple times, be aware that this document may have already been modified in the previous run(s). Keep this in mind while using this feature or writing the script.
- If a PDF document is modified while using a pre-processing script, you will be prompted to save changes to the file when closing the document in Adobe Acrobat. If this is not desired, add the following line to the end of the script:
-
// this line will prevent Acrobat from prompting to save changes to the input document
this.dirty = false;
- You can find more AutoSplit tutorials here.