Esempio n. 1
0
        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 { }
            }
        }
Esempio n. 2
0
        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 { }
            }
        }
Esempio n. 3
0
        private void PrintIt(int FileTyp)
        {
            string   fname, DefaultPrinter;
            FileInfo fi;

            PDFCreator.clsPDFCreatorOptions opt;
            openFileDialog1.Multiselect     = false;
            openFileDialog1.CheckFileExists = true;
            openFileDialog1.CheckPathExists = true;
            openFileDialog1.Filter          = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                fi = new FileInfo(openFileDialog1.FileName);
                if (fi.Name.Length > 0)
                {
                    if (fi.Name.IndexOf(".") > 1)
                    {
                        fname = fi.Name.Substring(0, fi.Name.IndexOf("."));
                    }
                    else
                    {
                        fname = fi.Name;
                    }
                    if (!_PDFCreator.cIsPrintable(fi.FullName))
                    {
                        MessageBox.Show("File '" + fi.FullName + "' is not printable!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    opt                      = _PDFCreator.cOptions;
                    opt.UseAutosave          = 1;
                    opt.UseAutosaveDirectory = 1;
                    opt.AutosaveDirectory    = fi.DirectoryName;
                    opt.AutosaveFormat       = FileTyp;
                    if (FileTyp == 5)
                    {
                        opt.TIFFResolution = 72;
                    }
                    opt.AutosaveFilename = fname;
                    _PDFCreator.cOptions = opt;
                    _PDFCreator.cClearCache();
                    DefaultPrinter = _PDFCreator.cDefaultPrinter;
                    _PDFCreator.cDefaultPrinter = "PDFCreator";
                    _PDFCreator.cPrintFile(fi.FullName);
                    ReadyState = false;
                    _PDFCreator.cPrinterStop = false;
                    timer1.Interval          = maxTime * 1000;
                    timer1.Enabled           = true;
                    while (!ReadyState && timer1.Enabled)
                    {
                        Application.DoEvents();
                    }
                    timer1.Enabled = false;
                    if (!ReadyState)
                    {
                        MessageBox.Show("Creating printer test page as pdf.\n\r\n\r" +
                                        "An error is occured: Time is up!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    _PDFCreator.cPrinterStop    = true;
                    _PDFCreator.cDefaultPrinter = DefaultPrinter;
                }
            }
        }