JustPaste.it

// ------------Read before any edits-----------
//
// Make sure to save the script encoded as ANSI
// since Adobe Acrobat cant handle UTF encoded
// characters like ae, oe, ue and sz

var main = app.trustedFunction(function () {
app.beginPriv();
var iconPath = "C:\Program Files (x86)\Adobe\Acrobat DC\Acrobat\Javascripts\bcs.jpg";

var myDoc = app.newDoc();
myDoc.importIcon("bcsIcon", iconPath, 0);
var oIcon = util.iconStreamFromIcon(myDoc.getIcon("bcsIcon"));
myDoc.closeDoc(true);

app.addToolButton({
cName: "BCSadd",
oIcon: oIcon,
cLabel: "Finish PR",
cEnable: "event.rc = (app.doc != null);",
cExec: "BCSadd();",
nPos: 0
});

app.endPriv();
});

var BCSadd = app.trustedFunction(function () {
app.beginPriv();

// Create desktop path
var pathDesk = app.getPath("user", "documents");
pathDesk = pathDesk.substring(0, pathDesk.length - 9);
pathDesk += "Desktop/";

// Get javascript path for temporary storage
var pathJS = app.getPath("app", "javascript") + "/";

// Copy attached invoice to javascript path
var invoiceAttachment = this.openDataObject(this.dataObjects[0].name);
invoiceAttachment.saveAs(pathJS + "Invoice.pdf");

// Open saved Invoice
var invoice = app.openDoc(pathJS + "Invoice.pdf");
invoice.insertPages({ nPage: -1, cPath: pathDesk + "BCS.pdf" });

// Get the amount of words in the submitter name to calculate the position of the vendor name
var submitterNameBegin = 10;
var submitterNameMax = 10;
var submitterWordCount = 0;
for (var i = submitterNameBegin; i < submitterNameBegin + submitterNameMax; i++) {
if (!(parseInt(invoice.getPageNthWord(0, i)) > 0)) {
submitterWordCount++
} else {
break;
}
};

if (submitterWordCount >= submitterNameMax) {
app.alert("Couldn't get word count of submitter!\n Using 1 instead");
submitterWordCount = 1;
}

// Get vendor name
var vendorName = "";
var wordCountNormal = 91;
var vendorNormNameOffset = 15;
var vendorNameOffset = vendorNormNameOffset + submitterWordCount;
var vendorWordCount = invoice.getPageNumWords(0) - wordCountNormal + vendorNameOffset;

for (var i = vendorNameOffset; i < vendorWordCount; i++) {
vendorName += invoice.getPageNthWord(0, i) + " ";
};

// Get document code
var docCode = invoice.getPageNthWord(0, 3);

/* Removed due to deprecation in Adobe Acrobat DC
Possible fix using invoice.xaf.timeStamp

if (invoice.modTime.getTime() < new Date().getTime() - 120000) {
var nButton = app.alert({
cTitle: "",
cMessage: "",
cIcon: 1,
cType: 2
});

if (nButton == 3) {
invoice.closeDoc(true);
return;
}
}*/

// Send mail including modified invoice
invoice.mailDoc({ cTo: "payment.request@merck.com", cSubject: vendorName + " | " + docCode });

// Save pdf before closing to prevent save-dialogue
invoice.saveAs(pathJS + "Invoice.pdf");

// Since we cant delete the file we will just overwrite it everytime
invoice.closeDoc(true);

// Insert docCode into the comment field of parent pdf
this.xfa.form.form1.Header.Kommentar.rawValue = docCode;

// Press "PBR" button
this.xfa.form.form1.Header.Kommentar.Schaltfläche1.execEvent("click");

// Close the document
this.closeDoc(true);

app.endPriv();
});

main();