Ejemplo n.º 1
0
        protected override void EndProcessing()
        {
            var book = new SCBookContext();

            if (!string.IsNullOrWhiteSpace(FileName))
            {
                var fileName = Project.Current.MapPath(FileName);

                var ext = Path.GetExtension(fileName)?.ToLower();
                if (string.Compare(ext, ".pdf", true) == 0) //When loading PDF - extract text.
                {
                    string documentText;

                    using (var documentProcessor = new PdfDocumentProcessor())
                    {
                        ExecuteLocked(() => documentProcessor.LoadDocument(fileName), LockFiles ? LockObject : null);
                        documentText = documentProcessor.Text;
                    }

                    book.Document.AppendText(documentText);
                }
                else
                {
                    ExecuteLocked(() => book.Document.LoadDocument(fileName), LockFiles ? LockObject : null);
                }
            }

            WriteObject(book);
        }
        protected void WriteTable(Document book)
        {
            var dataSource = GetDataSource(_Output, DataSource, IgnoreErrors);

            if (string.IsNullOrWhiteSpace(TemplateFile))
            {
                throw new Exception("Template file is not specified.");
            }

            var templateFile = Project.Current.MapPath(TemplateFile);

            if (!File.Exists(templateFile))
            {
                throw new Exception($"Template file '{TemplateFile}' does not exist.");
            }

            MemoryStream documentData;

            using (var template = SCBook.NewSnapSCBook())
            {
                ExecuteLocked(() => template.Document.LoadDocument(templateFile), LockFiles ? LockObject : null);

                var mergeOptions = (template.BookServer as SnapDocumentServer).CreateSnapMailMergeExportOptions();
                mergeOptions.DataSource                 = dataSource;
                mergeOptions.DataMember                 = null;
                mergeOptions.CopyTemplateStyles         = CopyTemplateStyles;
                mergeOptions.HeaderFooterLinkToPrevious = !DoNotLinkHeaderFooter;
                if (NumberedListRestart.HasValue)
                {
                    mergeOptions.NumberedListRestart = NumberedListRestart.Value;
                }

                var snapDocument = template.Document as SnapDocument;
                snapDocument.Parameters.Clear();
                if (Parameters != null)
                {
                    foreach (DictionaryEntry keyValuePair in Parameters)
                    {
                        var parameter = new Parameter()
                        {
                            Name  = Convert.ToString(keyValuePair.Key),
                            Value = keyValuePair.Value,
                            Type  = keyValuePair.Value?.GetType() ?? typeof(string)
                        };
                        snapDocument.Parameters.Add(parameter);
                    }
                }

                documentData = new MemoryStream();
                snapDocument.SnapMailMerge(mergeOptions, documentData, DocumentFormat.OpenXml);
                documentData.Seek(0, SeekOrigin.Begin);
            }

            ExecuteSynchronized(() => DoWriteBook(book, documentData));

            if (OutputBook)
            {
                documentData.Seek(0, SeekOrigin.Begin);

                var outBook = new SCBookContext();
                outBook.Document.LoadDocument(documentData, DocumentFormat.OpenXml);

                WriteObject(outBook);
            }
        }