2011-05-03 324 views
1

我迫切需要在不同类型的pdf上添加水印,包括普通pdf,数字签名的pdf和密码prodected pdf以编程方式使用C#。我可以对使用下面的代码进行数字签名的普通pdf和一些pdf进行水印处理,但对于数字签名和安全保护的其他pdf文件不起作用。任何人都可以告诉我如何使用itextsharp删除pdf的安全性,以便它们可以被嵌入水印。如何使用itextsharp去除PDF和水印的安全性pdf

以下代码可以在不受安全保护但不能在安全保护pdf上加水印的普通PDF上添加水印。

public void AddWatermarkText(string sourceFile, string outputFile, 
string watermarkText, iTextSharp.text.pdf.BaseFont watermarkFont, float 
watermarkFontSize, iTextSharp.text.Color watermarkFontColor, float 
watermarkFontOpacity, float watermarkRotation) 
    { 
     iTextSharp.text.pdf.PdfReader reader = null; 
     iTextSharp.text.pdf.PdfStamper stamper = null; 
     iTextSharp.text.pdf.PdfGState gstate = null; 
     iTextSharp.text.pdf.PdfContentByte underContent = null; 
     iTextSharp.text.Rectangle rect = null; 

     int pageCount = 0; 
     try 
     { 
          { 
       reader = new iTextSharp.text.pdf.PdfReader(sourceFile); 
       rect = reader.GetPageSizeWithRotation(1); 
       stamper = new PdfStamper(reader, new System.IO.FileStream(outputFile, System.IO.FileMode.CreateNew), '\0', true); 

       if (watermarkFont == null) 
       { 
        watermarkFont =iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.HELVETICA,iTextSharp.text.pdf.BaseFont.CP1252,iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED); 
       } 
       if (watermarkFontColor == null) 
       { 
        watermarkFontColor = iTextSharp.text.Color.BLUE; 
       } 
       gstate = new iTextSharp.text.pdf.PdfGState(); 
       gstate.FillOpacity = watermarkFontOpacity; 
       gstate.StrokeOpacity = watermarkFontOpacity; 
       pageCount = reader.NumberOfPages; 
       for (int i = 1; i <= pageCount; i++) 
       { 
        underContent = stamper.GetUnderContent(i); 
        //_with1 = underContent; 
        underContent.SaveState(); 
        underContent.SetGState(gstate); 
        underContent.SetColorFill(watermarkFontColor); 
        underContent.BeginText(); 
        underContent.SetFontAndSize(watermarkFont,watermarkFontSize); 
        underContent.SetTextMatrix(30, 30); 

underContent.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER,watermarkText,rect.Width/2, rect.Height/2, watermarkRotation); 
        underContent.EndText(); 
        underContent.RestoreState(); 
       } 
      } 
      stamper.Close(); 
      reader.Close(); 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 
    } 

感谢

+0

安全保护的重点在于您无法修改文档。 – 2011-05-03 15:32:39

回答

1

iTextSharp.text.pdf.PdfReader允许你,如果你知道密码指定所有者的密码作为参数。如果您有权这样做,也可以使用许多在线网站删除PDF文件的安全设置。 iTextSharp无法删除安全性设置。

+0

就我而言,我不能将一个所有者Passowrd作为参数。所以,我需要去寻找其他的选择。你可以建议我一些网站,这将有助于我申请我的代码来删除安全设置。你也可以给我建议我最好的选择。 (我在法律上有权更改安全设置) – 2011-05-03 17:38:25

+0

如果您无法控制PDF文档的来源,可以在此处使用此工具([link] http://soft.rubypdf.com/software/pdfcrypt)并运行服务器上的可执行文件通过.net。它将扁平化任何形式的文件(不会再被填充)。解密的命令参数为:pdfcrypt input_file.pdf output_file.pdf解密“”确保对未知密码使用一对双引号。 – Jay 2011-05-03 19:08:56

+0

我使用(http://rubypdf.appspot.com/pdfdecrypt.html)在线解密了我的pdf,并尝试使用上面的代码对此解密的pdf进行水印处理。但是我的代码仍然无法为PDF加水印。那么,你能否告诉我为什么某些pdf可以使用上面的代码容易地嵌入水印,即使它们是数字签名的。同时,即使使用上述软件解密,也不能加水印。在这种情况下,你认为什么是最好的选择。 – 2011-05-03 20:06:43