1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.Win32;
using System.Threading;
using System.Diagnostics;
namespace DC_ConversionEnPdf
{
class ConversionPdf
{
private const int maxTime = 20;
private PDFCreator.clsPDFCreator _PDFCreator;
private PDFCreator.clsPDFCreatorError pErr;
private bool ReadyState;
string _repertoire = "C:\\";
string _fichier = "fichier.pdf";
private string cleRegistre_PdfCreator = @"SOFTWARE\PDFCreator\Program";
private string fichier;
public ConversionPdf(string _arg)
{
fichier = _arg;
if(fichier.Length>0)
convertirEnPdf();
}
private void _PDFCreator_eReady()
{
_PDFCreator.cPrinterStop = true;
ReadyState = true;
}
private void _PDFCreator_eError()
{
pErr = _PDFCreator.cError;
}
private void imprimerPDF(string unfichier)
{
string DefaultPrinter;
Console.WriteLine("Conversion du document en cours...");
//On change les paramètres de PDF Creator
RegistryKey rk = Registry.CurrentUser.OpenSubKey(cleRegistre_PdfCreator, true);
rk.SetValue("AutosaveDirectory", _repertoire, RegistryValueKind.Unknown);
rk.SetValue("AutosaveFilename", _fichier, RegistryValueKind.Unknown);
rk.SetValue("UseAutosave", "1", RegistryValueKind.Unknown);
#region On lance l'impression
PDFCreator.clsPDFCreatorOptions opt;
_PDFCreator = new PDFCreator.clsPDFCreator();
_PDFCreator.eError += new PDFCreator.__clsPDFCreator_eErrorEventHandler(_PDFCreator_eError);
_PDFCreator.eReady += new PDFCreator.__clsPDFCreator_eReadyEventHandler(_PDFCreator_eReady);
DefaultPrinter = _PDFCreator.cDefaultPrinter;
_PDFCreator.cDefaultPrinter = "PDFCreator";
_PDFCreator.cStart("/NoProcessingAtStartup", true);
_PDFCreator.cPrinterStop = false;
opt = _PDFCreator.cOptions;
opt.UseAutosave = 1;
opt.UseAutosaveDirectory = 1;
opt.AutosaveFormat = 0;
_PDFCreator.cOptions = opt;
_PDFCreator.cClearCache();
_PDFCreator.cVisible = true;
_PDFCreator.cPrintFile(unfichier);
_PDFCreator.cPrinterStop = false;
ReadyState = false;
_PDFCreator.cPrinterStop = true;
_PDFCreator.cDefaultPrinter = DefaultPrinter;
_PDFCreator.cClose();
System.Runtime.InteropServices.Marshal.ReleaseComObject(_PDFCreator);
System.Runtime.InteropServices.Marshal.ReleaseComObject(pErr);
pErr = null;
GC.Collect();
GC.WaitForPendingFinalizers();
#endregion
}
private void convertirEnPdf()
{
try
{
if (File.Exists(fichier))
imprimerPDF(fichier);
}
catch(Exception ex)
{
Console.WriteLine("Erreur : "+ex.Message);
}
}
}
} |