/*********************************************************************************************
Muhimbi PDF Converter - JavaScript Watermarking
Copyright 2010, Muhimbi Ltd - www.muhimbi.com - All rights reserved
The following code shows a simple way of adding JavaScript to existing PDF Files. It adds
the current date to each page in the document in order to simulate a 'print date' that is
always up to date without the need to modify the PDF file. The code is automatically executed
when the document is opened in the Adobe Acrobat Viewer.
Error and permission checking as well as other minor features have been omitted for the sake
of brevity and clarity.
Ideally PDF Conversion, applying security and watermarking is executed in the same step, see
http://www.muhimbi.com/blog/2010/01/configure-pdf-security-from-sharepoint.html
This code requires Muhimbi’s PDF Converter and Workflow Power Pack to be installed.
*********************************************************************************************/
using System.Drawing;
using System.IO;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Interactive;
using Muhimbi.SharePoint.DocumentConverter.PDF;
SPFile spSourceDocument = MyWorkflow.Item.File;
string destinationFileName = spSourceDocument.Name;
string destinationFolderName = MyWorkflow.Parameter1 as string;
// ** Load the document
PdfLoadedDocument sourceDocument = new PdfLoadedDocument(spSourceDocument.OpenBinary());
PdfDocument destinationDocument = new PdfDocument();
// ** Copy all pages from the source document into the destination document
// ** so we can add JavaScript actions.
destinationDocument.ImportPageRange(sourceDocument, 0, sourceDocument.Pages.Count - 1);
sourceDocument.Dispose();
// ** Iterate over all pages and add a form element
for (int i = 0; i < destinationDocument.Pages.Count; i++)
{
PdfPage destinationPage = destinationDocument.Pages[i];
// ** Create a new field using a unique name
PdfTextBoxField field = new PdfTextBoxField(destinationPage, "_M_PrintDateField_" + i);
// ** Center the field
const int BOX_WIDTH = 200;
int boxLeft = (int)((destinationPage.Size.Width - BOX_WIDTH) / 2);
field.Bounds = new RectangleF(boxLeft, 20, BOX_WIDTH, 20);
// ** Format the field
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12f);
field.Font = font;
field.BorderColor = new PdfColor(Color.White);
field.BackColor = new PdfColor(Color.White);
field.ReadOnly = true;
field.TextAlignment = PdfTextAlignment.Center;
destinationDocument.Form.Fields.Add(field);
}
// ** Create a client side script that iterates over all fields and populates the date
string jscript = @"
var pages = " + destinationDocument.Pages.Count + @";
var today = util.printd('dd-mm-yyyy', new Date());
for(var i=0; i<pages; i++)
{
var field = this.getField('_M_PrintDateField_' + i);
field.value = 'Today is: ' + today;
}
";
// ** Attach the script to the Document Open event.
PdfJavaScriptAction jsAction = new PdfJavaScriptAction(jscript);
destinationDocument.Actions.AfterOpen = jsAction;
// ** Construct the path and file to write the watermarked PDF file to.
if (string.IsNullOrEmpty(destinationFolderName) == true)
destinationFolderName = spSourceDocument.ParentFolder.Url;
SPFolder destinationFolder = Utility.GetSPFolder(destinationFolderName, MyWorkflow.Web);
string destinationFilePath = string.Format("{0}/{1}", destinationFolder.Url,
destinationFileName);
SPWeb destinationWeb = destinationFolder.ParentWeb;
SPFile spDestinationFile = destinationWeb.GetFile(destinationFilePath);
// ** If a document library requires manual checkout and the file is not checked out, then
// ** check the file out before uploading.
if (spDestinationFile.Exists && spDestinationFile.Item.ParentList.ForceCheckout &&
spDestinationFile.CheckOutStatus == SPFile.SPCheckOutStatus.None)
{
spDestinationFile.CheckOut();
}
// ** Add the file to the site including the meta data
using (MemoryStream watermarkedFile = new MemoryStream())
{
destinationDocument.Save(watermarkedFile);
spDestinationFile = destinationWeb.Files.Add(destinationFilePath, watermarkedFile,
spSourceDocument.Item.Properties, true);
}
// ** Check the file back in if this script was responsible for checking it out.
if (spDestinationFile.Item.ParentList.ForceCheckout == true)
{
spDestinationFile.CheckIn("Auto check-in after PDF watermarking.");
}