Active1 year, 11 months ago
- Acrobat Dc Javascript
- Javascript In Adobe Acrobat Forms
- Javascript In Adobe Acrobat
- Using Javascript In Adobe Acrobat Reader
Thank you for sharing. The point is that I designed a form with script validation on certain fields. If the user use an Adobe Reader with the setting for 'Enable Acrobat Javascript' not checked, those validations using script will not be executed. The form data will not be correct. Omnisphere keygen download. How to warn user if the setting is not enable?
Waves plug-ins crack windows. This may seem like a really novice question, but I have been tearing my hair out all day on this.
I am running the trial version of Adobe Acrobat Professional XII am looking to add simple JavaScript to a PDF file.
The aim is to have a PDF file, that when opened, pops up with an alert message with Yes and No options.Click Yes, and the alert goes away, leaving the PDF to be read.Click No, and the file closes.
The main trouble I am having is where to write my code. I have used the JavaScript Debugger but it is not very intuitive, and doesn't seem to let me add JavaScript to the file, only run from within the debugger.
All the tutorials I have looked at online show me the code, but not where to write it.
Devan SomaiaDevan Somaia49511 gold badge66 silver badges1919 bronze badges
1 Answer
In Adobe Acrobat open Tools pane on the right, select JavaScript section and then Document JavaScripts.
Enter a script name and click Add. In the JavaScript editor remove the generated code and add yours:
Enter a script name and click Add. In the JavaScript editor remove the generated code and add yours:
This code (if not placed in a function) will be executed automatically when the document is opened.
iPDFdeviPDFdev4,37422 gold badges1313 silver badges1818 bronze badges
Not the answer you're looking for? Browse other questions tagged javascriptpdfadobeacrobat or ask your own question.
Introduction:
1. What is Acrobat JavaScript?
2. What can you do with Acrobat JavaScript?
3. Where can I read more about Acrobat JavaScript?
4. How to create a batch sequence with JavaScript code?
5. Sample Acrobat JavaScripts: Introduction
2. What can you do with Acrobat JavaScript?
3. Where can I read more about Acrobat JavaScript?
4. How to create a batch sequence with JavaScript code?
5. Sample Acrobat JavaScripts: Introduction
Q & A
- What is Acrobat JavaScript?
- JavaScript is an object-oriented scripting language developed by Netscape Communications. Acrobat JavaScript implements extensions, in the form of new objects and their accompanying methods and properties, to the JavaScript programming language. These Acrobat-specific objects enable a developer to manipulate a PDF file, allowing the PDF file to communicate with a database, modify its appearance, and so on.
- What can you do with Acrobat JavaScript?
- Acrobat JavaScript enables you to perform calculations in form fields, respond to user actions, validate user data, modify appearance of Acrobat application, control the behavior of PDF documents, dynamically modify document's appearance and functions, process multiple PDF documents with batch sequences, dynamically create new pages based on template, interface to a database. Acrobat JavaScript code can be executed:
- as an action to respond to user input in the document.
- as document-level script to respond to actions like printing.
- as part of a batch processing sequence that can be applied to multiple files. - Where can I read more about Acrobat JavaScript?
- Acrobat JavaScript enables you to do a wide variety of things within a PDF document. The Adobe Solutions Network (ASN) has an Adobe Acrobat JavaScript Training course that covers a wide variety of the possibilities available within Acrobat JavaScript.
Other Acrobat JavaScript documents available from Adobe: Acrobat JavaScript Scripting Guide - provides general introduction into JavaScript programming for Adobe Acrobat. Acrobat JavaScript Object Specification - contains detailed description of all Acrobat specific objects and classes. - How to create a batch sequence with JavaScript code?
- The batch processing tool in Adobe Acrobat Pro DC is called Action Wizard. Please see the following tutorial on step-by-step introduction to Action Wizard functionality. Add 'Execute JavaScript' tool to the 'action' in Action Wizard to run a custom JavaScript code.
Most sample scripts provided on this page are designed to be run inside Action Wizard's 'actions'. Most scripts have comments in the code that will help you to understand the logic and make any custom modifications. EverMap LLC. provides these scripts for reference only, without warranty of any kind (see disclaimer below). No technical support is provided.
DISCLAIMER: All javascripts on this web site are provided 'as is' without warranty of any kind either express or implied, including but not limited to the implied warranty of merchantability and fitness for a particular purpose. The entire risk arising out of the use or performance of these scripts and documentation remains with you.
DISCLAIMER: All javascripts on this web site are provided 'as is' without warranty of any kind either express or implied, including but not limited to the implied warranty of merchantability and fitness for a particular purpose. The entire risk arising out of the use or performance of these scripts and documentation remains with you.
/* Create bookmarks for each page in the document */
var root = this.bookmarkRoot;
try {
for (var i = 0; i < this.numPages; i++)
{
root.createChild('Page ' + (i+1), 'this.pageNum=' + i, i);
}
}
catch(e)
{
app.alert('Processing error: '+e)
}
var root = this.bookmarkRoot;
try {
for (var i = 0; i < this.numPages; i++)
{
root.createChild('Page ' + (i+1), 'this.pageNum=' + i, i);
}
}
catch(e)
{
app.alert('Processing error: '+e)
}
app.addMenuItem({ cName: '&Bookmark Every Page', cParent: 'Extensions',
cExec: 'BookmarkAllPages();',
cEnable: 'event.rc = (event.target != null);',
nPos: 0
});
function BookmarkAllPages()
{
var t = app.thermometer;
t.duration = this.numPages;
t.begin();
/* Create bookmarks for each page in the document */
var root = this.bookmarkRoot;
try {
for (var i = 0; i < this.numPages; i++)
{
t.value = i;
t.text = 'Processing page ' + (i + 1);
if (t.cancelled) break;
root.createChild('Page ' + (i+1), 'this.pageNum=' + i, i);
}
}
catch(e)
{
app.alert('Processing error: '+e)
}
t.end();
}
cExec: 'BookmarkAllPages();',
cEnable: 'event.rc = (event.target != null);',
nPos: 0
});
function BookmarkAllPages()
{
var t = app.thermometer;
t.duration = this.numPages;
t.begin();
/* Create bookmarks for each page in the document */
var root = this.bookmarkRoot;
try {
for (var i = 0; i < this.numPages; i++)
{
t.value = i;
t.text = 'Processing page ' + (i + 1);
if (t.cancelled) break;
root.createChild('Page ' + (i+1), 'this.pageNum=' + i, i);
}
}
catch(e)
{
app.alert('Processing error: '+e)
}
t.end();
}
app.addMenuItem({ cName: '&Delete All Bookmarks', cParent: 'Extensions',
cExec: 'this.bookmarkRoot.remove();',
cEnable: 'event.rc = (event.target != null);',
nPos: 0
});
cExec: 'this.bookmarkRoot.remove();',
cEnable: 'event.rc = (event.target != null);',
nPos: 0
});
/* Trim Bookmark Titles */
function TrimBookmarkTitle(NumChars, Bm, nLevel)
{
var nLevelMax = 2; // change maximum level of the processed bookmarks, starts from 0
if (nLevel > nLevelMax)
{
return;
}
var Title = new String(Bm.name);
Bm.name = Title.substr(0, NumChars); // set bookmark title to first NumChars characters
// Uncomment one of the following lines for a desired action
// Bm.name = Title.substr(-NumChars,NumChars); // set bookmak title to last NumChars
// Bm.name = Title.slice(NumChars,Title.length); // trims first NumChars from the bookmark
// Bm.name = Title.slice(0,-NumChars); // trims last NumChars from the bookmark
// process children
if (Bm.children != null)
{
for (var i = 0; i < Bm.children.length; i++)
{
TrimBookmarkTitle(NumChars, Bm.children[i], nLevel + 1);
}
}
}
var root = this.bookmarkRoot;
var NumCharsToTrimTo = 10; // trim bookmark title to the first 10 characters
TrimBookmarkTitle(NumCharsToTrimTo, root, 0);
function TrimBookmarkTitle(NumChars, Bm, nLevel)
{
var nLevelMax = 2; // change maximum level of the processed bookmarks, starts from 0
if (nLevel > nLevelMax)
{
return;
}
var Title = new String(Bm.name);
Bm.name = Title.substr(0, NumChars); // set bookmark title to first NumChars characters
// Uncomment one of the following lines for a desired action
// Bm.name = Title.substr(-NumChars,NumChars); // set bookmak title to last NumChars
// Bm.name = Title.slice(NumChars,Title.length); // trims first NumChars from the bookmark
// Bm.name = Title.slice(0,-NumChars); // trims last NumChars from the bookmark
// process children
if (Bm.children != null)
{
for (var i = 0; i < Bm.children.length; i++)
{
TrimBookmarkTitle(NumChars, Bm.children[i], nLevel + 1);
}
}
}
var root = this.bookmarkRoot;
var NumCharsToTrimTo = 10; // trim bookmark title to the first 10 characters
TrimBookmarkTitle(NumCharsToTrimTo, root, 0);
/* Merge first level bookmark titles with second level bookmarks, delete second level bookmark */
function MergeTitles(Bm)
{
if (Bm.children != null)
{
Bm.name = Bm.name + ' ' + Bm.children[0].name;
Bm.children[0].remove();
}
}
var root = this.bookmarkRoot;
if (root.children != null)
{
for (var i = 0; i < root.children.length; i++)
{
MergeTitles(root.children[i]);
}
}
function MergeTitles(Bm)
{
if (Bm.children != null)
{
Bm.name = Bm.name + ' ' + Bm.children[0].name;
Bm.children[0].remove();
}
}
var root = this.bookmarkRoot;
if (root.children != null)
{
for (var i = 0; i < root.children.length; i++)
{
MergeTitles(root.children[i]);
}
}
Acrobat Dc Javascript
/* Delete All Links and File Attachments */
this.syncAnnotScan();
var numpages = this.numPages;
for (var i=0; i < numpages; i++)
{
var annots = this.getAnnots( { nPage:i});
if (annots != null)
{
for (var j = annots.length - 1; j >= 0 ; j--)
{
if (annots[j].type 'Link' || annots[j].type 'FileAttachment')
{
annots[j].destroy();
}
}
}
}
this.syncAnnotScan();
var numpages = this.numPages;
for (var i=0; i < numpages; i++)
{
var annots = this.getAnnots( { nPage:i});
if (annots != null)
{
for (var j = annots.length - 1; j >= 0 ; j--)
{
if (annots[j].type 'Link' || annots[j].type 'FileAttachment')
{
annots[j].destroy();
}
}
}
}
/* Mark misspelled words with squiggle */
var chWord, numWords;
for (var i = 0; i < this.numPages; i++)
{
numWords = this.getPageNumWords(i);
for (var j = 0; j < numWords; j++) {
ckWord = spell.checkWord(this.getPageNthWord(i,j))
if (ckWord != null) {
this.addAnnot({
page: i,
type: 'Squiggly',
quads: this.getPageNthWordQuads(i,j),
author: 'A.C.Acrobat',
contents: ckWord.toString()
});
}
}
}
var chWord, numWords;
for (var i = 0; i < this.numPages; i++)
{
numWords = this.getPageNumWords(i);
for (var j = 0; j < numWords; j++) {
ckWord = spell.checkWord(this.getPageNthWord(i,j))
if (ckWord != null) {
this.addAnnot({
page: i,
type: 'Squiggly',
quads: this.getPageNthWordQuads(i,j),
author: 'A.C.Acrobat',
contents: ckWord.toString()
});
}
}
}
// Carbon Copy pages in the PDF file
// IMPORTANT: Set Output Options to the filename you need
// CHANGE THIS NUMBER: IT'S NUMBER OF ADDITIONAL COPIES
var nNumCopies = 2; // make 1 additional copy(es) of each page
try {
var newName = this.path;
var filename = newName.replace('.pdf','_Original.pdf'); // save a copy of original document
this.saveAs(filename);
var Num = this.numPages;
var nNumDups = nNumCopies+1;
for (var i = 0; i < Num; i++) {
for (var j = 0; j < nNumCopies; j++) {
k = i*nNumDups + j;
this.insertPages({ nPage: k, cPath:newName, nStart:i });
}
}
// set page labels: number each copy with a label
for (var k = 0; k < this.numPages; k+= nNumDups)
{
this.setPageLabels(k,[ 'D', 'Copy ', 1]);
}
}
catch(e)
{
app.alert(e);
}
// IMPORTANT: Set Output Options to the filename you need
// CHANGE THIS NUMBER: IT'S NUMBER OF ADDITIONAL COPIES
var nNumCopies = 2; // make 1 additional copy(es) of each page
try {
var newName = this.path;
var filename = newName.replace('.pdf','_Original.pdf'); // save a copy of original document
this.saveAs(filename);
var Num = this.numPages;
var nNumDups = nNumCopies+1;
for (var i = 0; i < Num; i++) {
for (var j = 0; j < nNumCopies; j++) {
k = i*nNumDups + j;
this.insertPages({ nPage: k, cPath:newName, nStart:i });
}
}
// set page labels: number each copy with a label
for (var k = 0; k < this.numPages; k+= nNumDups)
{
this.setPageLabels(k,[ 'D', 'Copy ', 1]);
}
}
catch(e)
{
app.alert(e);
}
// This script will scan all pages of the input document
// and extract valid email addresses into new PDF document
// Output PDF document will be placed in the same folder
// as input. The name of the output document will be:
// Original filename + '_Extracted_Emails'
// Visit www.evermap.com for more useful JavaScript samples.
var reEmail = /(([^<>()[].,;:s@']+(.[^<>()[].,;:s@']+)*)|('.+'))@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}])|(([a-zA-Z-0-9]+.)+[a-zA-Z]{2,}))/g;
var strExt = '_Extracted_Emails.pdf';
var strIntro = 'Email addresses extracted from document: ';
var strFinal = 'Total number of email addresses extracted: ' ;
ExtractFromDocument(reEmail,strExt,strIntro,strFinal);
function ExtractFromDocument(reMatch, strFileExt, strMessage1, strMessage2)
{
var chWord, numWords;
// construct filename for output document
var filename = this.path.replace(/.pdf$/, strFileExt);
// create a report document
try {
var ReportDoc = new Report();
var Out = new Object(); // array where we will collect all our emails before outputing them
ReportDoc.writeText(strMessage1 + this.path);
ReportDoc.divide(1); // draw a horizontal divider
ReportDoc.writeText(' '); // write a blank line to output
var nTotal = 0;
var nCounter = 0;
var nLinesPerPages = 60;
for (var i = 0; i < this.numPages; i++)
{
numWords = this.getPageNumWords(i);
var PageText = ';
for (var j = 0; j < numWords; j++) {
var word = this.getPageNthWord(i,j,false);
PageText += word;
}
var strMatches = PageText.match(reMatch);
if (strMatches null) continue;
// now output matches into report document
for (j = 0; j < strMatches.length; j++)
{
ReportDoc.writeText(strMatches[j]);
nTotal++;
nCounter++;
if (nCounter > nLinesPerPages)
{
ReportDoc.breakPage();
nCounter= 0;
}
}
}
ReportDoc.writeText(' '); // output extra blank line
ReportDoc.divide(1); // draw a horizontal divider
ReportDoc.writeText(strMessage2 + nTotal);
// save report to a document
ReportDoc.save(
{
cDIPath: filename
});
}
catch(e)
{
app.alert('Processing error: '+e)
}
} // end of the function
// and extract valid email addresses into new PDF document
// Output PDF document will be placed in the same folder
// as input. The name of the output document will be:
// Original filename + '_Extracted_Emails'
// Visit www.evermap.com for more useful JavaScript samples.
var reEmail = /(([^<>()[].,;:s@']+(.[^<>()[].,;:s@']+)*)|('.+'))@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}])|(([a-zA-Z-0-9]+.)+[a-zA-Z]{2,}))/g;
var strExt = '_Extracted_Emails.pdf';
var strIntro = 'Email addresses extracted from document: ';
var strFinal = 'Total number of email addresses extracted: ' ;
ExtractFromDocument(reEmail,strExt,strIntro,strFinal);
function ExtractFromDocument(reMatch, strFileExt, strMessage1, strMessage2)
{
var chWord, numWords;
// construct filename for output document
var filename = this.path.replace(/.pdf$/, strFileExt);
// create a report document
try {
var ReportDoc = new Report();
var Out = new Object(); // array where we will collect all our emails before outputing them
ReportDoc.writeText(strMessage1 + this.path);
ReportDoc.divide(1); // draw a horizontal divider
ReportDoc.writeText(' '); // write a blank line to output
var nTotal = 0;
var nCounter = 0;
var nLinesPerPages = 60;
for (var i = 0; i < this.numPages; i++)
{
numWords = this.getPageNumWords(i);
var PageText = ';
for (var j = 0; j < numWords; j++) {
var word = this.getPageNthWord(i,j,false);
PageText += word;
}
var strMatches = PageText.match(reMatch);
if (strMatches null) continue;
// now output matches into report document
for (j = 0; j < strMatches.length; j++)
{
ReportDoc.writeText(strMatches[j]);
nTotal++;
nCounter++;
if (nCounter > nLinesPerPages)
{
ReportDoc.breakPage();
nCounter= 0;
}
}
}
ReportDoc.writeText(' '); // output extra blank line
ReportDoc.divide(1); // draw a horizontal divider
ReportDoc.writeText(strMessage2 + nTotal);
// save report to a document
ReportDoc.save(
{
cDIPath: filename
});
}
catch(e)
{
app.alert('Processing error: '+e)
}
} // end of the function
/* Extract ISBN numbers From the Document */
// This script will scan all pages of the input document
// and extract valid ISBN numbers into new PDF document.
// Output PDF document will be placed in the same folder
// as input. The name of the output document will be:
// Original filename + '_Extracted_ISBN'
// Visit www.evermap.com for more useful JavaScript samples.
// This is a combination of strict and relaxed versions of ISBN number format
var reISBN=/(ISBN[:=s][s]*(?=[-0-9xX ]{13})(?:[0-9]+[- ]){3}[0-9]*[xX0-9])|(ISBN[:=s][ ]*d{9,10}[d|x])/g;
var strExt = '_Extracted_ISBN.pdf';
var strIntro = 'ISBN numbers extracted from document: ';
var strFinal = 'Total number of ISBN numbers extracted: ' ;
ExtractFromDocument(reISBN,strExt,strIntro,strFinal);
function ExtractFromDocument(reMatch, strFileExt, strMessage1, strMessage2)
{
var chWord, numWords;
// construct filename for output document
var filename = this.path.replace(/.pdf$/, strFileExt);
// create a report document
try {
var ReportDoc = new Report();
var Out = new Object(); // array where we will collect all our emails before outputing them
ReportDoc.writeText(strMessage1 + this.path);
ReportDoc.divide(1); // draw a horizontal divider
ReportDoc.writeText(' '); // write a blank line to output
for (var i = 0; i < this.numPages; i++)
{
numWords = this.getPageNumWords(i);
var PageText = ';
for (var j = 0; j < numWords; j++) {
var word = this.getPageNthWord(i,j,false);
PageText += word;
}
var strMatches = PageText.match(reMatch);
if (strMatches null) continue;
// now output matches into report document
for (j = 0; j < strMatches.length; j++) {
Out[strMatches[j]] = true; // store email as a property name
}
}
var nTotal = 0;
for (var prop in Out)
{
ReportDoc.writeText(prop);
nTotal++;
}
ReportDoc.writeText(' '); // output extra blank line
ReportDoc.divide(1); // draw a horizontal divider
ReportDoc.writeText(strMessage2 + nTotal);
// save report to a document
ReportDoc.save(
{
cDIPath: filename
});
}
catch(e)
{
app.alert('Processing error: '+e)
}
} // end of the function
// This script will scan all pages of the input document
// and extract valid ISBN numbers into new PDF document.
// Output PDF document will be placed in the same folder
// as input. The name of the output document will be:
// Original filename + '_Extracted_ISBN'
// Visit www.evermap.com for more useful JavaScript samples.
// This is a combination of strict and relaxed versions of ISBN number format
var reISBN=/(ISBN[:=s][s]*(?=[-0-9xX ]{13})(?:[0-9]+[- ]){3}[0-9]*[xX0-9])|(ISBN[:=s][ ]*d{9,10}[d|x])/g;
var strExt = '_Extracted_ISBN.pdf';
var strIntro = 'ISBN numbers extracted from document: ';
var strFinal = 'Total number of ISBN numbers extracted: ' ;
ExtractFromDocument(reISBN,strExt,strIntro,strFinal);
function ExtractFromDocument(reMatch, strFileExt, strMessage1, strMessage2)
{
var chWord, numWords;
// construct filename for output document
var filename = this.path.replace(/.pdf$/, strFileExt);
// create a report document
try {
var ReportDoc = new Report();
var Out = new Object(); // array where we will collect all our emails before outputing them
ReportDoc.writeText(strMessage1 + this.path);
ReportDoc.divide(1); // draw a horizontal divider
ReportDoc.writeText(' '); // write a blank line to output
for (var i = 0; i < this.numPages; i++)
{
numWords = this.getPageNumWords(i);
var PageText = ';
for (var j = 0; j < numWords; j++) {
var word = this.getPageNthWord(i,j,false);
PageText += word;
}
var strMatches = PageText.match(reMatch);
if (strMatches null) continue;
// now output matches into report document
for (j = 0; j < strMatches.length; j++) {
Out[strMatches[j]] = true; // store email as a property name
}
}
var nTotal = 0;
for (var prop in Out)
{
ReportDoc.writeText(prop);
nTotal++;
}
ReportDoc.writeText(' '); // output extra blank line
ReportDoc.divide(1); // draw a horizontal divider
ReportDoc.writeText(strMessage2 + nTotal);
// save report to a document
ReportDoc.save(
{
cDIPath: filename
});
}
catch(e)
{
app.alert('Processing error: '+e)
}
} // end of the function
/* Extract US Social Security Numbers From the Document */
// This script will scan all pages of the input document
// and extract :
// Social security numbers:
// Output PDF document will be placed in the same folder
// as input. The name of the output document will be:
// Original filename + '_Extracted_SSNs'
// Visit www.evermap.com for more useful JavaScript samples.
var reMatch=/(?!000)([0-6]d{2}|7([0-6]d|7[012]))([ -])(?!00)dd3(?!0000)d{4}/g;
var strExt = '_Extracted_SSNs.pdf';
var strIntro = 'Social Security Numbers extracted from document: ';
var strFinal = 'Total number of SSNs extracted: ' ;
ExtractFromDocument(reMatch,strExt,strIntro,strFinal);
function ExtractFromDocument(reMatch, strFileExt, strMessage1, strMessage2)
{
var chWord, numWords;
// construct filename for output document
var filename = this.path.replace(/.pdf$/, strFileExt);
// create a report document
try {
var ReportDoc = new Report();
var Out = new Object(); // array where we will collect all our emails before outputing them
ReportDoc.writeText(strMessage1 + this.path);
ReportDoc.divide(1); // draw a horizontal divider
ReportDoc.writeText(' '); // write a blank line to output
for (var i = 0; i < this.numPages; i++)
{
numWords = this.getPageNumWords(i);
var PageText = ';
for (var j = 0; j < numWords; j++) {
var word = this.getPageNthWord(i,j,false);
PageText += word;
}
var strMatches = PageText.match(reMatch);
if (strMatches null) continue;
// now output matches into report document
for (j = 0; j < strMatches.length; j++) {
Out[strMatches[j]] = true; // store email as a property name
}
}
var nTotal = 0;
for (var prop in Out)
{
ReportDoc.writeText(prop);
nTotal++;
}
ReportDoc.writeText(' '); // output extra blank line
ReportDoc.divide(1); // draw a horizontal divider
ReportDoc.writeText(strMessage2 + nTotal);
// save report to a document
ReportDoc.save(
{
cDIPath: filename
});
}
catch(e)
{
app.alert('Processing error: '+e)
}
} // end of the function
// This script will scan all pages of the input document
// and extract :
// Social security numbers:
// Output PDF document will be placed in the same folder
// as input. The name of the output document will be:
// Original filename + '_Extracted_SSNs'
// Visit www.evermap.com for more useful JavaScript samples.
var reMatch=/(?!000)([0-6]d{2}|7([0-6]d|7[012]))([ -])(?!00)dd3(?!0000)d{4}/g;
var strExt = '_Extracted_SSNs.pdf';
var strIntro = 'Social Security Numbers extracted from document: ';
var strFinal = 'Total number of SSNs extracted: ' ;
ExtractFromDocument(reMatch,strExt,strIntro,strFinal);
function ExtractFromDocument(reMatch, strFileExt, strMessage1, strMessage2)
{
var chWord, numWords;
// construct filename for output document
var filename = this.path.replace(/.pdf$/, strFileExt);
// create a report document
try {
var ReportDoc = new Report();
var Out = new Object(); // array where we will collect all our emails before outputing them
ReportDoc.writeText(strMessage1 + this.path);
ReportDoc.divide(1); // draw a horizontal divider
ReportDoc.writeText(' '); // write a blank line to output
for (var i = 0; i < this.numPages; i++)
{
numWords = this.getPageNumWords(i);
var PageText = ';
for (var j = 0; j < numWords; j++) {
var word = this.getPageNthWord(i,j,false);
PageText += word;
}
var strMatches = PageText.match(reMatch);
if (strMatches null) continue;
// now output matches into report document
for (j = 0; j < strMatches.length; j++) {
Out[strMatches[j]] = true; // store email as a property name
}
}
var nTotal = 0;
for (var prop in Out)
{
ReportDoc.writeText(prop);
nTotal++;
}
ReportDoc.writeText(' '); // output extra blank line
ReportDoc.divide(1); // draw a horizontal divider
ReportDoc.writeText(strMessage2 + nTotal);
// save report to a document
ReportDoc.save(
{
cDIPath: filename
});
}
catch(e)
{
app.alert('Processing error: '+e)
}
} // end of the function
/* Extract URLs (Web Addresses) From the Document */
// This script will scan all pages of the input document
// and extract :
// Valid URL s -- allows http, https, ftp, ftps and simple www.mydomain..
// Output PDF document will be placed in the same folder
// as input. The name of the output document will be:
// Original filename + '_Extracted_URLs'
// Visit www.evermap.com for more useful JavaScript samples.
var reMatch=/((((ht|f)tp(s?))://)|(www.))([0-9a-zA-Z-]+.)+[a-zA-Z]{2,6}(:[0-9]+)?(/S*)?/g;
//var reMatch=/(((ht|f)tp(s?))://).*/g;
var strExt = '_Extracted_URLs.pdf';
var strIntro = 'URLs (http,https,ftp,ftps) extracted from document: ';
var strFinal = 'Total number of URLs (web addresses) extracted: ' ;
ExtractFromDocument(reMatch,strExt,strIntro,strFinal);
function ExtractFromDocument(reMatch, strFileExt, strMessage1, strMessage2)
{
var chWord, numWords;
// construct filename for output document
var filename = this.path.replace(/.pdf$/, strFileExt);
// create a report document
try {
var ReportDoc = new Report();
var Out = new Object(); // array where we will collect all our emails before outputing them
ReportDoc.writeText(strMessage1 + this.path);
ReportDoc.divide(1); // draw a horizontal divider
ReportDoc.writeText(' '); // write a blank line to output
for (var i = 0; i < this.numPages; i++)
{
numWords = this.getPageNumWords(i);
var PageText = ';
for (var j = 0; j < numWords; j++) {
var word = this.getPageNthWord(i,j,false);
PageText += word;
}
var strMatches = PageText.match(reMatch);
if (strMatches null) continue;
// now output matches into report document
for (j = 0; j < strMatches.length; j++) {
Out[strMatches[j]] = true; // store email as a property name
}
}
var nTotal = 0;
for (var prop in Out)
{
ReportDoc.writeText(prop);
nTotal++;
}
ReportDoc.writeText(' '); // output extra blank line
ReportDoc.divide(1); // draw a horizontal divider
ReportDoc.writeText(strMessage2 + nTotal);
// save report to a document
ReportDoc.save(
{
cDIPath: filename
});
}
catch(e)
{
app.alert('Processing error: '+e)
}
} // end of the function
// This script will scan all pages of the input document
// and extract :
// Valid URL s -- allows http, https, ftp, ftps and simple www.mydomain..
// Output PDF document will be placed in the same folder
// as input. The name of the output document will be:
// Original filename + '_Extracted_URLs'
// Visit www.evermap.com for more useful JavaScript samples.
var reMatch=/((((ht|f)tp(s?))://)|(www.))([0-9a-zA-Z-]+.)+[a-zA-Z]{2,6}(:[0-9]+)?(/S*)?/g;
//var reMatch=/(((ht|f)tp(s?))://).*/g;
var strExt = '_Extracted_URLs.pdf';
var strIntro = 'URLs (http,https,ftp,ftps) extracted from document: ';
var strFinal = 'Total number of URLs (web addresses) extracted: ' ;
ExtractFromDocument(reMatch,strExt,strIntro,strFinal);
function ExtractFromDocument(reMatch, strFileExt, strMessage1, strMessage2)
{
var chWord, numWords;
// construct filename for output document
var filename = this.path.replace(/.pdf$/, strFileExt);
// create a report document
try {
var ReportDoc = new Report();
var Out = new Object(); // array where we will collect all our emails before outputing them
ReportDoc.writeText(strMessage1 + this.path);
ReportDoc.divide(1); // draw a horizontal divider
ReportDoc.writeText(' '); // write a blank line to output
for (var i = 0; i < this.numPages; i++)
{
numWords = this.getPageNumWords(i);
var PageText = ';
for (var j = 0; j < numWords; j++) {
var word = this.getPageNthWord(i,j,false);
PageText += word;
}
var strMatches = PageText.match(reMatch);
if (strMatches null) continue;
// now output matches into report document
for (j = 0; j < strMatches.length; j++) {
Out[strMatches[j]] = true; // store email as a property name
}
}
var nTotal = 0;
for (var prop in Out)
{
ReportDoc.writeText(prop);
nTotal++;
}
ReportDoc.writeText(' '); // output extra blank line
ReportDoc.divide(1); // draw a horizontal divider
ReportDoc.writeText(strMessage2 + nTotal);
// save report to a document
ReportDoc.save(
{
cDIPath: filename
});
}
catch(e)
{
app.alert('Processing error: '+e)
}
} // end of the function
/* Rotate all pages (or page range) in the document */
// Modify nStart, nEnd and nRotate to change script logic
nStart = 0 // first page to rotate
nEnd = this.numPages - 1; // last page to rotate
nRotate = 90 // allowed rotations: 0, 90, 180, 270
try {
if (this.numPages > 0) {
this.setPageRotations(nStart,nEnd,nRotate)
}
}
catch(e)
{
app.alert('Processing error: '+e)
}
// Modify nStart, nEnd and nRotate to change script logic
nStart = 0 // first page to rotate
nEnd = this.numPages - 1; // last page to rotate
nRotate = 90 // allowed rotations: 0, 90, 180, 270
try {
if (this.numPages > 0) {
this.setPageRotations(nStart,nEnd,nRotate)
}
}
catch(e)
{
app.alert('Processing error: '+e)
}
/* Rotate only landscape or portrait pages in the document */
// Modify nStart, nEnd and nRotate to change script logic
nStart = 0; // first page to rotate
nEnd = this.numPages - 1; // last page to rotate
nRotate = 0; // allowed rotations: 0, 90, 180, 270
bLanscape = true; // set to false to process only Portrait pages
try {
for (var i = nStart; i <= nEnd; i++)
{
// check if this page is landscape or portrait
var aRect = this.getPageBox('Media',i);
var Width = aRect[2] - aRect[0];
var Height = aRect[1] - aRect[3];
if (Height > Width) { // portrait
if (!bLandscape) {
this.setPageRotations(i,i,nRotate)
}
}
else { // landscape
if (bLandscape) {
this.setPageRotations(i,i,nRotate)
}
}
}
}
catch(e)
{
app.alert('Processing error: '+e)
}
// Modify nStart, nEnd and nRotate to change script logic
nStart = 0; // first page to rotate
nEnd = this.numPages - 1; // last page to rotate
nRotate = 0; // allowed rotations: 0, 90, 180, 270
bLanscape = true; // set to false to process only Portrait pages
try {
for (var i = nStart; i <= nEnd; i++)
{
// check if this page is landscape or portrait
var aRect = this.getPageBox('Media',i);
var Width = aRect[2] - aRect[0];
var Height = aRect[1] - aRect[3];
if (Height > Width) { // portrait
if (!bLandscape) {
this.setPageRotations(i,i,nRotate)
}
}
else { // landscape
if (bLandscape) {
this.setPageRotations(i,i,nRotate)
}
}
}
}
catch(e)
{
app.alert('Processing error: '+e)
}
// Acrobat JavaScript Code - www.evermap.com
// DELETE PDF PAGES WITHOUT TEXT
// IMPORTANT: This script assumes that page is blank if it does not contain any 'pdf words'
// OUTPUT: An output PDF file is created by appending _Original.pdf to the filename
try {
var newName = this.path;
var filename = newName.replace('.pdf','_Original.pdf');
this.saveAs(filename);
for (var i = 0; i < this.numPages; i++)
{
numWords = this.getPageNumWords(i);
if (numWords 0)
{
// this page has no text, delete it
this.deletePages(i,i);
}
}
}
catch(e)
{
app.alert(e);
}
// DELETE PDF PAGES WITHOUT TEXT
// IMPORTANT: This script assumes that page is blank if it does not contain any 'pdf words'
// OUTPUT: An output PDF file is created by appending _Original.pdf to the filename
try {
var newName = this.path;
var filename = newName.replace('.pdf','_Original.pdf');
this.saveAs(filename);
for (var i = 0; i < this.numPages; i++)
{
numWords = this.getPageNumWords(i);
if (numWords 0)
{
// this page has no text, delete it
this.deletePages(i,i);
}
}
}
catch(e)
{
app.alert(e);
}
Javascript In Adobe Acrobat Forms
// Add navigation buttons to the page
// This script puts 3 buttons on top of every page (except the first one that has one button)
// First button '<' : takes to the previous page
// Second button: '1' : takes to the first page of the document
// Third button: '>' : takes to the next page in the document (does not exists on the last page)
var inch = 72;
try
{
nLastPage = this.numPages - 1;
for (var p = 0; p < this.numPages; p++)
{
var x = 0.5;
if (p > 0)
{
AddButton(p,x,0.5,0.25,0.25,'PrevPage','<','Previous Page','this.pageNum--;'); // left arrow, previous page
x += 0.3;
}
if (p != 0)
{
AddButton(p,x,0.5,0.25,0.25,'StartPage','1','Go To First Page','this.pageNum=0;'); // '1', takes to the first page
x += 0.3;
}
if (p < nLastPage)
{
AddButton(p,x,0.5,0.25,0.25,'NextPage','>','Next Page','this.pageNum++;'); // right arrow, next page
x += 0.3;
}
AddButton(p,x,0.5,0.25,0.25,'Back','<<','Go Back','app.execMenuItem('GoBack');'); // right arrow, next page
x += 0.3;
}
}
catch (e)
{
app.alert(e);
}
// AddButton function creates a button with given parameters and action
function AddButton(nPageNum, x, y, width, height, strText, strCaption, strToolTip, strAction)
{
var aRect = this.getPageBox( { nPage: nPageNum} );
aRect[0] += x * inch;
aRect[1] -= y * inch;
aRect[2] = aRect[0] + width * inch;
aRect[3] = aRect[1] - height * inch;
var f = this.addField(strText,'button', nPageNum, aRect);
f.setAction('MouseUp',strAction);
f.userName = strToolTip;
f.delay = true;
f.borderStyle = border.s;
f.highlight = 'push';
f.textSize = 0; // autosized
f.textColor = color.blue;
f.strokeColor = color.blue;
f.fillColor = color.white;
// you can specify a different font here, otherwise it uses a default one
//f.textFont = font.ZapfD;
f.buttonSetCaption(strCaption);
f.delay = false;
}
// This script puts 3 buttons on top of every page (except the first one that has one button)
// First button '<' : takes to the previous page
// Second button: '1' : takes to the first page of the document
// Third button: '>' : takes to the next page in the document (does not exists on the last page)
var inch = 72;
try
{
nLastPage = this.numPages - 1;
for (var p = 0; p < this.numPages; p++)
{
var x = 0.5;
if (p > 0)
{
AddButton(p,x,0.5,0.25,0.25,'PrevPage','<','Previous Page','this.pageNum--;'); // left arrow, previous page
x += 0.3;
}
if (p != 0)
{
AddButton(p,x,0.5,0.25,0.25,'StartPage','1','Go To First Page','this.pageNum=0;'); // '1', takes to the first page
x += 0.3;
}
if (p < nLastPage)
{
AddButton(p,x,0.5,0.25,0.25,'NextPage','>','Next Page','this.pageNum++;'); // right arrow, next page
x += 0.3;
}
AddButton(p,x,0.5,0.25,0.25,'Back','<<','Go Back','app.execMenuItem('GoBack');'); // right arrow, next page
x += 0.3;
}
}
catch (e)
{
app.alert(e);
}
// AddButton function creates a button with given parameters and action
function AddButton(nPageNum, x, y, width, height, strText, strCaption, strToolTip, strAction)
{
var aRect = this.getPageBox( { nPage: nPageNum} );
aRect[0] += x * inch;
aRect[1] -= y * inch;
aRect[2] = aRect[0] + width * inch;
aRect[3] = aRect[1] - height * inch;
var f = this.addField(strText,'button', nPageNum, aRect);
f.setAction('MouseUp',strAction);
f.userName = strToolTip;
f.delay = true;
f.borderStyle = border.s;
f.highlight = 'push';
f.textSize = 0; // autosized
f.textColor = color.blue;
f.strokeColor = color.blue;
f.fillColor = color.white;
// you can specify a different font here, otherwise it uses a default one
//f.textFont = font.ZapfD;
f.buttonSetCaption(strCaption);
f.delay = false;
}
/* Rename Files Using Bookmarks */
// This is a script that can rename files based on bookmarks.
// If document has 3 bookmarks the output filename is:
// First bookmark From Second Bookmark – Third Bookmark
// If there are more than 3 bookmarks, then output filename is:
// First bookmark From Second Bookmark To Third Bookmark – Forth Bookmark
var fname = ';
var bm = this.bookmarkRoot;
// build a name
if (bm.children != null)
{
var numBookmarks = Math.min(4,bm.children.length);
for (var i = 0; i < numBookmarks; i++)
{
switch (i)
{
case 1:
fname += ' From ';
break;
case 2:
if (numBookmarks > 3)
{
fname += ' To ';
}
else
{
fname += ' - ';
}
break;
case 3:
fname += ' - ';
break;
}
fname += bm.children[i].name;
}
}
// make sure no illegal characters are included in the filename
var outputname = fname.replace(/[?:/|<>'*]/g,'_');
// console.println(outputname);
// save document into c:/data/ folder
// IMPORTANT: replace with a desired output folder location
this.saveAs('/c/data/' + outputname + '.pdf');
// This is a script that can rename files based on bookmarks.
// If document has 3 bookmarks the output filename is:
// First bookmark From Second Bookmark – Third Bookmark
// If there are more than 3 bookmarks, then output filename is:
// First bookmark From Second Bookmark To Third Bookmark – Forth Bookmark
var fname = ';
var bm = this.bookmarkRoot;
// build a name
if (bm.children != null)
{
var numBookmarks = Math.min(4,bm.children.length);
for (var i = 0; i < numBookmarks; i++)
{
switch (i)
{
case 1:
fname += ' From ';
break;
case 2:
if (numBookmarks > 3)
{
fname += ' To ';
}
else
{
fname += ' - ';
}
break;
case 3:
fname += ' - ';
break;
}
fname += bm.children[i].name;
}
}
// make sure no illegal characters are included in the filename
var outputname = fname.replace(/[?:/|<>'*]/g,'_');
// console.println(outputname);
// save document into c:/data/ folder
// IMPORTANT: replace with a desired output folder location
this.saveAs('/c/data/' + outputname + '.pdf');
/* Save files with a timestamp filename */
/* this script does not overwrite existing files - unique names are created */
/* EverMap LLC, 2009 */
function DoesFileExists(pathname)
{
var result = false;
try
{
var otherDoc = app.openDoc(pathname);
if (otherDoc != null)
{
result = true;
otherDoc.closeDoc();
}
}
catch(e)
{
result = false;
}
return result;
}
// create a unique filename for output file
function GetUniqueOutputFileName(pathName)
{
var i = 1;
var baseName = pathName.slice(0, pathName.length - 4);
var testName = pathName;
while (DoesFileExists(testName) true)
{
testName = baseName + ' ' + i + '.pdf';
i++;
}
return testName;
}
var i = this.path.search(/[^:/]+.pdf$/);
var fname = this.path.slice(i, this.path.length - 4);
// replace /c/data/ with a desired folder path where to store extracted files
var folder = '/c/data2/';
// now add time stamp data to the filename
var t = new Date();
fname += ' - ' + t.toLocaleString();
var outputname = fname.replace(/[,/?*<>]/g,' ');
outputname = outputname.replace(/[:]/g,'-');
var outputpath = folder + outputname + '.pdf';
this.saveAs(GetUniqueOutputFileName(outputpath));
/* this script does not overwrite existing files - unique names are created */
/* EverMap LLC, 2009 */
function DoesFileExists(pathname)
{
var result = false;
try
{
var otherDoc = app.openDoc(pathname);
if (otherDoc != null)
{
result = true;
otherDoc.closeDoc();
}
}
catch(e)
{
result = false;
}
return result;
}
// create a unique filename for output file
function GetUniqueOutputFileName(pathName)
{
var i = 1;
var baseName = pathName.slice(0, pathName.length - 4);
var testName = pathName;
while (DoesFileExists(testName) true)
{
testName = baseName + ' ' + i + '.pdf';
i++;
}
return testName;
}
var i = this.path.search(/[^:/]+.pdf$/);
var fname = this.path.slice(i, this.path.length - 4);
// replace /c/data/ with a desired folder path where to store extracted files
var folder = '/c/data2/';
// now add time stamp data to the filename
var t = new Date();
fname += ' - ' + t.toLocaleString();
var outputname = fname.replace(/[,/?*<>]/g,' ');
outputname = outputname.replace(/[:]/g,'-');
var outputpath = folder + outputname + '.pdf';
this.saveAs(GetUniqueOutputFileName(outputpath));
Javascript In Adobe Acrobat
/* Create a bookmark report */
function PrintBookmark(Report, Bm, nLevel)
{
// write a name of the bookmark
var inch = 72;
Report.writeText(Bm.name);
// process children
if (Bm.children != null)
{
Report.indent(inch/2);
for (var i = 0; i < Bm.children.length; i++)
{
PrintBookmark(Report, Bm.children[i], nLevel + 1);
}
Report.outdent(inch/2);
}
}
var root = this.bookmarkRoot;
var Report = new Report();
PrintBookmark(Report, root, 0);
// Change output path for the report
Report.save('/c/myreport.pdf');
function PrintBookmark(Report, Bm, nLevel)
{
// write a name of the bookmark
var inch = 72;
Report.writeText(Bm.name);
// process children
if (Bm.children != null)
{
Report.indent(inch/2);
for (var i = 0; i < Bm.children.length; i++)
{
PrintBookmark(Report, Bm.children[i], nLevel + 1);
}
Report.outdent(inch/2);
}
}
var root = this.bookmarkRoot;
var Report = new Report();
PrintBookmark(Report, root, 0);
// Change output path for the report
Report.save('/c/myreport.pdf');
Using Javascript In Adobe Acrobat Reader
// This script converts all StrikeOut (cross-out) annotations into Highlights
// The similar approach can be used to convert between all 3 text markup
// annotations types: Highlights, Underline and CrossOut
try
{
this.syncAnnotScan();
for (var nPage = 0; nPage < this.numPages; nPage++)
{
// get all annotations on the page
var Annots = this.getAnnots({
nPage:nPage
});
// process each annotation
if (Annots != null)
{
for (var i = 0; i < Annots.length; i++)
{
if (Annots[i].type 'StrikeOut')
{
Annots[i].type = 'Highlight';
}
}
}
}
}
catch(e)
{
app.alert(e);
}
// The similar approach can be used to convert between all 3 text markup
// annotations types: Highlights, Underline and CrossOut
try
{
this.syncAnnotScan();
for (var nPage = 0; nPage < this.numPages; nPage++)
{
// get all annotations on the page
var Annots = this.getAnnots({
nPage:nPage
});
// process each annotation
if (Annots != null)
{
for (var i = 0; i < Annots.length; i++)
{
if (Annots[i].type 'StrikeOut')
{
Annots[i].type = 'Highlight';
}
}
}
}
}
catch(e)
{
app.alert(e);
}