public void EncryptiText() { string password = "******"; string inPdf = "Skyhigh-Secure-Datasheet-0214.pdf"; string sourcePdfFile = Path.Combine(this.pdfRessources, inPdf); // Set source and target folders string targetFolder = outDir; var method = System.Reflection.MethodBase.GetCurrentMethod().Name; string targetFile = Path.Combine(targetFolder, string.Concat(DateTime.Now.ToString("yyyyMMdd"), "_" + method + "_encrypted.pdf")); Assert.IsTrue(File.Exists(sourcePdfFile), "Source file doesn't exist"); if (File.Exists(targetFile)) { File.Delete(targetFile); } Assert.IsFalse(File.Exists(targetFile)); byte[] passwordBytes = Encoding.ASCII.GetBytes(password); WriterProperties propComp = new WriterProperties(); propComp.SetFullCompressionMode(false); propComp.SetCompressionLevel(CompressionConstants.NO_COMPRESSION); propComp.SetStandardEncryption(passwordBytes, passwordBytes, 0, EncryptionConstants.ENCRYPTION_AES_256 | EncryptionConstants.DO_NOT_ENCRYPT_METADATA); PdfWriter theCompWriter = new PdfWriter(targetFile, propComp); PdfDocument origPdf = new PdfDocument(new PdfReader(sourcePdfFile), theCompWriter); Assert.IsNotNull(origPdf); origPdf.Close(); Assert.IsTrue(new FileInfo(sourcePdfFile).Length < new FileInfo(targetFile).Length, "target file is not bigger than the source file"); //check out http://itextsupport.com/apidocs/itext7/latest/com/itextpdf/kernel/pdf/PdfEncryptedPayloadDocument.html }
public void AttachFileiText() { string inPdf = "noattach.pdf"; string sourcePdfFile = Path.Combine(this.pdfRessources, inPdf); // Set source and target folders string targetFolder = outDir; var method = System.Reflection.MethodBase.GetCurrentMethod().Name; string targetFile = Path.Combine(targetFolder, string.Concat(DateTime.Now.ToString("yyyyMMdd"), "_" + method + ".pdf")); Assert.IsTrue(File.Exists(sourcePdfFile), "Source file doesn't exist"); if (File.Exists(targetFile)) { File.Delete(targetFile); } Assert.IsFalse(File.Exists(targetFile)); WriterProperties propComp = new WriterProperties(); propComp.SetFullCompressionMode(false); propComp.SetCompressionLevel(CompressionConstants.NO_COMPRESSION); PdfWriter theCompWriter = new PdfWriter(targetFile, propComp); PdfDocument origPdf = new PdfDocument(new PdfReader(sourcePdfFile), theCompWriter); Assert.IsNotNull(origPdf); PdfFileSpec spec = PdfFileSpec.CreateEmbeddedFileSpec(origPdf, Encoding.ASCII.GetBytes("Some text in the attached document"), "description of attachment here", "test.txt", null, null, null); origPdf.AddFileAttachment("attadescr", spec); origPdf.Close(); Assert.IsTrue(new FileInfo(sourcePdfFile).Length < new FileInfo(targetFile).Length, "target file is not bigger than the source file"); }
private bool createiText7Pdf(string password = null) { bool success = false; log.Info($"Using iText7 library to create a package with format [{this.packerType}]"); if (this.overwrite) { deleteIfExist(this.dstFileName); } try { Stream stream = getTemplatePdfStream(); if (stream == null) { log.Error("Could not read the PDF Template from stream"); throw new FileNotFoundException("Could not read the PDF template"); } WriterProperties propComp = new WriterProperties(); propComp.SetFullCompressionMode(true); propComp.SetCompressionLevel(CompressionConstants.BEST_COMPRESSION); if (String.IsNullOrEmpty(password)) { log.Warn($"No password was supplied, so the output file will be unencrypted [{this.dstFileName}]"); } else { byte[] passwordBytes = Encoding.ASCII.GetBytes(password); propComp.SetStandardEncryption(passwordBytes, passwordBytes, 0, EncryptionConstants.ENCRYPTION_AES_256 | EncryptionConstants.DO_NOT_ENCRYPT_METADATA); log.Info("Enabled PDF AES_256 encryption"); } PdfWriter theCompWriter = new PdfWriter(this.dstFileName, propComp); PdfDocument newPdf = new PdfDocument(new PdfReader(stream), theCompWriter); foreach (string file in this.srcFileList) { string filename = Path.GetFileName(file); PdfFileSpec fileSpec = PdfFileSpec.CreateEmbeddedFileSpec( newPdf, File.ReadAllBytes(file), filename, filename, null, null, null); newPdf.AddFileAttachment(filename, fileSpec); log.Debug($"Added file '{filename}' as attachment to pdf"); } newPdf.Close(); } catch (Exception e) { log.Error($"Exception while trying to pack with iText7", e); return(false); } log.Info($"Successfully packed the files into [{this.dstFileName}]"); return(success); }