예제 #1
0
 private static byte[] GetBytes(GemBox.Document.DocumentModel document, SaveOptions options)
 {
     using (MemoryStream stream = new MemoryStream())
     {
         document.Save(stream, options);
         return(stream.ToArray());
     }
 }
예제 #2
0
        public async Task <IActionResult> OnPostGetSummaryOfUploadedFilesAndChecksAsync(Guid departmentsDocumentId)
        {
            try
            {
                List <SummaryOfUploadedFilesAndChecks> summary = new List <SummaryOfUploadedFilesAndChecks>();

                var DocumentStatusHistories = db.DocumentStatusHistory
                                              .Where(dsh => dsh.DepartmentsDocumentId == departmentsDocumentId)
                                              .Include(dsh => dsh.User);

                // Adding all statuses and time of their setting
                foreach (var documentStatusHistory in DocumentStatusHistories)
                {
                    var status = await db.DocumentStatus.FirstOrDefaultAsync(s => s.Id == documentStatusHistory.DocumentStatusId);

                    if (status != null)
                    {
                        summary.Add(new SummaryOfUploadedFilesAndChecks("Установлен статус " + status.Status,
                                                                        documentStatusHistory.SettingDateTime,
                                                                        documentStatusHistory.User.Email + " (" + documentStatusHistory.User.FistName + " " + documentStatusHistory.User.LastName + ")"));
                    }
                }

                var UploadedDocuments = db.DepartmentsDocumentsVersion
                                        .Where(dsh => dsh.DepartmentDocumentId == departmentsDocumentId)
                                        .Include(ddv => ddv.User);

                // Adding all uploads and time of their upload
                foreach (DepartmentsDocumentsVersion departmentsDocumentsVersion in UploadedDocuments)
                {
                    if (departmentsDocumentsVersion != null)
                    {
                        summary.Add(
                            new SummaryOfUploadedFilesAndChecks(
                                "Загружен документ " + departmentsDocumentsVersion.FileName,
                                departmentsDocumentsVersion.UploadedDateTime,
                                departmentsDocumentsVersion.User.Email + " (" + departmentsDocumentsVersion.User.FistName + " " + departmentsDocumentsVersion.User.LastName + ")"));
                    }
                }


                ComponentInfo.SetLicense("FREE-LIMITED-KEY");

                GemBox.Document.DocumentModel document = new GemBox.Document.DocumentModel();
                Section section = new Section(document);
                document.Sections.Add(section);

                var DepartmentDocument = (await db.DepartmentsDocument
                                          .Include(rydt => rydt.Department)
                                          .Include(dd => dd.ReportingYearDocumentTitle)
                                          .ThenInclude(rydt => rydt.DocumentTitle)
                                          .ThenInclude(dt => dt.DocumentType)
                                          .Include(dd => dd.ReportingYearDocumentTitle)
                                          .ThenInclude(dd => dd.ReportingYear)
                                          .FirstOrDefaultAsync(dd => dd.Id == departmentsDocumentId));

                section.Blocks.Add(
                    new Paragraph(document,
                                  new Run(document,
                                          "Сводка загрузок и проверок по файлу "
                                          + DepartmentDocument.ReportingYearDocumentTitle.ReportingYear.Number + "/"
                                          + DepartmentDocument.Department.Name + "/"
                                          + DepartmentDocument.ReportingYearDocumentTitle.DocumentTitle.DocumentType.Type + "/"
                                          + DepartmentDocument.ReportingYearDocumentTitle.DocumentTitle.Name)
                {
                    CharacterFormat = { Bold = true, Size = 24 }
                }));
                foreach (var item in summary.OrderBy(s => s.DateTime))
                {
                    section.Blocks.Add(
                        new Paragraph(document,
                                      new Run(document, item.Info + " | " + item.DateTime + " " + item.UploadedUser)));
                }


                SaveOptions options = SaveOptions.DocxDefault;

                return(File(GetBytes(document, options), options.ContentType, "SummaryOfDownloadedFilesAndChecks.docx"));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error while saving document");
                return(NotFound());
            }
        }