Exemplo n.º 1
0
        /// <summary>
        /// Generates a PDF document that is encrypted using AES method.
        /// </summary>
        /// <param name="keySize">The encryption key size.</param>
        /// <param name="aes"></param>
        /// <returns></returns>
        private static PDFFixedDocument EncryptAES(int keySize, PDFAesSecurityHandler aes)
        {
            PDFFixedDocument doc = new PDFFixedDocument();

            aes.EnableContentExtractionForAccessibility = false;
            aes.EnableDocumentAssembly         = false;
            aes.EnableDocumentChange           = false;
            aes.EnableContentExtraction        = false;
            aes.EnableFormsFill                = false;
            aes.EnableAnnotationsAndFieldsEdit = false;
            aes.EnablePrint     = false;
            aes.EncryptMetadata = true;
            aes.KeySize         = keySize;
            aes.UserPassword    = "******";
            aes.OwnerPassword   = "******";

            PDFPage         page       = doc.Pages.Add();
            PDFStandardFont helvetica  = new PDFStandardFont(PDFStandardFontFace.HelveticaBoldItalic, 16);
            PDFBrush        blackBrush = new PDFBrush();

            string text = string.Format("Encryption: AES {0} bit", keySize);

            if ((aes.KeySize == 256) && aes.UseEnhancedPasswordValidation)
            {
                text = text + " with enhanced password validation (Acrobat X or later)";
            }
            page.Canvas.DrawString(text, helvetica, blackBrush, 50, 100);

            return(doc);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Main method for running the sample.
        /// </summary>
        public static SampleOutputInfo[] Run(Stream input)
        {
            PDFRc4SecurityHandler rc4_40    = new PDFRc4SecurityHandler();
            PDFFixedDocument      document1 = EncryptRC4(40, rc4_40);
            PDFRc4SecurityHandler rc4_128   = new PDFRc4SecurityHandler();
            PDFFixedDocument      document2 = EncryptRC4(128, rc4_128);

            PDFAesSecurityHandler aes128    = new PDFAesSecurityHandler();
            PDFFixedDocument      document3 = EncryptAES(128, aes128);
            PDFAesSecurityHandler aes256    = new PDFAesSecurityHandler();
            PDFFixedDocument      document4 = EncryptAES(256, aes256);
            PDFAesSecurityHandler aes256e   = new PDFAesSecurityHandler();

            aes256e.UseEnhancedPasswordValidation = true;
            PDFFixedDocument document5 = EncryptAES(256, aes256e);
            PDFFixedDocument document6 = Decrypt(input);

            SampleOutputInfo[] output = new SampleOutputInfo[]
            {
                new SampleOutputInfo(document1, "encryption.rc4.40bit.pdf", rc4_40),
                new SampleOutputInfo(document2, "encryption.rc4.128bit.pdf", rc4_128),
                new SampleOutputInfo(document3, "encryption.aes.128bit.pdf", aes128),
                new SampleOutputInfo(document4, "encryption.aes.256bit.pdf", aes256),
                new SampleOutputInfo(document5, "encryption.aes.256bit.enhanced.pdf", aes256e),
                new SampleOutputInfo(document6, "encryption.decrypted.pdf"),
            };
            return(output);
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            string supportPath = "..\\..\\..\\..\\..\\SupportFiles\\";

            PDFMerger merger = new PDFMerger();

            merger.AppendFile(supportPath + "content.pdf");
            merger.AppendFile(supportPath + "formfill.pdf");
            merger.AppendFile(supportPath + "encrypted.pdf", "pdf4net");

            PDFAesSecurityHandler aes256e = new PDFAesSecurityHandler();

            aes256e.KeySize = 256;
            aes256e.UseEnhancedPasswordValidation = true;
            aes256e.UserPassword = "******";
            merger.Save("mergeandencrypt.pdf", aes256e);

            Console.WriteLine("Merged file saved with success to current folder.");
        }
Exemplo n.º 4
0
        // For this sample to work, the following file
        // need to exist: multicolumntextandimages.pdf.
        // Compile the corresponding sample to get this file
        public static void EncryptFile()
        {
            //PDF4NET v5:
            //PDFSecurityManager securityManager = new PDFSecurityManager();
            //securityManager.KeySize = EncryptionKeySize.Use128BitKey;
            //securityManager.UserPassword = Encoding.ASCII.GetBytes("userpass");
            //securityManager.OwnerPassword = Encoding.ASCII.GetBytes("ownerpass");
            //securityManager.AllowPrint = false;
            //securityManager.FullQualityPrint = true;
            //securityManager.AllowModifyDocument = false;
            //securityManager.AllowExtractContent = false;
            //securityManager.AllowInteractiveEdit = false;
            //securityManager.AllowFormsFill = true;
            //securityManager.AllowAccessibilityExtractContent = true;
            //securityManager.AllowAssembleDocument = false;

            //PDFFile.EncryptFile(securityManager,
            //    "Sample_MultiColumnTextAndImages.pdf", "Sample_MultiColumnTextAndImages-Secure.pdf");

            PDFFixedDocument      document           = new PDFFixedDocument("..\\..\\..\\..\\..\\SupportFiles\\content.pdf");
            PDFAesSecurityHandler aesSecurityHandler = new PDFAesSecurityHandler();

            aesSecurityHandler.KeySize                                 = 256;
            aesSecurityHandler.UserPassword                            = "******";
            aesSecurityHandler.OwnerPassword                           = "******";
            aesSecurityHandler.EnablePrint                             = false;
            aesSecurityHandler.HighQualityPrint                        = true;
            aesSecurityHandler.EnableDocumentChange                    = false;
            aesSecurityHandler.EnableContentExtraction                 = false;
            aesSecurityHandler.EnableAnnotationsAndFieldsEdit          = false;
            aesSecurityHandler.EnableFormsFill                         = true;
            aesSecurityHandler.EnableContentExtractionForAccessibility = true;
            aesSecurityHandler.EnableDocumentAssembly                  = false;
            document.Save("content-secure.pdf", aesSecurityHandler);

            // or
            //PDFDocument doc =
            //	PDFFile.EncryptFile( securityManager, "multicolumntextandimages-secure.pdf" );
            ////Serial number goes here
            //doc.SerialNumber = "serial number goes here";
            //doc.Save( stream );
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            // Create the pdf document
            //PDF4NET v5: PDFDocument pdfDoc = new PDFDocument();
            PDFFixedDocument pdfDoc = new PDFFixedDocument();

            // set the security options
            //PDF4NET v5: pdfDoc.SecurityManager = new PDFSecurityManager();
            PDFAesSecurityHandler aesSecurityHandler = new PDFAesSecurityHandler();

            // use 128 bit encryption
            //PDF4NET v5: pdfDoc.SecurityManager.KeySize = EncryptionKeySize.Use128BitKey;
            aesSecurityHandler.KeySize = 256;
            // set user password to "userpass"
            //PDF4NET v5: pdfDoc.SecurityManager.UserPassword = Encoding.ASCII.GetBytes("userpass");
            aesSecurityHandler.UserPassword = "******";
            // set owner password to "ownerpass"
            //PDF4NET v5: pdfDoc.SecurityManager.OwnerPassword = Encoding.ASCII.GetBytes("ownerpass");
            aesSecurityHandler.OwnerPassword = "******";
            // allow to print the pdf document
            //PDF4NET v5: pdfDoc.SecurityManager.AllowPrint = true;
            aesSecurityHandler.EnablePrint = true;
            // do not allow high quality print
            //PDF4NET v5: pdfDoc.SecurityManager.FullQualityPrint = false;
            aesSecurityHandler.HighQualityPrint = false;
            // do not alow to modify the document
            //PDF4NET v5: pdfDoc.SecurityManager.AllowModifyDocument = false;
            aesSecurityHandler.EnableDocumentChange = false;
            // do not allow to extract content (text and images) from the document
            //PDF4NET v5: pdfDoc.SecurityManager.AllowExtractContent = false;
            aesSecurityHandler.EnableContentExtraction = false;
            // do not allow to fill forms or to create annotations
            //PDF4NET v5: pdfDoc.SecurityManager.AllowInteractiveEdit = false;
            aesSecurityHandler.EnableAnnotationsAndFieldsEdit = false;
            // do not allow forms fill
            //PDF4NET v5: pdfDoc.SecurityManager.AllowFormsFill = false;
            aesSecurityHandler.EnableFormsFill = false;
            // allow to extract content in support for accessibility
            //PDF4NET v5: pdfDoc.SecurityManager.AllowAccessibilityExtractContent = true;
            aesSecurityHandler.EnableContentExtractionForAccessibility = true;
            // do not allow to assemble document
            //PDF4NET v5: pdfDoc.SecurityManager.AllowAssembleDocument = false;
            aesSecurityHandler.EnableDocumentAssembly = false;

            // Create one page
            //PDF4NET v5: PDFPage pdfPage = pdfDoc.AddPage();
            PDFPage pdfPage = pdfDoc.Pages.Add();

            // Draw "Encrypted Hello world" in the center of the page
            //PDF4NET v5:
            //pdfPage.Canvas.DrawText("Encrypted",
            //    new PDFFont(PDFFontFace.Helvetica, 100), new PDFPen(new PDFRgbColor(255, 0, 0), 1),
            //    new PDFBrush(new PDFRgbColor(0, 0, 255)), pdfPage.Width / 2, pdfPage.Height / 2 - 3,
            //    0, PDFTextAlign.BottomCenter);
            PDFStringLayoutOptions slo = new PDFStringLayoutOptions();

            slo.X = pdfPage.Width / 2;
            slo.Y = pdfPage.Height / 2 - 3;
            slo.HorizontalAlign = PDFStringHorizontalAlign.Center;
            slo.VerticalAlign   = PDFStringVerticalAlign.Bottom;
            PDFStringAppearanceOptions sao = new PDFStringAppearanceOptions();

            sao.Font  = new PDFStandardFont(PDFStandardFontFace.Helvetica, 100);
            sao.Pen   = new PDFPen(new PDFRgbColor(255, 0, 0), 1);
            sao.Brush = new PDFBrush(new PDFRgbColor(0, 0, 255));
            pdfPage.Canvas.DrawString("Encrypted", sao, slo);
            slo.Y             = pdfPage.Height / 2 + 3;
            slo.VerticalAlign = PDFStringVerticalAlign.Top;
            pdfPage.Canvas.DrawString("Hello world !", sao, slo);

            // Save the document to disk
            //PDF4NET v5:pdfDoc.Save("Sample_Encryption.pdf");
            pdfDoc.Save("Sample_Encryption.pdf", aesSecurityHandler);
        }