Example #1
0
        /// <summary>
        /// Updates the file process.
        /// </summary>
        /// <param name="file">The file.</param>
        public static void UpdateFileProcess(IFileProcess file)
        {
            if (file != null && (!file.IsNewVersion || string.IsNullOrEmpty(file.FileName)))
                return;

            var sysOptions = SystemOptionsInfo.GetSystemOptionsInfo();
            if (sysOptions == null)
                return;

            if (file == null)
                return;

            var newFileName = Guid.NewGuid() + Path.GetExtension(file.FileName);

            UpdateFileProcess(file.FileName, newFileName, sysOptions.FileProcessorURI);

            if (file.IsReadyForPdf())
            {
                string newFileNameWithPdfExtension = FileHelper.GetNewFileNameWithPdfExtension(newFileName);
                UpdateFileProcess(file.ConvertedFileName, newFileNameWithPdfExtension, sysOptions.FileProcessorURI);
            }

            file.FileName = newFileName;
        }
Example #2
0
        public void UpdatePdf(IEditableRoot item, IFileProcess file)
        {
            if (file == null || item == null || string.IsNullOrEmpty(file.FileName)) return;

            var sysOptions = SystemOptionsInfo.GetSystemOptionsInfo();
            if (sysOptions == null)
                return;

            if (file.UseReport.HasValue && file.UseReport.Value)
            {
                GeneratePdfFromReport(item, file, sysOptions);
                return;
            }

            if (!file.IsReadyForPdf())
            {
                return;
            }

            if (!file.IsPdfWithReport())
            {
                return;
            }

            DownloadDocument(
                file.FileName,
                sysOptions,
                fileStream =>
                {
                    if (fileStream == null || fileStream.Length == 0)
                        return;

                    try
                    {
                        var isLandscape = false;

                        using (var documentStream = new MemoryStream())
                        {
                            if (!SavePdfToStream(file.FileName, fileStream, documentStream, ref isLandscape)) return;

                            //watermarks
                            using (var pdfDoc = new Aspose.Pdf.Document(documentStream))
                            {
                                if (pdfDoc.Pages.Count == 0)
                                    return;

                                var watermarkReport = isLandscape ? file.WatermarkLandscapeReportName : file.WatermarkPortraitReportName;
                                if (!string.IsNullOrEmpty(watermarkReport))
                                {
                                    var ms = GetReportStream(item, watermarkReport);

                                    var watermarkPdf = new Aspose.Pdf.Document(ms);
                                    if (watermarkPdf.Pages.Count > 0)
                                    {
                                        var pageStamp = new Aspose.Pdf.PdfPageStamp(watermarkPdf.Pages[1]) { Background = false, Opacity = 0.0 };

                                        var page = (file.FirstPageWatermark.HasValue && file.FirstPageWatermark.Value) ? 1 : 2;
                                        for (var i = page; i <= pdfDoc.Pages.Count; i++)
                                            pdfDoc.Pages[i].AddStamp(pageStamp);
                                    }
                                }

                                //cover page
                                var coverPageReport = isLandscape ? file.CoverPageLandscapeReportName : file.CoverPagePortraitReportName;
                                if (!string.IsNullOrEmpty(coverPageReport))
                                {
                                    var ms = GetReportStream(item, coverPageReport);

                                    var coverPagePdf = new Aspose.Pdf.Document(ms);
                                    pdfDoc.Pages.Insert(1, coverPagePdf.Pages);
                                }

                                //Appendix
                                var appendixReport = isLandscape ? file.AppendixLandscapeReportName : file.AppendixPortraitReportName;
                                if (!string.IsNullOrEmpty(appendixReport))
                                {
                                    var ms = GetReportStream(item, appendixReport);

                                    var appendixePdf = new Aspose.Pdf.Document(ms);
                                    pdfDoc.Pages.Insert(pdfDoc.Pages.Count + 1, appendixePdf.Pages);
                                }

                                var outputStream = new MemoryStream();
                                pdfDoc.Save(outputStream);

								var newFileNameWithPdfExtension = string.IsNullOrEmpty(file.ConvertedFileName) ?
									FileHelper.GetNewFileNameWithPdfExtension(file.FileName) :
									file.ConvertedFileName;

								UploadFile(newFileNameWithPdfExtension, outputStream, sysOptions);

                                if (file.ConvertedFileName != newFileNameWithPdfExtension)
                                {
                                    file.ConvertedFileName = newFileNameWithPdfExtension;
                                    ((ISavable)file).Save();
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Logger.Log(LogSeverity.Error, "FileManager.UpdatePdf", e.ToString());
                    }
                });
        }