コード例 #1
0
        public void CheckIfDocumentWasScannedAndFixIfNecessary_File_DocumentWasFixed()
        {
            string pdfPath = Directory.GetCurrentDirectory() + "/BlackRectangles4.pdf";

            PdfProcessorTests.FlushResourceToFile(Resources + "BlackRectangles.pdf", pdfPath);
            DateTime lastWrite = File.GetLastWriteTime(pdfPath);

            target.CheckIfDocumentWasScannedAndFixIfNecessary(pdfPath);
            bool wasModified = lastWrite != File.GetLastWriteTime(pdfPath);

            Assert.IsTrue(wasModified); // file was modified
            using (var reader = new PdfReader(pdfPath))
            {
                AssertPageRotation(reader, 1, 0);
                AssertOnlyImageAndImageSize(reader, 1, 1275, 1650);
                AssertPageRotation(reader, 2, 90);
                AssertOnlyImageAndImageSize(reader, 2, 1275, 1650);
                AssertPageRotation(reader, 3, 180);
                AssertOnlyImageAndImageSize(reader, 3, 1275, 1650);
            }
        }
コード例 #2
0
        public bool ConvertIfNotExist(FileDTO fileDTO, string failedFolder, string connectionString, byte[] ms = null)
        {
            try
            {
                var fileStoragePhysicalPath = (string)FileModel.FileStoragePhysicalPath(this._settings);
                EnsureDirectoryExist(fileStoragePhysicalPath);
                var fileId        = fileDTO.fileId;
                int numberOfPages = fileDTO.numberOfPages;
                var workingDir    = Path.Combine(fileStoragePhysicalPath, fileId.ToString());
                EnsureDirectoryExist(workingDir);
                var pdfFile      = Path.Combine(workingDir, "document.pdf");
                var swfFile      = Path.Combine(workingDir, "document.swf");
                var swfPagedFile = Path.Combine(workingDir, "%.swf");

                if (!System.IO.File.Exists(pdfFile) && ms != null)
                {
                    System.IO.File.WriteAllBytes(pdfFile, ms);
                }

                if (!System.IO.File.Exists(pdfFile))
                {
                    return(false);
                }

                if (System.IO.File.Exists(swfFile) && new FileInfo(pdfFile).LastWriteTime < new FileInfo(swfFile).LastWriteTime)
                {
                    return(true);
                }

                SafelyDeleteSwfFile(swfFile);

                string baseArgs = ((string)_settings.SWFToolsCommandPattern).Replace("{pdfFile}", pdfFile);

                var logger = _logger;

                Task.Factory.StartNew(
                    () =>
                {
                    UpdateFileWithStatus(connectionString, new Guid(fileId), UploadFileStatus.Rendering, logger);
                    try
                    {
                        _pdfProcessor.CheckIfDocumentWasScannedAndFixIfNecessary(pdfFile);
                    }
                    catch (Exception ex)
                    {
                        CopyFileToFailedFolder(connectionString, failedFolder, pdfFile, new Guid(fileId), logger, "_rendering");
                    }

                    UpdateFileWithStatus(connectionString, new Guid(fileId), UploadFileStatus.Converting, logger);
                    bool renderAsBitmap  = false;
                    bool overWriteSource = false;
                    bool bigPageSucceded = this.Convert(baseArgs.Replace("{swfFile}", swfFile), pdfFile, ref renderAsBitmap, ref overWriteSource, () => System.IO.File.Exists(swfFile), (fail) => this.CopyFileToFailedFolder(connectionString, failedFolder, pdfFile, new Guid(fileId), logger, "_converting", UploadFileStatus.ConvertingFailed, fail));

                    if (bigPageSucceded)
                    {
                        // convert page files
                        UpdateFileWithStatus(connectionString, new Guid(fileId), UploadFileStatus.Converted, logger);

                        Convert(baseArgs.Replace("{swfFile}", swfPagedFile), pdfFile, ref renderAsBitmap, ref overWriteSource, () => this.CheckIfSwfPagedFilesExist(swfPagedFile, numberOfPages), (fail) => this.CopyFileToFailedFolder(connectionString, failedFolder, pdfFile, new Guid(fileId), logger, "_pages", UploadFileStatus.ConvertingPagesFailed, fail));
                    }
                });

                return(true);
            }
            catch (Exception ex)
            {
                _logger.Error("Converting error", ex);
                return(false);
            }
        }
コード例 #3
0
        /// <summary>
        /// The convert if not exist.
        /// </summary>
        /// <param name="fileDTO">
        /// The file DTO.
        /// </param>
        /// <param name="ms">
        /// The memory stream.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public bool ConvertIfNotExist(FileDTO fileDTO, string failedFolder, string connectionString, byte[] ms = null)
        {
            try
            {
                var fileStoragePhysicalPath = (string)FileModel.FileStoragePhysicalPath(this.settings);
                var permSWFPattern          = (string)this.settings.PermSWFPattern;
                var permSWFPagePattern      = (string)this.settings.PermSWFPagePattern;
                this.EnsureDirectoryExist(fileStoragePhysicalPath);
                var fileId        = fileDTO.fileId;
                int numberOfPages = fileDTO.numberOfPages;
                var workingDir    = Path.Combine(fileStoragePhysicalPath, fileId.ToString());
                this.EnsureDirectoryExist(workingDir);
                var  pdfFile        = Path.Combine(workingDir, "document.pdf");
                var  swfFile        = Path.Combine(workingDir, "document.swf");
                var  swfPagedFile   = Path.Combine(workingDir, "%.swf");
                bool isDebugEnabled = this.logger.IsDebugEnabled;

                if (!System.IO.File.Exists(pdfFile) && ms != null)
                {
                    System.IO.File.WriteAllBytes(pdfFile, ms);
                }

                if (!System.IO.File.Exists(pdfFile))
                {
                    return(false);
                }

                if (System.IO.File.Exists(swfFile) && new FileInfo(pdfFile).LastWriteTime < new FileInfo(swfFile).LastWriteTime)
                {
                    return(true);
                }

                this.SafelyDeleteSwfFile(swfFile);

                string baseArgs = ((string)this.settings.SWFToolsCommandPattern).Replace("{pdfFile}", pdfFile);

                var logger = this.logger;

                Task.Factory.StartNew(
                    () =>
                {
                    var s = new Stopwatch();
                    if (isDebugEnabled)
                    {
                        logger.Debug("Save SWF pages in background");
                        s.Start();
                    }

                    this.UpdateFileWithStatus(connectionString, fileId, UploadFileStatus.Rendering, logger);
                    try
                    {
                        pdfProcessor.CheckIfDocumentWasScannedAndFixIfNecessary(pdfFile);
                    }
                    catch (Exception ex)
                    {
                        this.CopyFileToFailedFolder(connectionString, failedFolder, pdfFile, fileId, logger, "_rendering");
                    }


                    if (isDebugEnabled)
                    {
                        logger.DebugFormat("Process images PDF took {0}s", s.ElapsedMilliseconds / 1000.0);
                        s.Restart();
                    }

                    this.UpdateFileWithStatus(connectionString, fileId, UploadFileStatus.Converting, logger);
                    bool renderAsBitmap  = false;
                    bool overWriteSource = false;
                    bool bigPageSucceded = this.Convert(baseArgs.Replace("{swfFile}", swfFile), pdfFile, ref renderAsBitmap, ref overWriteSource, () => System.IO.File.Exists(swfFile), (fail) => this.CopyFileToFailedFolder(connectionString, failedFolder, pdfFile, fileId, logger, "_converting", UploadFileStatus.ConvertingFailed, fail));
                    if (isDebugEnabled)
                    {
                        logger.DebugFormat("Convert SWF took {0}s", s.ElapsedMilliseconds / 1000.0);
                        s.Restart();
                    }

                    if (bigPageSucceded)
                    {
                        // convert page files
                        this.UpdateFileWithStatus(connectionString, fileId, UploadFileStatus.Converted, logger);

                        this.Convert(baseArgs.Replace("{swfFile}", swfPagedFile), pdfFile, ref renderAsBitmap, ref overWriteSource, () => this.CheckIfSwfPagedFilesExist(swfPagedFile, numberOfPages), (fail) => this.CopyFileToFailedFolder(connectionString, failedFolder, pdfFile, fileId, logger, "_pages", UploadFileStatus.ConvertingPagesFailed, fail));
                        if (isDebugEnabled)
                        {
                            logger.DebugFormat("Convert SWF pages took {0}s", s.ElapsedMilliseconds / 1000.0);
                        }
                    }
                });

                return(true);
            }
            catch (Exception ex)
            {
                this.logger.Error("Converting error", ex);
                return(false);
            }
        }