2015-04-23 59 views
0

我想添加一個功能,使用itextSharp庫添加水印到正在被添加到庫中的pdf文檔。爲此,我創建了一個事件偵聽器,該事件偵聽器在添加項目時觸發。代碼如下:關於文檔上傳的水印pdf

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> 
     /// An item is being added. 
     /// </summary> 
     public override void ItemAdding(SPItemEventProperties properties) 
     { 
      base.ItemAdding(properties); 
      string watermarkedFile = "Watermarked.pdf"; 
     // Creating watermark on a separate layer 
     // Creating iTextSharp.text.pdf.PdfReader object to read the 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 to write Data from iTextSharp.text.pdf.PdfReader object to FileStream object 
     using (PdfStamper stamper = new PdfStamper(reader1, fs)) 
     { 
      // Getting total number of pages of the Existing Document 
      int pageCount = reader1.NumberOfPages; 

      // Create New Layer for Watermark 
      PdfLayer layer = new PdfLayer("WatermarkLayer", stamper.Writer); 
      // Loop through each Page 
      for (int i = 1; i <= pageCount; i++) 
      { 
       // Getting the Page Size 
       Rectangle rect = reader1.GetPageSize(i); 

       // Get the ContentByte object 
       PdfContentByte cb = stamper.GetUnderContent(i); 

       // Tell the cb that the next commands should be "bound" to this 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 the layer 
       cb.EndLayer(); 


     } 


    } 
} 

我想知道如何添加路徑,而不在這裏硬編碼:

PdfReader reader1 = new PdfReader("C:\\Users\\Desktop\\Hello.pdf");  //THE RELATIVE PATH 

然後上傳水印文檔庫,而不是原始的PDF。 我知道它也可以通過工作流完成,但我對共享點來說很新。因此,如果你有一個有工作流程的答案,請給出解釋自動化PDF水印工作流程的鏈接。

回答

0

你不需要有工作流程,實現你在找什麼:

首先,使用ItemAdded事件,而不是ItemAdding。然後,您可以訪問與更新的列表項關聯的SPFile。

public override void ItemAdded(SPItemEventProperties properties) 
{ 
    var password = string.Empty; //or you put some password handling 
    SPListItem listItemToFile = properties.Listitem; 
    SPFile pdfOriginalFile = listItemToFile.File; 
    //get byte[] of uploaded file 
    byte[] contentPdfOriginalFile = pdfOriginalFile.OpenBinary(); 
    //create reader from byte[] 
    var pdfReader = new PdfReader(new RandomAccessFileOrArray(contentPdfOriginalFile), password); 

    using (var ms = new MemoryStream()) { 
     using (var stamper = new PdfStamper(pdfReader, ms, '\0', true)) { 
     // do your watermarking stuff 
     ... 
     // resuming SP stuff  
     }  

     var watermarkedPdfContent = ms.ToArray(); 
     base.EventFiringEnabled = false; //to prevent other events being fired 
     var folder = pdfOriginalFile.ParentFolder;//you want to upload to the same place 
     folder.Files.Add(contentPdfOriginalFile.Name, fs.ToArray(),true); 
     base.EventFiringEnabled = true; 
    } 
} 

我可能做了一兩個錯字,因爲我沒有運行這段代碼。但是,它應該給你一個想法。