PDF轉換為圖片的windows服務

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.