示例#1
0
        /// <summary>
        /// Returns a new <see cref="T:iTin.Utilities.Pdf.ComponentModel.OutputResult" /> reference thats represents a one <b>unique zip stream</b> that contains the same entries in <param ref="items"/>
        /// but compressed individually using the name in <param ref="filenames"/>.
        /// </summary>
        /// <param name="items">Items</param>
        /// <param name="filenames">Item filenames.</param>
        /// <returns>
        /// A <see cref="T:iTin.Core.ComponentModel.OutputResult" /> reference that contains action result.
        /// </returns>
        public static OutputResult CreateJoinResult(this IEnumerable <PdfInput> items, IEnumerable <string> filenames)
        {
            IList <PdfInput> elementList = items as IList <PdfInput> ?? items.ToList();

            SentinelHelper.ArgumentNull(elementList, nameof(elementList));

            IList <string> filenameList = filenames as IList <string> ?? filenames.ToList();

            SentinelHelper.ArgumentNull(filenameList, nameof(filenameList));

            try
            {
                Stream zippedStream = elementList.ToStreamEnumerable().AsZipStream(filenameList);
                zippedStream.Position = 0;
                return
                    (OutputResult.CreateSuccessResult(
                         new OutputResultData
                {
                    Zipped = true,
                    Configuration = null,
                    UncompressOutputStream = zippedStream
                }));
            }
            catch (Exception e)
            {
                return(OutputResult.FromException(e));
            }
        }
示例#2
0
        /// <summary>
        /// Merges all <see cref="XlsxInput"/> entries.
        /// </summary>
        /// <returns>
        /// <para>
        /// A <see cref="OutputResult"/> reference that contains the result of the operation, to check if the operation is correct, the <b>Success</b>
        /// property will be <b>true</b> and the <b>Value</b> property will contain the value; Otherwise, the the <b>Success</b> property
        /// will be false and the <b>Errors</b> property will contain the errors associated with the operation, if they have been filled in.
        /// </para>
        /// <para>
        /// The type of the return value is <see cref="OutputResultData"/>, which contains the operation result
        /// </para>
        /// </returns>
        public OutputResult TryMergeInputs()
        {
            Logger.Instance.Debug("");
            Logger.Instance.Debug(" Assembly: iTin.Utilities.Xlsx.Writer, Namespace: iTin.Utilities.Xlsx.Writer, Class: XlsxObject");
            Logger.Instance.Debug($" Merges all {typeof(XlsxInput)} entries into a new {typeof(XlsxObject)}");
            Logger.Instance.Debug($" > Signature: ({typeof(OutputResult)}) TryMergeInputs()");

            var items = Items.ToList();

            //if (Configuration.UseIndex)
            //{
            //    items = items.OrderBy(i => i.Index).ToList();
            //}

            try
            {
                MemoryStream outStream = new MemoryStream(); //MergeFiles(items.Select(item => item.ToStream()));

                if (Configuration.DeletePhysicalFilesAfterMerge)
                {
                    foreach (var item in items)
                    {
                        var inputType = item.InputType;
                        if (inputType != KnownInputType.Filename)
                        {
                            continue;
                        }

                        if (item.DeletePhysicalFilesAfterMerge)
                        {
                            File.Delete(TypeHelper.ToType <string>(item.Input));
                        }
                    }
                }

                var safeOutAsByteArray  = outStream.GetBuffer();
                var outputInMegaBytes   = (float)safeOutAsByteArray.Length / XlsxObjectConfig.OneMegaByte;
                var generateOutputAsZip = outputInMegaBytes > Configuration.CompressionThreshold;
                var zipped = Configuration.AllowCompression && generateOutputAsZip;

                return
                    (OutputResult.CreateSuccessResult(
                         new OutputResultData
                {
                    Zipped = zipped,
                    Configuration = Configuration,
                    UncompressOutputStream = safeOutAsByteArray.ToMemoryStream()
                }));
            }
            catch (Exception ex)
            {
                return(OutputResult.FromException(ex));
            }
        }
示例#3
0
        /// <summary>
        /// Returns a new reference <see cref="OutputResult"/> that complies with what is indicated in its configuration object. By default, this <see cref="XlsxInput"/> will not be zipped.
        /// </summary>
        /// <param name="config">The output result configuration.</param>
        /// <returns>
        /// <para>
        /// A <see cref="OutputResult"/> reference that contains the result of the operation, to check if the operation is correct, the <b>Success</b>
        /// property will be <b>true</b> and the <b>Result</b> property will contain the Result; Otherwise, the the <b>Success</b> property
        /// will be false and the <b>Errors</b> property will contain the errors associated with the operation, if they have been filled in.
        /// </para>
        /// <para>
        /// The type of the return Result is <see cref="OutputResultData"/>, which contains the operation result
        /// </para>
        /// </returns>
        public OutputResult CreateResult(OutputResultConfig config = null)
        {
            OutputResultConfig configToApply = OutputResultConfig.Default;

            if (config != null)
            {
                configToApply          = config;
                configToApply.Filename = NativeIO.Path.ChangeExtension(
                    string.IsNullOrEmpty(config.Filename)
                        ? File.GetUniqueTempRandomFile().Segments.LastOrDefault()
                        : config.Filename,
                    XlsxExtension);
            }

            try
            {
                if (configToApply.AutoFitColumns)
                {
                    Set(new SetAutoFitColumns());
                }

                Set(new SetSheetsSettings {
                    Settings = configToApply.GlobalSettings.SheetsSettings
                });
                Set(new SetDocumentSettings {
                    Settings = configToApply.GlobalSettings.DocumentSettings
                });

                if (!configToApply.Zipped)
                {
                    return(OutputResult.CreateSuccessResult(
                               new OutputResultData
                    {
                        Zipped = false,
                        Configuration = configToApply,
                        UncompressOutputStream = Clone().ToStream()
                    }));
                }

                OutputResult zippedOutputResult = OutputResult.CreateSuccessResult(null); // new[] { Clone() }.CreateJoinResult(new[] { configToApply.Filename });
                //zippedOutputResult.Result.Configuration = configToApply;

                return(zippedOutputResult);
            }
            catch (Exception e)
            {
                return(OutputResult.FromException(e));
            }
        }
示例#4
0
        /// <summary>
        /// Returns a new reference <see cref="OutputResult"/> that complies with what is indicated in its configuration object. By default, this <see cref="PdfInput"/> will not be zipped.
        /// </summary>
        /// <param name="config">The output result configuration.</param>
        /// <returns>
        /// <para>
        /// A <see cref="OutputResult"/> reference that contains the result of the operation, to check if the operation is correct, the <b>Success</b>
        /// property will be <b>true</b> and the <b>Result</b> property will contain the Result; Otherwise, the the <b>Success</b> property
        /// will be false and the <b>Errors</b> property will contain the errors associated with the operation, if they have been filled in.
        /// </para>
        /// <para>
        /// The type of the return Result is <see cref="OutputResultData"/>, which contains the operation result
        /// </para>
        /// </returns>
        public OutputResult CreateResult(OutputResultConfig config = null)
        {
            OutputResultConfig configToApply = OutputResultConfig.Default;

            if (config != null)
            {
                configToApply          = config;
                configToApply.Filename = NativeIO.Path.ChangeExtension(
                    string.IsNullOrEmpty(config.Filename)
                        ? iTinIO.File.GetUniqueTempRandomFile().Segments.LastOrDefault()
                        : config.Filename,
                    PdfExtension);
            }

            try
            {
                if (!configToApply.Zipped)
                {
                    return(OutputResult.CreateSuccessResult(
                               new OutputResultData
                    {
                        Zipped = false,
                        Configuration = configToApply,
                        UncompressOutputStream = Clone().ToStream()
                    }));
                }

                OutputResult zippedOutputResult = new[] { Clone() }.CreateJoinResult(new[] { configToApply.Filename });
                zippedOutputResult.Result.Configuration = configToApply;

                return(zippedOutputResult);
            }
            catch (Exception e)
            {
                return(OutputResult.FromException(e));
            }
        }
示例#5
0
        /// <summary>
        /// Merges all <see cref="PdfInput"/> entries.
        /// </summary>
        /// <returns>
        /// <para>
        /// A <see cref="OutputResult"/> reference that contains the result of the operation, to check if the operation is correct, the <b>Success</b>
        /// property will be <b>true</b> and the <b>Result</b> property will contain the Result; Otherwise, the the <b>Success</b> property
        /// will be false and the <b>Errors</b> property will contain the errors associated with the operation, if they have been filled in.
        /// </para>
        /// <para>
        /// The type of the return Result is <see cref="OutputResultData"/>, which contains the operation result
        /// </para>
        /// </returns>
        public OutputResult TryMergeInputs()
        {
            Logger.Instance.Debug("");
            Logger.Instance.Debug(" Assembly: iTin.Utilities.Pdf.Writer, Namespace: iTin.Utilities.Pdf.Writer, Class: PdfObject");
            Logger.Instance.Debug($" Merges all {typeof(PdfInput)} entries into a new {typeof(PdfObject)}");
            Logger.Instance.Debug($" > Signature: ({typeof(OutputResult)}) TryMergeInputs()");

            var items = Items.ToList();

            if (Configuration.UseIndex)
            {
                items = items.OrderBy(i => i.Index).ToList();
            }

            try
            {
                var outStream = new MemoryStream();
                using (var document = new Document())
                {
                    using (var copy = new PdfCopy(document, outStream))
                    {
                        document.Open();

                        PdfReader.unethicalreading = true;

                        foreach (var item in items)
                        {
                            var itemAsStream = item.Clone().ToStream();
                            if (itemAsStream == null)
                            {
                                continue;
                            }

                            itemAsStream.Position = 0;
                            copy.AddDocument(new PdfReader(itemAsStream));
                        }
                    }
                }

                bool hasSystemTags = false;
                if (Configuration.Tags.Any())
                {
                    hasSystemTags = true;

                    var pdfRawMerged = new PdfInput
                    {
                        AutoUpdateChanges = true,
                        Input             = outStream.GetBuffer().ToMemoryStream().Clone()
                    };

                    ReplaceResult rawMergedResult = null;
                    foreach (var tag in Configuration.Tags)
                    {
                        rawMergedResult = pdfRawMerged.Replace(new ReplaceSystemTag(tag.BuildReplacementObject()));
                    }

                    if (rawMergedResult.Success)
                    {
                        outStream = new MemoryStream((byte[])rawMergedResult.Result.OutputStream.AsByteArray().Clone());
                    }
                }

                bool hasGlobalReplacements = false;
                if (Configuration.GlobalReplacements.Any())
                {
                    hasGlobalReplacements = true;

                    var pdfRawMergedWithTags = new PdfInput
                    {
                        AutoUpdateChanges = true,
                        Input             = hasSystemTags ? outStream.ToMemoryStream().Clone() : outStream.GetBuffer().ToMemoryStream().Clone()
                    };

                    ReplaceResult rawMergedWithTagsResult = null;
                    foreach (var replacement in Configuration.GlobalReplacements)
                    {
                        rawMergedWithTagsResult = pdfRawMergedWithTags.Replace(new ReplaceText(replacement));
                    }

                    if (rawMergedWithTagsResult.Success)
                    {
                        if (rawMergedWithTagsResult.Result.OutputStream.Position != 0)
                        {
                            rawMergedWithTagsResult.Result.OutputStream.Position = 0;
                        }

                        outStream = new MemoryStream((byte[])rawMergedWithTagsResult.Result.OutputStream.AsByteArray().Clone());
                    }
                }

                if (Configuration.DeletePhysicalFilesAfterMerge)
                {
                    foreach (var item in items)
                    {
                        var inputType = item.InputType;
                        if (inputType != KnownInputType.Filename)
                        {
                            continue;
                        }

                        if (item.DeletePhysicalFilesAfterMerge)
                        {
                            File.Delete(TypeHelper.ToType <string>(item.Input));
                        }
                    }
                }

                var safeOutAsByteArray  = (hasSystemTags || hasGlobalReplacements) ? outStream.AsByteArray() : outStream.GetBuffer();
                var outputInMegaBytes   = (float)safeOutAsByteArray.Length / PdfObjectConfig.OneMegaByte;
                var generateOutputAsZip = outputInMegaBytes > Configuration.CompressionThreshold;
                var zipped = Configuration.AllowCompression && generateOutputAsZip;

                return
                    (OutputResult.CreateSuccessResult(
                         new OutputResultData
                {
                    Zipped = zipped,
                    Configuration = Configuration,
                    UncompressOutputStream = safeOutAsByteArray.ToMemoryStream()
                }));
            }
            catch (Exception ex)
            {
                return(OutputResult.FromException(ex));
            }
        }