示例#1
0
 public void Initialize()
 {
     this.currentDir     = Directory.GetCurrentDirectory();
     this.testRessources = Path.GetFullPath(currentDir + Path.DirectorySeparatorChar + ".." + Path.DirectorySeparatorChar + ".." + Path.DirectorySeparatorChar + "Testresources");
     this.pdfRessources  = testRessources + Path.DirectorySeparatorChar + "pdf" + Path.DirectorySeparatorChar;
     this.outDir         = Path.GetFullPath(currentDir + "testout");
     Directory.CreateDirectory(outDir);
     this.qpdf = new QpdfAdapter();
 }
示例#2
0
        public void PdfEncryptExisting()
        {
            string password      = "******";
            string inPdf         = "usdl4_match_keyword.pdf";
            string sourcePdfFile = Path.Combine(this.pdfRessources, inPdf);
            // Set source and target folders
            string targetFolder = outDir;
            string srcFolder    = testRessources + Path.DirectorySeparatorChar + "tozip" + Path.DirectorySeparatorChar;
            var    method       = System.Reflection.MethodBase.GetCurrentMethod().Name;
            string targetFile   = Path.Combine(targetFolder, string.Concat(DateTime.Now.ToString("yyyyMMdd"), "_" + method + "_encrypted.pdf"));

            if (File.Exists(targetFile))
            {
                File.Delete(targetFile);
            }
            Assert.IsFalse(File.Exists(targetFile));

            //call init
            bool success = qpdf.init();

            Assert.IsTrue(success);
            Assert.IsTrue(qpdf.isInitialized());
            Assert.AreNotEqual(qpdf.getContext(), IntPtr.Zero);

            // read file
            int error = qpdf.read(sourcePdfFile);

            Assert.AreEqual(0, error);
            Assert.IsFalse(qpdf.has_error());

            error = qpdf.initWrite(targetFile);
            Assert.AreEqual(0, error);
            Assert.IsFalse(qpdf.has_error());

            qpdf.setRestrictiveAes256EncryptionOptions(password);
            Assert.IsFalse(qpdf.has_error(), "Error in lib after setting encryption options");

            //call write compressed
            error = qpdf.write(false);
            Assert.AreEqual(0, error);
            Assert.IsFalse(qpdf.has_error());

            Assert.IsTrue(File.Exists(targetFile), "Target file does not exist");
            FileInfo targetFileInfo = new FileInfo(targetFile);

            Assert.IsNotNull(targetFileInfo, "Could not read target file info");
            Assert.IsTrue(targetFileInfo.Length > 20000, "Target file is too small");

            qpdf.cleanup();
            Assert.AreEqual(qpdf.getContext(), IntPtr.Zero);

            // read the file again and check if encrypted
            this.qpdf = new QpdfAdapter();
            success   = qpdf.init();
            Assert.IsTrue(success);
            Assert.IsTrue(qpdf.isInitialized());
            Assert.AreNotEqual(qpdf.getContext(), IntPtr.Zero);
            error = qpdf.read(targetFile, password);
            Assert.AreEqual(0, error, "PDF Read failed");
            Assert.IsFalse(qpdf.has_error());

            Assert.IsTrue(qpdf.isEncrypted(), "output file is not showing as encrypted after loading again");

            qpdf.cleanup();
            Assert.AreEqual(qpdf.getContext(), IntPtr.Zero);
        }