1. Create a new Windows Service project, add service files Pdfconverter
Partial class Pdfconverter:servicebase
{
Public Pdfconverter ()
{
InitializeComponent ();
}
Private System.Timers.Timer Timer;
protected override void OnStart (string[) args)
{
Timer = new System.Timers.Timer (2000);
Timer. Elapsed + = new Elapsedeventhandler (timer_elapsed);
Timer. Start ();
}
void Timer_elapsed (object sender, Elapsedeventargs e)
{
Thread thread = new Thread (new ThreadStart (Process));
Thread. Setapartmentstate (ApartmentState.STA);
Thread. Start ();
}
protected override void OnStop ()
{
Timer. Stop ();
}
[STAThread]
protected void Process ()
{
String pdffilepath = configurationmanager.appsettings["Pdffilepath"];
String ImagePath = configurationmanager.appsettings["Imagefilepath"];
DirectoryInfo directory = new DirectoryInfo (Pdffilepath);
if (directory = null)
Return
if (! Directory.Exists (ImagePath))
Directory.CreateDirectory (ImagePath);
fileinfo[] Files = directory. GetFiles ("*.pdf");
if (Files. Length > 0)
{
foreach (FileInfo file in files)
{
if (file. Fullname.endswith (". pdf"))
{
String pdffilename = ImagePath + file. Name;
if (! File.exists (pdffilename) | | File.getcreationtime (pdffilename) < file. CreationTime)
{
IDataObject data = Clipboard.getdataobject ();
File.Copy (file. FullName, Pdffilename, true);
Acropddoc doc = new Acropddoc ();
Doc. Open (file. FullName);
int pagecount = doc. Getnumpages ();
for (int i = 0; i < PageCount; i++)
{
Acropdpage page = (acropdpage) doc. Acquirepage (i);
Acropoint point = (acropoint) page. GetSize ();
Acrorectclass rect = new Acrorectclass ();
Rect. top = rect. left = 0;
Rect.right = Point.x;
Rect.bottom = Point.y;
Try
{
bool OK = page. CopyToClipboard (rect, 0, 0, 100);
if (OK)
{
IDataObject pdfdata = Clipboard.getdataobject ();
string[] formats = Pdfdata.getformats ();
Bitmap Bitmap = (Bitmap) pdfdata.getdata (DATAFORMATS.BITMAP);
if (bitmap!= null)
{
Bitmap. Save (ImagePath + file). Name.substring (0, file. name.length-4) + i.tostring () + ". jpg");
Bitmap. Dispose ();
}
}
}
Catch {}
Finally
{
Marshal.ReleaseComObject (page);
Marshal.ReleaseComObject (point);
}
}
Clipboard.clear ();
Marshal.ReleaseComObject (DOC);
if (data!= null)
Clipboard.setdataobject (data);
}
}
}
}
}
}