protected override void DoRunInitialize(Ghostscript ghostscript) { // run the prolog if there is any if (_prologAndSetup != null) { ghostscript.Run(_prologAndSetup); } }
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); }
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); } }
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); } }
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)); }
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(); } } }
protected abstract void DoRunPage(Ghostscript ghostscript, int pageNumber);
protected abstract void DoRunInitialize(Ghostscript ghostscript);
protected override void DoRunPage(Ghostscript ghostscript, int pageNumber) { // not a Ghostscript file throw new NotSupportedException(); }
protected override void DoRunInitialize(Ghostscript ghostscript) { // not a Ghostscript file throw new NotSupportedException(); }
protected override void DoRunPage(Ghostscript ghostscript, int pageNumber) { // always run showpage base.DoRunPage(ghostscript, pageNumber); ghostscript.Run("showpage"); }
protected override void DoRunPage(Ghostscript ghostscript, int pageNumber) { // run the given page ghostscript.Run(_pages.Values[pageNumber - 1]); }
protected override void DoRunPage(Ghostscript ghostscript, int pageNumber) { // run the page ghostscript.Run(string.Format(CultureInfo.InvariantCulture, "{0} pdfgetpage pdfshowpage", pageNumber)); }
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); }