コード例 #1
0
        private void ExtractEncrypted(String encryptedName, String wrapperName, byte[] password)
        {
            String      inPath   = sourceFolder + "cmp_" + wrapperName;
            String      cmpPath  = sourceFolder + "cmp_" + encryptedName;
            String      outPath  = destinationFolder + encryptedName;
            String      diff     = "diff_" + encryptedName + "_";
            PdfDocument document = new PdfDocument(new PdfReader(inPath));
            PdfEncryptedPayloadDocument encryptedDocument = document.GetEncryptedPayloadDocument();

            byte[]     encryptedDocumentBytes = encryptedDocument.GetDocumentBytes();
            FileStream fos = new FileStream(outPath, FileMode.Create);

            fos.Write(encryptedDocumentBytes);
            fos.Dispose();
            document.Close();
            PdfEncryptedPayload ep = encryptedDocument.GetEncryptedPayload();

            NUnit.Framework.Assert.AreEqual(PdfEncryptedPayloadFileSpecFactory.GenerateFileDisplay(ep), encryptedDocument
                                            .GetName());
            if (password != null)
            {
                NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPath, cmpPath, destinationFolder, diff
                                                                                 , password, password));
            }
            else
            {
                RandomAccessFileOrArray raf = new RandomAccessFileOrArray(new RandomAccessSourceFactory().CreateBestSource
                                                                              (cmpPath));
                byte[] cmpBytes = new byte[(int)raf.Length()];
                raf.ReadFully(cmpBytes);
                raf.Close();
                NUnit.Framework.Assert.AreEqual(cmpBytes, encryptedDocumentBytes);
            }
        }
コード例 #2
0
        private void CreateWrapper(String encryptedName, String wrapperName, String cryptoFilter)
        {
            String      inPath   = sourceFolder + "cmp_" + encryptedName;
            String      cmpPath  = sourceFolder + "cmp_" + wrapperName;
            String      outPath  = destinationFolder + wrapperName;
            String      diff     = "diff_" + wrapperName + "_";
            PdfDocument document = new PdfDocument(new PdfWriter(outPath, new WriterProperties().SetPdfVersion(PdfVersion
                                                                                                               .PDF_2_0)));
            PdfFileSpec fs = PdfEncryptedPayloadFileSpecFactory.Create(document, inPath, new PdfEncryptedPayload(cryptoFilter
                                                                                                                 ));

            document.SetEncryptedPayload(fs);
            PdfFont   font   = PdfFontFactory.CreateFont();
            PdfCanvas canvas = new PdfCanvas(document.AddNewPage());

            canvas.SaveState().BeginText().MoveText(36, 750).SetFontAndSize(font, 30).ShowText("Hi! I'm wrapper document."
                                                                                               ).EndText().RestoreState();
            canvas.Release();
            document.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPath, cmpPath, destinationFolder, diff
                                                                             ));
        }