예제 #1
0
        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");
        }
예제 #2
0
        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
        }
예제 #3
0
        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);
        }
예제 #4
0
 protected override PdfDocument InitDocument(PdfReader reader, PdfWriter writer, StampingProperties properties)
 {
     try
     {
         return(base.InitDocument(reader, writer, properties));
     }
     finally
     {
         FieldInfo        propertiesField  = typeof(PdfWriter).GetField("properties", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
         WriterProperties writerProperties = (WriterProperties)propertiesField.GetValue(writer);
         writerProperties.SetFullCompressionMode(false);
     }
 }
예제 #5
0
        public void CompressedPdfiText()
        {
            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 targetFileComp   = Path.Combine(targetFolder, string.Concat(DateTime.Now.ToString("yyyyMMdd"), "_" + method + "_compressed.pdf"));
            string targetFileUnComp = Path.Combine(targetFolder, 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));


            // read file
            WriterProperties propComp = new WriterProperties();

            propComp.SetFullCompressionMode(true);
            PdfDocument origPdf = new PdfDocument(new PdfReader(sourcePdfFile), new PdfWriter(targetFileComp, propComp));

            Assert.IsNotNull(origPdf);

            PdfWriter theCompWriter = origPdf.GetWriter();

            origPdf.Close();

            Assert.IsTrue(File.Exists(targetFileComp), "target file comp does not exist");

            // read file
            PdfDocument origPdfAgain = new PdfDocument(new PdfReader(sourcePdfFile), new PdfWriter(targetFileUnComp));

            Assert.IsNotNull(origPdfAgain);
            origPdfAgain.GetWriter().SetCompressionLevel(CompressionConstants.NO_COMPRESSION);
            PdfWriter theUncompWriter = origPdfAgain.GetWriter();

            origPdfAgain.Close();

            Assert.IsTrue(File.Exists(targetFileUnComp), "target file uncomp does not exist");

            Assert.IsTrue(new FileInfo(targetFileComp).Length < new FileInfo(targetFileUnComp).Length, "Uncompressed file is not smaller than the compressed file");
        }