예제 #1
0
 protected override void DoRunInitialize(Ghostscript ghostscript)
 {
     // run the prolog if there is any
     if (_prologAndSetup != null)
     {
         ghostscript.Run(_prologAndSetup);
     }
 }
예제 #2
0
        private string[] ExtractToMultipleFiles(Work work, Func <bool> isCanelled, Action <int, object> reportStatus)
        {
            // extract all selected pages
            var fileNames = new string[work.Indices.Length];
            var progress  = 0;

            for (var i = 0; i < work.Indices.Length; i++)
            {
                // return if canceled
                if (isCanelled())
                {
                    if (i > 0)
                    {
                        Array.Resize(ref fileNames, i);
                        return(fileNames);
                    }
                    else
                    {
                        return(null);
                    }
                }

                // get the current index and file name
                var index    = work.Indices[i];
                var fileName = GetDocumentFileName(index, work.Format.FileExtension);
                var filePath = Path.Combine(work.Path, fileName);
                reportStatus(progress, fileName);

                // extract and save the page
                if (work.Format == ConvertFormat.Pdf)
                {
                    using (var onePageDocument = new PdfDocument())
                    {
                        onePageDocument.AddPage(_document.Pages[index]);
                        onePageDocument.Save(filePath);
                    }
                }
                else
                {
                    lock (_renderDocument)
                    {
                        _renderDocument.EnsureNoOpenRenderer();
                        using (var ghostscript = new Ghostscript(work.Format.GetArguments(filePath)))
                        {
                            _renderDocument.RunInitialize(ghostscript);
                            _renderDocument.RunPage(ghostscript, index + 1);
                        }
                    }
                }

                // store the file name and continue
                fileNames[i] = fileName;
                progress     = (int)Math.Round((100.0 * (i + 1)) / work.Indices.Length);
                reportStatus(progress, string.Empty);
            }
            return(fileNames);
        }
예제 #3
0
        public void RunInitialize(Ghostscript ghostscript)
        {
            // check the arguments and state
            if (ghostscript == null)
            {
                throw new ArgumentNullException(nameof(ghostscript));
            }

            // perform the action
            lock (this)
            {
                CheckDisposed();
                DoRunInitialize(ghostscript);
            }
        }
예제 #4
0
        public void RunPage(Ghostscript ghostscript, int pageNumber)
        {
            // check the arguments and state
            if (ghostscript == null)
            {
                throw new ArgumentNullException(nameof(ghostscript));
            }
            if (pageNumber < 1 || pageNumber > PageCount)
            {
                throw new ArgumentOutOfRangeException(nameof(pageNumber));
            }

            // perform the action
            lock (this)
            {
                CheckDisposed();
                DoRunPage(ghostscript, pageNumber);
            }
        }
예제 #5
0
            protected override void DoRunInitialize(Ghostscript ghostscript)
            {
                // escape the path
                var escapedFileName = new StringBuilder(FilePath);

                for (var i = escapedFileName.Length - 1; i >= 0; i--)
                {
                    switch (escapedFileName[i])
                    {
                    case '(':
                    case ')':
                    case '\\':
                    {
                        escapedFileName.Insert(i, '\\');
                        break;
                    }
                    }
                }

                // load the PDF
                ghostscript.Run(string.Format(CultureInfo.InvariantCulture, "({0}) (r) file runpdfbegin process_trailer_attrs", escapedFileName));
            }
예제 #6
0
            protected override void DoConvert(ConvertFormat format, Action pageCompletedCallback, Func <bool> cancellationCallback)
            {
                // convert the file using Ghostscript
                var outputFile = format.SupportsSingleFile ? Path.ChangeExtension(FilePath, format.FileExtension) : Path.Combine(Path.GetDirectoryName(FilePath), new StringBuilder(Path.GetFileNameWithoutExtension(FilePath)).Append("_%d.").Append(format.FileExtension).ToString());

                using (var ghostscript = new Ghostscript(format.GetArguments(outputFile)))
                {
                    if (cancellationCallback != null)
                    {
                        ghostscript.Poll += (s, e) => e.Cancel = cancellationCallback();
                    }
                    if (format == ConvertFormat.Pdf)
                    {
                        ghostscript.Run(".setpdfwrite");
                    }
                    DoRunInitialize(ghostscript);
                    for (var i = 0; i < PageCount; i++)
                    {
                        DoRunPage(ghostscript, i + 1);
                        pageCompletedCallback?.Invoke();
                    }
                }
            }
예제 #7
0
 protected abstract void DoRunPage(Ghostscript ghostscript, int pageNumber);
예제 #8
0
 protected abstract void DoRunInitialize(Ghostscript ghostscript);
예제 #9
0
 protected override void DoRunPage(Ghostscript ghostscript, int pageNumber)
 {
     // not a Ghostscript file
     throw new NotSupportedException();
 }
예제 #10
0
 protected override void DoRunInitialize(Ghostscript ghostscript)
 {
     // not a Ghostscript file
     throw new NotSupportedException();
 }
예제 #11
0
 protected override void DoRunPage(Ghostscript ghostscript, int pageNumber)
 {
     // always run showpage
     base.DoRunPage(ghostscript, pageNumber);
     ghostscript.Run("showpage");
 }
예제 #12
0
 protected override void DoRunPage(Ghostscript ghostscript, int pageNumber)
 {
     // run the given page
     ghostscript.Run(_pages.Values[pageNumber - 1]);
 }
예제 #13
0
 protected override void DoRunPage(Ghostscript ghostscript, int pageNumber)
 {
     // run the page
     ghostscript.Run(string.Format(CultureInfo.InvariantCulture, "{0} pdfgetpage pdfshowpage", pageNumber));
 }
예제 #14
0
        private bool ExtractToSingleFile(Work work, Func <bool> isCanelled, Action <int, object> reportStatus)
        {
            // extract the pages to a single file
            var fileName = Path.GetFileName(work.Path);

            reportStatus(0, fileName);
            if (work.Format == ConvertFormat.Pdf)
            {
                // create the resulting PDF
                using (var combinedDocument = new PdfDocument())
                {
                    var prevProgress = 0;
                    for (var i = 0; i < work.Indices.Length; i++)
                    {
                        // return if canceled
                        if (isCanelled())
                        {
                            return(false);
                        }

                        // add the page and increment the progress
                        combinedDocument.AddPage(_document.Pages[work.Indices[i]]);
                        var progress = (int)Math.Round((90.0 * (i + 1)) / work.Indices.Length);
                        if (progress != prevProgress)
                        {
                            reportStatus(progress, fileName);
                            prevProgress = progress;
                        }
                    }
                    combinedDocument.Save(work.Path);
                }
            }
            else
            {
                // ensure there is no open renderer and create Ghostscript
                lock (_renderDocument)
                {
                    _renderDocument.EnsureNoOpenRenderer();
                    try
                    {
                        using (var ghostscript = new Ghostscript(work.Format.GetArguments(work.Path)))
                        {
                            // hook up the listener and initialize the document
                            ghostscript.Poll += (s, e) => e.Cancel = isCanelled();
                            _renderDocument.RunInitialize(ghostscript);
                            var prevProgress = 5;
                            reportStatus(prevProgress, fileName);

                            // extract each page
                            for (var i = 0; i < work.Indices.Length; i++)
                            {
                                _renderDocument.RunPage(ghostscript, work.Indices[i] + 1);
                                var progress = 5 + (int)Math.Round((90.0 * (i + 1)) / work.Indices.Length);
                                if (progress != prevProgress)
                                {
                                    reportStatus(progress, fileName);
                                    prevProgress = progress;
                                }
                            }
                        }
                    }
                    catch (OperationCanceledException)
                    {
                        // delete the file and exit
                        try
                        {
                            File.Delete(work.Path);
                        }
                        catch { }
                        return(false);
                    }
                }
            }

            // finish the operation
            reportStatus(100, string.Empty);
            return(true);
        }