/********************************************************************************************* Muhimbi PDF Converter - Dynamic Watermarking Copyright 2010, Muhimbi Ltd - www.muhimbi.com - All rights reserved The following code shows a simple way of adding a dynamic watermark to existing PDF Files. The watermark content is read from a SharePoint column and rendered on the center of the page. 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 Muhimbi.SharePoint.DocumentConverter.PDF; using Syncfusion.Pdf.Security; SPFile spSourceDocument = MyWorkflow.Item.File; string destinationFileName = spSourceDocument.Name; string destinationFolderName = MyWorkflow.Parameter1 as string; // ** Read the text from the Column named 'Watermark', use a column name of your choice. string watermark = MyWorkflow.Item["Watermark"] as string; string pdfOwnerPassword = "some!Strong^Password"; // ** z-order and transparency of the watermark bool watermarkInBackground = true; float watermarkTransparancy = 0.25f; // ** Method for creating a simple template, amend as needed WorkflowFunction CreateWatermark = delegate(string[] parameters) { const float watermarkWidth = 250; const float watermarkHeight = 23; string watermarkText = parameters[0]; if(watermarkText==null) watermarkText = "no watermark specified"; // ** Create a new Template PdfTemplate template = new PdfTemplate(watermarkWidth, watermarkHeight); PdfGraphics tg = template.Graphics; // ** Rectangle PdfPen pen = new PdfPen(PdfBrushes.Black, 1); tg.DrawRectangle(pen, PdfBrushes.White, new RectangleF(0, 0, watermarkWidth, watermarkHeight)); // ** Evaluation message text using (Font f = new Font("Arial", 10f, FontStyle.Regular)) { PdfFont font = new PdfTrueTypeFont(f, false); PdfStringFormat stringFormat = new PdfStringFormat(); stringFormat.Alignment = PdfTextAlignment.Center; stringFormat.LineAlignment = PdfVerticalAlignment.Middle; stringFormat.WordWrap = PdfWordWrapType.WordOnly; stringFormat.ClipPath = true; tg.DrawString(watermarkText, font, PdfBrushes.Black, new RectangleF(5, 0, watermarkWidth - 10, watermarkHeight), stringFormat); } return template; }; // ** Method for applying security settings, amend / uncomment as needed WorkflowMethod ApplySecurity = delegate(PdfLoadedDocument[] pdfDocuments) { PdfLoadedDocument pdfDocument = pdfDocuments[0]; // ** Enable encryption of content pdfDocument.Security.KeySize = PdfEncryptionKeySize.Key128Bit; // ** Specify the password used for modifying permissions pdfDocument.Security.OwnerPassword = pdfOwnerPassword; // ** Apply the restrictions, if any. Note that by default nothing is // ** allowed and we need to individually enable permissions pdfDocument.Security.Permissions = PdfPermissionsFlags.Default; // ** Allow Commenting / annotations pdfDocument.Security.SetPermissions(PdfPermissionsFlags.EditAnnotations); // ** Allow Accessibility //pdfDocument.Security.SetPermissions(PdfPermissionsFlags.AccessibilityCopyContent); // ** Allow Copy content via clipboard etc. //pdfDocument.Security.SetPermissions(PdfPermissionsFlags.CopyContent); // ** Allow Assemble / modification of document //pdfDocument.Security.SetPermissions(PdfPermissionsFlags.AssembleDocument); // ** Allow Form fields to be filled out pdfDocument.Security.SetPermissions(PdfPermissionsFlags.FillFields); // ** Allow high resolution printing pdfDocument.Security.SetPermissions(PdfPermissionsFlags.FullQualityPrint); // ** Allow any kind of printing pdfDocument.Security.SetPermissions(PdfPermissionsFlags.Print); }; // ** Load the document from SharePoint PdfLoadedDocument sourceDocument = new PdfLoadedDocument(spSourceDocument.OpenBinary()); // ** Restrict the rights ApplySecurity(sourceDocument); // ** Create the watermark Template PdfTemplate watermarkTemplate = CreateWatermark(watermark); // ** Iterate over all pages and apply the watermark foreach (PdfPageBase page in sourceDocument.Pages) { // ** Position the watermark at the centre of the page float wmWidth = watermarkTemplate.Width; float wmHeight = watermarkTemplate.Height; float wmLeft = ((page.Size.Width - wmWidth) / 2); float wmTop = ((page.Size.Height - wmHeight) / 2); PointF wmPosition = new PointF(wmLeft, wmTop); // ** Place watermark behind or in front of text? if (watermarkInBackground == true) { PdfTemplate pageTemplate = page.CreateTemplate(); page.Layers.Clear(); PdfGraphics g = page.Graphics; g.SetTransparency(watermarkTransparancy); g.DrawPdfTemplate(watermarkTemplate, wmPosition); g.SetTransparency(1f); g.DrawPdfTemplate(pageTemplate, PointF.Empty, page.Size); } else { PdfGraphics g = page.Graphics; g.SetTransparency(watermarkTransparancy); g.DrawPdfTemplate(watermarkTemplate, wmPosition); } } // ** 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()) { sourceDocument.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."); }