1.新建一windows服務工程,添加服務檔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);
}
}
}
}
}
}
2.增加InstallerClass檔
[RunInstaller(true)]
publicpartialclass ProjectInstaller : Installer
{
public ProjectInstaller()
{
InitializeComponent();
}

privatevoid ProjectInstaller_AfterInstall(object sender, InstallEventArgs e)
{
RegistryKey rk
= Registry.LocalMachine;
string key =@"SYSTEM\CurrentControlSet\Services\"+this.serviceInstaller1.ServiceName;
RegistryKey sub
= rk.CreateSubKey(key);
sub.CreateSubKey(
"Type");
int value = (int)sub.GetValue("Type");
sub.SetValue(
"Type", value |256);
}
 
 
3.增加一個Setup工程創建安裝程式,安裝服務後配置AppConfig.config中的目錄資訊,啟動服務。每隔2s服務檢查PDF檔目錄對新增檔自動進行轉換。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="pdfFilepath" value="D:\PDFPath\"/>
<add key="imageFilepath" value="D:\ImagePath\"/>
</appSettings>
</configuration>

 

創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()