/// <summary> /// Напечатать PDF /// </summary> private IResultError PrintPdf(PDFCreator.clsPDFCreator pdfCreator, Func <IResultError> printFunction) { pdfCreator.eReady += PdfCreatorReady; _readyState = false; pdfCreator.cPrinterStop = false; var printFunctionResult = printFunction.Invoke(); int waitingLimit = 0; while (!_readyState && waitingLimit < WAITING_PRINTING_LIMIT_IN_SECONDS && printFunctionResult.OkStatus) { waitingLimit += 1; Thread.Sleep(1000); } pdfCreator.cPrinterStop = true; pdfCreator.cClose(); KillAllPreviousProcess(); return((_readyState || printFunctionResult.HasErrors ? new ResultError() : new ErrorCommon(ErrorConvertingType.PdfPrintingError, "Время создания PDF файла истекло"). ToResult()). ConcatErrors(printFunctionResult.Errors)); }
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e) { _PDFCreator.cClose(); while (_PDFCreator.cProgramIsRunning) { Application.DoEvents(); System.Threading.Thread.Sleep(100); } pErr = null; _PDFCreator = null; }
public void ConvertToPdf(string filePath, string pdfDestinationPath) { try { ReadyState = false; string parameters; pErr = new PDFCreator.clsPDFCreatorError(); _PDFCreator = new PDFCreator.clsPDFCreator(); _PDFCreator.eError += new PDFCreator.__clsPDFCreator_eErrorEventHandler(_PDFCreator_eError); _PDFCreator.eReady += new PDFCreator.__clsPDFCreator_eReadyEventHandler(_PDFCreator_eReady); //System.Threading.Thread.Sleep(5000); parameters = "/NoProcessingAtStartup"; if (!_PDFCreator.cStart(parameters, false)) throw new Exception("Error starting PdfCreator: [" + pErr.Number + "]: " + pErr.Description); //System.Threading.Thread.Sleep(5000); if (!_PDFCreator.cIsPrintable(filePath)) throw new Exception("PdfCreator says that file '" + filePath + "' is not printable!"); PDFCreator.clsPDFCreatorOptions opt; opt = _PDFCreator.cOptions; // Store previous option values: int useautosave = opt.UseAutosave; int useautosaveDir = opt.UseAutosaveDirectory; string autosaveDir = opt.AutosaveDirectory; int autosaveformat = opt.AutosaveFormat; string autosavefilename = opt.AutosaveFilename; //System.Threading.Thread.Sleep(5000); // Set new options to save to the desired file: opt.UseAutosave = 1; opt.UseAutosaveDirectory = 1; opt.AutosaveDirectory = Path.GetDirectoryName(pdfDestinationPath); opt.AutosaveFormat = 0; opt.AutosaveFilename = Path.GetFileNameWithoutExtension(pdfDestinationPath); // Print: _PDFCreator.cOptions = opt; _PDFCreator.cClearCache(); string DefaultPrinter = _PDFCreator.cDefaultPrinter; _PDFCreator.cDefaultPrinter = "PDFCreator"; _PDFCreator.cPrintFile(filePath); _PDFCreator.cPrinterStop = false; // Wait until print ends timer.Interval = maxTime * 1000; timer.Enabled = true; while (!ReadyState && timer.Enabled) { Application.DoEvents(); System.Threading.Thread.Sleep(100); // Wait 100 miliseconds. } timer.Enabled = false; // Restore previous options values: opt.UseAutosave = useautosave; opt.UseAutosaveDirectory = useautosaveDir; opt.AutosaveDirectory = autosaveDir; opt.AutosaveFormat = autosaveformat; opt.AutosaveFilename = autosavefilename; _PDFCreator.cOptions = opt; if (!ReadyState) throw new Exception("PdfCreator printing timeout"); _PDFCreator.cPrinterStop = true; _PDFCreator.cDefaultPrinter = DefaultPrinter; } finally { try { // Close the PDF com object: if (_PDFCreator != null) { _PDFCreator.cClose(); System.Runtime.InteropServices.Marshal.ReleaseComObject(_PDFCreator); //_PDFCreator = null; System.Runtime.InteropServices.Marshal.ReleaseComObject(pErr); pErr = null; GC.Collect(); } } catch { } } }
public void ConvertToPdf(string filePath, string pdfDestinationPath) { try { PrintingFinished = false; string parameters; pErr = new PDFCreator.clsPDFCreatorError(); _PDFCreator = new PDFCreator.clsPDFCreator(); _PDFCreator.eError += new PDFCreator.__clsPDFCreator_eErrorEventHandler(_PDFCreator_eError); _PDFCreator.eReady += new PDFCreator.__clsPDFCreator_eReadyEventHandler(_PDFCreator_eReady); parameters = "/NoProcessingAtStartup"; if (!_PDFCreator.cStart(parameters, false)) { throw new Exception("Error starting PdfCreator: [" + pErr.Number + "]: " + pErr.Description); } if (!_PDFCreator.cIsPrintable(filePath)) { throw new Exception("PdfCreator says that file '" + filePath + "' is not printable!"); } PDFCreator.clsPDFCreatorOptions opt; opt = _PDFCreator.cOptions; // Store previous option values: int useautosave = opt.UseAutosave; int useautosaveDir = opt.UseAutosaveDirectory; string autosaveDir = opt.AutosaveDirectory; int autosaveformat = opt.AutosaveFormat; string autosavefilename = opt.AutosaveFilename; // Set new options to save to the desired file: opt.UseAutosave = 1; opt.UseAutosaveDirectory = 1; opt.AutosaveDirectory = Path.GetDirectoryName(pdfDestinationPath); opt.AutosaveFormat = 0; opt.AutosaveFilename = Path.GetFileNameWithoutExtension(pdfDestinationPath); // Print: _PDFCreator.cOptions = opt; _PDFCreator.cClearCache(); string DefaultPrinter = _PDFCreator.cDefaultPrinter; _PDFCreator.cDefaultPrinter = "PDFCreator"; _PDFCreator.cPrintFile(filePath); _PDFCreator.cPrinterStop = false; // Wait until print ends PrintTimeoutTimer.Interval = MAXPRINTTIME * 1000; PrintTimeoutTimer.Enabled = true; while (!PrintingFinished && PrintTimeoutTimer.Enabled) { Application.DoEvents(); System.Threading.Thread.Sleep(100); // Wait 100 miliseconds. } PrintTimeoutTimer.Enabled = false; // Restore previous options values: opt.UseAutosave = useautosave; opt.UseAutosaveDirectory = useautosaveDir; opt.AutosaveDirectory = autosaveDir; opt.AutosaveFormat = autosaveformat; opt.AutosaveFilename = autosavefilename; _PDFCreator.cOptions = opt; if (!PrintingFinished) { throw new Exception("PdfCreator printing timeout"); } _PDFCreator.cPrinterStop = true; _PDFCreator.cDefaultPrinter = DefaultPrinter; } finally { try { // Close the PDF com object: if (_PDFCreator != null) { _PDFCreator.cClose(); System.Runtime.InteropServices.Marshal.ReleaseComObject(_PDFCreator); //_PDFCreator = null; System.Runtime.InteropServices.Marshal.ReleaseComObject(pErr); pErr = null; GC.Collect(); } } catch { } } }