sharepoint 2010 - Watermarking pdf on document upload -


i want add functionality of adding watermark using itextsharp library pdf document being added library. created event listener triggered when item being added. code follows :

using system; using system.security.permissions; using microsoft.sharepoint; using microsoft.sharepoint.utilities; using microsoft.sharepoint.workflow; using itextsharp.text; using itextsharp.text.pdf; using system.io;  namespace projectprac.watermarkonupload {     /// <summary>     /// list item events     /// </summary>     public class watermarkonupload : spitemeventreceiver     {         /// <summary>         /// item being added.         /// </summary>         public override void itemadding(spitemeventproperties properties)         {             base.itemadding(properties);             string watermarkedfile = "watermarked.pdf";         // creating watermark on separate layer         // creating itextsharp.text.pdf.pdfreader object read existing pdf document          pdfreader reader1 = new pdfreader("c:\\users\\desktop\\hello.pdf");      //the relative path          using (filestream fs = new filestream(watermarkedfile, filemode.create, fileaccess.write, fileshare.none))         // creating itextsharp.text.pdf.pdfstamper object write data itextsharp.text.pdf.pdfreader object filestream object         using (pdfstamper stamper = new pdfstamper(reader1, fs))         {             // getting total number of pages of existing document             int pagecount = reader1.numberofpages;              // create new layer watermark             pdflayer layer = new pdflayer("watermarklayer", stamper.writer);             // loop through each page             (int = 1; <= pagecount; i++)             {                 // getting page size                 rectangle rect = reader1.getpagesize(i);                  // contentbyte object                 pdfcontentbyte cb = stamper.getundercontent(i);                  // tell cb next commands should "bound" new layer                 cb.beginlayer(layer);                 cb.setfontandsize(basefont.createfont(                   basefont.helvetica, basefont.cp1252, basefont.not_embedded), 50);                  pdfgstate gstate = new pdfgstate();                 gstate.fillopacity = 0.25f;                 cb.setgstate(gstate);                  cb.setcolorfill(basecolor.black);                 cb.begintext();                 cb.showtextaligned(pdfcontentbyte.align_center, "confidential", rect.width / 2, rect.height / 2, 45f);                 cb.endtext();                  // close layer                 cb.endlayer();           }       } } 

i want know how add path without hardcoding here :

pdfreader reader1 = new pdfreader("c:\\users\\desktop\\hello.pdf");      //the relative path 

and uploading watermarked document library , not original pdf. know can done through workflow pretty new sharepoint. if @ have answer has workflow in please give link explains workflow automating pdf watermarking.

you don't need have workflow achieve looking for:

first, use itemadded event instead of itemadding. can access spfile associated updated list item.

public override void itemadded(spitemeventproperties properties) {     var password = string.empty; //or put password handling     splistitem listitemtofile = properties.listitem;     spfile pdforiginalfile = listitemtofile.file;     //get byte[] of uploaded file     byte[] contentpdforiginalfile = pdforiginalfile.openbinary();     //create reader byte[]     var pdfreader = new pdfreader(new randomaccessfileorarray(contentpdforiginalfile), password);      using (var ms = new memorystream()) {           using (var stamper = new pdfstamper(pdfreader, ms, '\0', true)) {         // watermarking stuff           ...         // resuming sp stuff             }              var watermarkedpdfcontent = ms.toarray();         base.eventfiringenabled = false; //to prevent other events being fired          var folder = pdforiginalfile.parentfolder;//you want upload same place         folder.files.add(contentpdforiginalfile.name, fs.toarray(),true);         base.eventfiringenabled = true;        } } 

i did typo or 2 since didn't run code. however, should give idea.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -