/// <summary> /// Create a new object that is a copy of the current instance. /// </summary> /// <returns> /// A new <see cref="PdfInput"/> that is a copy of this instance. /// </returns> public PdfInput Clone() { Logger.Instance.Debug(""); Logger.Instance.Debug(" Assembly: iTin.Utilities.Pdf, Namespace: iTin.Utilities.Pdf.ComponentModel, Class: PdfInput"); Logger.Instance.Debug(" Create a new object that is a copy of the current instance"); Logger.Instance.Debug($" > Signature: ({typeof(PdfInput)}) Clone()"); PdfInput clonned = (PdfInput)MemberwiseClone(); NativeIO.Stream innerStream = ToStream().Clone(); clonned.Input = innerStream; Logger.Instance.Debug($" > Output: Cloned correctly"); return(clonned); }
/// <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)); } }