SetEncryption() public méthode

public SetEncryption ( X509Certificate certs, int permissions, int encryptionType ) : void
certs Org.BouncyCastle.X509.X509Certificate
permissions int
encryptionType int
Résultat void
Exemple #1
0
        /// <summary>
        /// Create a new PDF text document.
        /// </summary>
        /// <param name="pdf">The PDF file stream.</param>
        /// <param name="text">The text to add to the document.</param>
        /// <param name="font">The text font to create.</param>
        /// <param name="password">The password used to protect the document.</param>
        /// <exception cref="System.Exception"></exception>
        public void CreateText(Stream pdf, string text, Nequeo.Drawing.Pdf.Font font, string password)
        {
            iTextSharp.text.Document document = null;

            try
            {
                // Create the document.
                document = new iTextSharp.text.Document();
                iTextSharp.text.pdf.PdfWriter pdfWriter = iTextSharp.text.pdf.PdfAWriter.GetInstance(document, pdf);
                pdfWriter.SetEncryption(iTextSharp.text.pdf.PdfWriter.ENCRYPTION_AES_256, password, password, 0);
                document.Open();

                // Add the text.
                iTextSharp.text.Font fontText = font.GetFont();
                document.Add(new iTextSharp.text.Paragraph(text, fontText));

                // Close the document.
                document.Close();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (document != null)
                {
                    document.Dispose();
                }
            }
        }
 // based on http://itextsharp.sourceforge.net/examples/Encrypt.cs
 public string ModifyPermissions(string PathSource, string PathTarget, string UserPassword, List <int> Permissons)
 {
     try
     {
         int PDFpermisson = 0;
         foreach (int permisson in Permissons)
         {
             PDFpermisson = PDFpermisson | permisson;
         }
         iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(PathSource);
         int n = reader.NumberOfPages;
         iTextSharp.text.Document      document = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(1));
         iTextSharp.text.pdf.PdfWriter writer   = iTextSharp.text.pdf.PdfWriter.GetInstance(document, new System.IO.FileStream(PathTarget, System.IO.FileMode.Create));
         writer.SetEncryption(iTextSharp.text.pdf.PdfWriter.STRENGTH128BITS, UserPassword, null, (int)PDFpermisson);
         // step 3: we open the document
         document.Open();
         iTextSharp.text.pdf.PdfContentByte  cb = writer.DirectContent;
         iTextSharp.text.pdf.PdfImportedPage page;
         int rotation;
         int i = 0;
         // step 4: we add content
         while (i < n)
         {
             i++;
             document.SetPageSize(reader.GetPageSizeWithRotation(i));
             document.NewPage();
             page     = writer.GetImportedPage(reader, i);
             rotation = reader.GetPageRotation(i);
             if (rotation == 90 || rotation == 270)
             {
                 cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
             }
             else
             {
                 cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
             }
         }
         document.Close();
         return("");
     }
     catch (Exception e)
     {
         return(e.Message);
     }
 }