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); }
public void ReadWritePDF() { string inPdf = "Skyhigh-Secure-Datasheet-0214.pdf"; string sourcePdfFile = Path.Combine(this.pdfRessources, inPdf); // Set source and target folders string targetFolder = testRessources; var method = System.Reflection.MethodBase.GetCurrentMethod().Name; string targetFileComp = Path.Combine(outDir, string.Concat(DateTime.Now.ToString("yyyyMMdd"), "_" + method + "_compressed.pdf")); string targetFileUnComp = Path.Combine(outDir, string.Concat(DateTime.Now.ToString("yyyyMMdd"), "_" + method + "_uncompressed.pdf")); Assert.IsTrue(File.Exists(sourcePdfFile), "Source file doesn't exist"); if (File.Exists(targetFileComp)) { File.Delete(targetFileComp); } Assert.IsFalse(File.Exists(targetFileComp)); if (File.Exists(targetFileUnComp)) { File.Delete(targetFileUnComp); } Assert.IsFalse(File.Exists(targetFileUnComp)); //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); bool hasError = qpdf.has_error(); Assert.IsFalse(hasError); // bool encrypted = qpdf.isEncrypted(); Assert.IsFalse(encrypted, "lib claims file is encrypted but isn't"); //call write uncompressed error = qpdf.writeSimple(targetFileUnComp, false); Assert.AreEqual(0, error); hasError = qpdf.has_error(); Assert.IsFalse(hasError); Assert.IsTrue(File.Exists(targetFileUnComp), "target file uncomp does not exist after write"); //call write compressed error = qpdf.writeSimple(targetFileComp); Assert.AreEqual(0, error); hasError = qpdf.has_error(); Assert.IsFalse(hasError); Assert.IsTrue(File.Exists(targetFileComp), "taregt file comp does not exist"); Assert.IsTrue(new FileInfo(targetFileComp).Length < new FileInfo(targetFileUnComp).Length, "Uncompressed file is not smaller than the compressed file"); qpdf.cleanup(); Assert.AreEqual(qpdf.getContext(), IntPtr.Zero); }