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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Configuration;
namespace test
{
public partial class Form1 : Form
{
private const int maxTime = 20;
private PDFCreator.clsPDFCreator _PDFCreator;
private PDFCreator.clsPDFCreatorError pErr;
private bool ReadyState;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string sourcePath = ConfigurationManager.AppSettings["source"];
string targetPath = ConfigurationManager.AppSettings["target"];
pErr = new PDFCreator.clsPDFCreatorError();
_PDFCreator = new PDFCreator.clsPDFCreator();
_PDFCreator.eError += new PDFCreator.__clsPDFCreator_eErrorEventHandler(_PDFCreator_eError);
_PDFCreator.eReady += new PDFCreator.__clsPDFCreator_eReadyEventHandler(_PDFCreator_eReady);
// récupérer les fichiers du dossier source
string[] fileNames = Directory.GetFiles(sourcePath, "*.doc*");
for (int i = 0; i < (fileNames.Count()); i++)
{
string fname, DefaultPrinter;
FileInfo fi;
PDFCreator.clsPDFCreatorOptions opt;
fi = new FileInfo(fileNames[i]);
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 = targetPath;
opt.AutosaveFormat = 0;
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;
}
Application.Exit();
this.Close();
Environment.Exit(1);
}
}
private void _PDFCreator_eReady()
{
MessageBox.Show( "Status: \"" + _PDFCreator.cOutputFilename + "\" was created!");
_PDFCreator.cPrinterStop = true;
ReadyState = true;
}
private void _PDFCreator_eError()
{
pErr = _PDFCreator.cError;
}
private void timer1_Tick(object sender, System.EventArgs e)
{
timer1.Enabled = false;
}
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
_PDFCreator.cClose();
while (_PDFCreator.cProgramIsRunning)
{
Application.DoEvents();
System.Threading.Thread.Sleep(100);
}
_PDFCreator = null;
pErr = null;
System.Diagnostics.Process.Start("taskkill /f /im PDFCreator.exe");
}
//protected override void OnVisibleChanged(EventArgs e)
//{
// base.OnVisibleChanged(e);
// this.Visible = false;
//}
}
} |
Partager