PDF转换为图片的 windows服务

 

 

 

1.新建一windows服务工程,添加服务文件PDFConverter

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
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文件

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
[RunInstaller( true )]
public partial class ProjectInstaller : Installer
{
public ProjectInstaller()
{
InitializeComponent();
}

private void 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文件目录对新增文件自动进行转换。

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
<? xml version = " 1.0 " encoding = " utf-8 " ?>
< configuration >
< appSettings >
< add key = " pdfFilepath " value = " D:\PDFPath\ " />
< add key = " imageFilepath " value = " D:\ImagePath\ " />
</ appSettings >
</ configuration >

转载于:https://www.cnblogs.com/haojuandon/archive/2010/04/19/1715673.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值