/// <summary>
        /// Merges a row tree with a PDF document containing form fields.
        /// </summary>
        public static void CreatePdf(
            MergeRowTree rowTree, bool ensureAllFieldsHaveValues, MemoryStream sourcePdfStream, Stream destinationStream,
            bool useLegacyBehaviorOfIgnoringInvalidFields = false)
        {
            var streams = new List<Stream>();
            try {
                foreach( var row in rowTree.Rows ) {
                    var stream = new MemoryStream();
                    streams.Add( stream );

                    using( var sourcePdfMemoryStreamCopy = new MemoryStream() ) {
                        // Aspose has decided that in the new Facades PDF library, they will close your source stream for you when you call doc.Save.
                        sourcePdfStream.Reset();
                        sourcePdfStream.CopyTo( sourcePdfMemoryStreamCopy );

                        var doc = new Aspose.Pdf.Facades.Form( sourcePdfMemoryStreamCopy );
                        foreach( var mergeField in doc.FieldNames.Where( mergeField => !mergeField.StartsWith( "noMerge" ) ) ) {
                            var mergeValue = row.Values.SingleOrDefault( v => v.Name == mergeField );
                            if( mergeValue == null ) {
                                if( useLegacyBehaviorOfIgnoringInvalidFields )
                                    continue;
                                throw new MailMergingException( string.Format( "PDF document contains a merge field ({0}) that does not exist.", mergeField ) );
                            }

                            var mergeValueAsString = mergeValue as MergeValue<string>;
                            string value = null;
                            if( mergeValueAsString != null )
                                value = mergeValueAsString.Evaluate( ensureAllFieldsHaveValues );
                            if( value == null )
                                throw new MailMergingException( "Merge field " + mergeValue.Name + " evaluates to an unsupported type." );

                            doc.FillField( mergeValue.Name, value );
                        }
                        doc.Save( stream );
                    }
                }

                if( streams.Any() )
                    PdfOps.ConcatPdfs( streams, destinationStream );
            }
            finally {
                foreach( var i in streams )
                    i.Dispose();
            }
        }
        public static string Decode(string s, bool convertPlus, Encoding charset)
        {
            if (s.IndexOf('%') == -1 && (!convertPlus || s.IndexOf('+') == -1)) {
                return s;
            }

            StringBuilder result = new StringBuilder(s.Length);
            MemoryStream @out = new MemoryStream();
            for (int i = 0; i < s.Length;) {
                char c = s[i];
                if (c == '%') {
                    do {
                        if (i + 2 >= s.Length) {
                            throw new ArgumentException("Incomplete % sequence at: " + i);
                        }
                        int d1 = HexToInt(s[i + 1]);
                        int d2 = HexToInt(s[i + 2]);
                        if (d1 == -1 || d2 == -1) {
                            throw new ArgumentException("Invalid % sequence " + s.Substring(i, 3) + " at " + i);
                        }
                        @out.WriteByte(unchecked((byte)((d1 << 4) + d2)));
                        i += 3;
                    } while (i < s.Length && s[i] == '%');
                    result.Append(charset.GetString(@out.ToArray()));
                    @out.Reset();
                }
                else {
                    if (convertPlus && c == '+') {
                        c = ' ';
                    }
                    result.Append(c);
                    i++;
                }
            }

            return result.ToString();
        }
示例#3
0
        /// <summary>
        /// Stores item manifest in zip package's comments
        /// </summary>
        private void UpdateItemManifest()
        {
            if (this.TempZipFile)
                throw new ApplicationException("ZipPackage is read-only when reading from temp directory");

            if (this.ItemManifest.Items.Count == 0)
                throw new ApplicationException("No items to add to package manifest");

            var manifestXml = XmlSerializer.SerializeXml(this.ItemManifest);

            using (var ms = new MemoryStream())
            {
                using (ZipFile zip = this.Items.Length == 0 ? new ZipFile() : ZipFile.Read(this.Items.Reset()))
                {
                    if (zip.ContainsEntry("manifest.xml"))
                        zip.UpdateEntry("manifest.xml", XmlSerializer.StringToUTF8ByteArray(manifestXml));
                    else
                        zip.AddEntry("manifest.xml", XmlSerializer.StringToUTF8ByteArray(manifestXml));

                    zip.Save(ms);
                }

                this.Items = new MemoryStream();
                ms.Reset().CopyTo(this.Items);
            }
        }
示例#4
0
        /// <summary>
        /// Returns an open stream from Items for the named item
        /// </summary>
        /// <param name="itemName"></param>
        /// <returns></returns>
        public Stream Extract(string itemName)
        {
            Stream ms = new MemoryStream();

            if (this.TempZipFile)
            {
                using (var file = new FileStream(this.TempZipFilePath, FileMode.Open))
                {
                    file.CopyTo(ms);
                }
            }
            else
            {
                using (ZipFile zip = ZipFile.Read(this.Items.Reset()))
                {
                    ZipEntry entry = zip[itemName];
                    if (entry != null)
                        entry.Extract(ms);
                }
            }

            return ms.Reset();
        }
示例#5
0
        /// <summary>
        /// Adds an item to Items
        /// </summary>
        /// <param name="itemType"></param>
        /// <param name="item"></param>
        /// <param name="itemName"></param>
        /// <returns></returns>
        public void Add(ZipPackageItemType itemType, string itemName, Stream item)
        {
            if (this.TempZipFile)
                throw new ApplicationException("ZipPackage is read-only when reading from temp directory");

            using (var ms = new MemoryStream())
            {
                using (ZipFile zip = this.Items.Length == 0 ? new ZipFile() : ZipFile.Read(this.Items.Reset()))
                {
                    zip.AddEntry(itemName, item.Reset());
                    zip.Save(ms);
                }

                this.Items = new MemoryStream();
                ms.Reset().CopyTo(this.Items);
            }

            this.ItemManifest.Items.Add(new OfflineZipPackageItem<string, ZipPackageItemType>(itemName, itemType));

            this.UpdateItemManifest();
        }
        internal static void Test()
        {
            // NOTE: This path is probably wrong, and should not be hard-coded.
            const string sourceFolderPath = @"C:\Red Stapler Vault\Supporting Files\Standard Library\Standard Library\MailMerging";

            var outputFolderPath = TestStatics.OutputFolderPath;
            IoMethods.DeleteFolder( outputFolderPath );

            // Create and extract empty zip file
            var emptyFolderPath = EwlStatics.CombinePaths( outputFolderPath, "Empty" );
            Directory.CreateDirectory( emptyFolderPath );
            var emptyZipPath = EwlStatics.CombinePaths( outputFolderPath, "empty.zip" );
            ZipFolderAsFile( emptyFolderPath, emptyZipPath );
            using( var memoryStream = new MemoryStream() ) {
                ZipFolderAsStream( emptyFolderPath, memoryStream );
                memoryStream.Reset();
                UnZipStreamIntoFolder( memoryStream, EwlStatics.CombinePaths( outputFolderPath, "Empty from stream" ) );
            }

            // File-based
            var zipFilePath = EwlStatics.CombinePaths( outputFolderPath, "file.zip" );
            var extractedPath = EwlStatics.CombinePaths( outputFolderPath, "Extracted from File" );
            ZipFolderAsFile( sourceFolderPath, zipFilePath );
            UnZipFileAsFolder( zipFilePath, extractedPath );

            // Byte-array-based
            var bytes = ZipFolderAsByteArray( sourceFolderPath );
            var byteZipFilePath = EwlStatics.CombinePaths( outputFolderPath, "fileFromBytes.zip" );
            File.WriteAllBytes( byteZipFilePath, bytes );
            UnZipByteArrayAsFolder( File.ReadAllBytes( byteZipFilePath ), EwlStatics.CombinePaths( outputFolderPath, "Extracted from Byte Array" ) );

            // Stream-based
            var streamedFilePath = EwlStatics.CombinePaths( outputFolderPath, "fileFromStream.zip" );
            using( var fs = new FileStream( streamedFilePath, FileMode.Create ) )
                ZipFolderAsStream( sourceFolderPath, fs );

            var streamedExtractedPath = EwlStatics.CombinePaths( outputFolderPath, "Extracted from Stream" );
            using( var memoryStream = new MemoryStream() ) {
                ZipFolderAsStream( sourceFolderPath, memoryStream );
                memoryStream.Reset();
                UnZipStreamIntoFolder( memoryStream, streamedExtractedPath );
            }

            using( var fs = File.OpenRead( streamedFilePath ) ) {
                var files = UnZipStreamAsFileObjects( fs );
                ZipFileObjectsAsStream( files, File.OpenWrite( EwlStatics.CombinePaths( outputFolderPath, "fileFromStreamedFileObjects.zip" ) ) );
            }
        }
示例#7
0
        static void Main(string[] args)
        {
            {
                var ms = new MemoryStream();
                var t1 = new Packtest();
                ms.Reset();
                t1.Field9 = null;
                t1.Pack(ms);
                ms.Reset();
                t1.Unpack(ms);
            }
            return;

            var test1 = new SpeedTest(delegate()
            {
                var ms = new MemoryStream();
                var t1 = new Packtest();
                ms.Reset();
                t1.Pack(ms);
                ms.Reset();
                t1.Unpack(ms);
            });

            var test2 = new SpeedTest(delegate()
            {
                var ms = new MemoryStream();
                var t2 = new Packtest2();
                ms.Reset();
                t2.Pack(ms);
                ms.Reset();
                t2.Unpack(ms);
            });

            test2.Test();
            test1.Test();

            Console.WriteLine("My serialize class");
            Console.WriteLine("Min: {0}ms", test1.Min);
            Console.WriteLine("Avg: {0}ms", test1.Average);
            Console.WriteLine("Max: {0}ms", test1.Max);
            Console.WriteLine();
            Console.WriteLine("Binaryreader/writer");
            Console.WriteLine("Min: {0}ms", test2.Min);
            Console.WriteLine("Avg: {0}ms", test2.Average);
            Console.WriteLine("Max: {0}ms", test2.Max);

            Console.ReadKey();
        }