PDF converted to a picture of Windows services

Source: Internet
Author: User

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);
}
}
}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.