Sunday, July 29, 2018

Splitting PDF File Names from a CSV Source

Splitting PDF File Names from a CSV Source


The previous post explored splitting a multi-page PDF file into single pages using a variable file name. This was achieved using �Tag� metadata from Adobe InDesign and Acrobat �Top-level Bookmarks� to split the document.

Another approach is to use Acrobat Pro�s support of JavaScript to split a multi-page PDF into separate pages using data from a spreadsheet .CSV file.

A great Acrobat Pro JavaScript is mentioned by Alan Gilbertson in a thread over on the Graphic Design Stack Exchange website.

Alan wrote:
�The script runs in Acrobat. It takes the CSV file as an input (said CSV must have a column named �filename� containing the individual name for the extracted PDF). It counts the total number of pages in the PDF and divides by the total number of records in the CSV in order to calculate the number of pages per individual PDF, then iterates through the document, extracting pages as it goes. There is user input for text to prefix and/or suffix the output file names.�

Thank you Alan!

The script is placed into an Acrobat Pro �Action� that executes a JavaScript. I have tested this script in Acrobat Pro X, however this script code may or may not perform as expected in other versions.

The source .csv file is very simple, it only requires a single column, with the key being that the column header (row 1) has to be named: filename

An example may be a 5 page PDF. The .csv used to split the PDF with the appropriate file names would have the following structure:

filename
First Page
2nd-page
page_3
iv
p5

The resulting split PDF files would be named:

First Page.pdf
2nd-page.pdf
page_3.pdf
iv.pdf
p5.pdf



The Acrobat Pro JavaScript code follows (there should be 86 lines of code if you copy/paste this script from this page):


var CSV = function (data, delimiter) {
var _data = CSVToArray(data, delimiter);
var _head = _data.shift();
return {
length: function () {return _data.length;},
adjustedLength: function () {return _data.length - 1;},
getRow: function (row) {return _data[row];},
getRowAndColumn: function (row, col) {
if (typeof col !== "string") {
return _data[row][col];
} else {
col = col.toLowerCase();
for (var i in _head) {
if (_head[i].toLowerCase() === col) {
return _data[row][i];
}
}
}
}
};
};
function CSVToArray( strData, strDelimiter ){
strDelimiter = (strDelimiter || ",");
var objPattern = new RegExp(
(
// Delimiters.
"(" + strDelimiter + ""|r?n|r|^)"" +
// Quoted fields.
""(?:""([^""]*(?:""""[^""]*)*)""|"" +
// Standard fields.
""([^"""" + strDelimiter + ""rn]*))""
)

visit link download