Guest User

Untitled

a guest
Oct 18th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.11 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Office.Interop.Word;
  6. using System.IO;
  7. using WindowsFormsWordToPDFMonitor.Properties;
  8. using iTextSharp.text.pdf;
  9.  
  10. namespace WindowsFormsWordToPDFMonitor
  11. {
  12.     class PendingJob
  13.     {
  14.         private string _name;
  15.         private string[] pending;
  16.         private string _done;
  17.         private string _output;
  18.  
  19.         public PendingJob(string name, string[] pending, string pathDone, string pathOutput)
  20.         {
  21.             this._name = name;
  22.             this.pending = pending;
  23.             this._done = pathDone;
  24.             this._output = pathOutput;
  25.         }
  26.  
  27.         private void NAR(object o)
  28.         {
  29.             try
  30.             {
  31.                 System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
  32.             }
  33.             catch { }
  34.             finally
  35.             {
  36.                 o = null;
  37.             }
  38.         }
  39.  
  40.         internal void Process(Microsoft.Office.Interop.Word.Application wordApp, String templates)
  41.         {
  42.             String outputPath = Path.Combine(Path.GetFullPath(_output), pending[1]);
  43.  
  44.             if (!File.Exists(outputPath))
  45.             {
  46.                 // Open document
  47.                 Document doc;
  48.  
  49.                 try
  50.                 {
  51.                     doc = wordApp.Documents.Open(Path.Combine(Path.GetFullPath(templates), pending[0]), Visible: true);
  52.                 }
  53.                 catch
  54.                 {
  55.                     throw new Exception("Error opening the file");
  56.                 }
  57.  
  58.                  // Save document
  59.                 try
  60.                 {
  61.                     doc.SaveAs(FileName: outputPath, FileFormat: WdSaveFormat.wdFormatPDF);
  62.                     doc.Close(SaveChanges: WdSaveOptions.wdDoNotSaveChanges);
  63.                     NAR(doc);
  64.                 }
  65.                 catch
  66.                 {
  67.                     throw new Exception("Error saving the file");
  68.                 }
  69.             }
  70.             File.CreateText(Path.Combine(Path.GetFullPath(_done), this._name)).Close();
  71.         }
  72.     }
  73. }
Add Comment
Please, Sign In to add comment