Ejemplo n.º 1
0
        public void ErrorTest()
        {
            //call init
            bool success = qpdf.init();

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

            //check for error, we expect one to be there here
            bool hasError = qpdf.has_error();

            Assert.IsFalse(hasError);
            IntPtr errPtr = qpdf.get_error();

            Assert.IsFalse(errPtr != IntPtr.Zero);

            int errorCodeCurr = qpdf.get_error_code(errPtr);

            Assert.AreEqual(errorCodeCurr, 0);

            int errorCode = qpdf.get_error_code();

            Assert.AreEqual(errorCode, 0);

            QPDF_ERROR_CODE_E estructCurr = qpdf.get_error_code_enum();

            Assert.AreEqual(estructCurr, QPDF_ERROR_CODE_E.qpdf_e_success);

            QPDF_ERROR_CODE_E estruct = qpdf.get_error_code_enum(errPtr);

            Assert.AreEqual(estruct, QPDF_ERROR_CODE_E.qpdf_e_success);

            string errorStrCurr = qpdf.get_error_full_text(errPtr);

            Assert.AreEqual(errorStrCurr, "");

            string errorStr = qpdf.get_error_full_text();

            Assert.AreEqual(errorStr, "");

            //call cleanup
            qpdf.cleanup();
            Assert.AreEqual(qpdf.getContext(), IntPtr.Zero);
        }
Ejemplo n.º 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);
        }