Krzysztof Radzimski

Pasja tworzenia

Konwersja dokumentu DOCX do PDF z użyciem Word-a 2007

30 marzec
Opublikował Krzysztof Radzimski 30. marca 2010 18:36
   1: using System;
   2: using System.IO;
   3: using System.Runtime.InteropServices;
   4: using Microsoft.Office.Interop.Word;
   5:  
   6: /// <summary>
   7: /// Klasa odpowiedzialna za konwersję pliku DOCX do formatu PDF.
   8: /// </summary>
   9: public static class ConverterController {
  10:     static object missing = Type.Missing;
  11:     static object False = false;
  12:     public const string KEY = "a787bbed-3d72-4316-9189-72a62b622068";
  13:  
  14:     /// <summary>
  15:     /// Zwraca informację czy aplikacja Microsoft Office Word 2007 jest zainstalowana.
  16:     /// </summary>
  17:     /// <returns></returns>
  18:     public static bool WordIsInstalled() {
  19:         string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
  20:             @"Microsoft Office\Office12\Winword.exe");
  21:  
  22:         return File.Exists(path);
  23:     }
  24:  
  25:     /// <summary>
  26:     /// Zwraca informację czy dodatek 'Zapisz jako PDF' został zainstalowany.
  27:     /// </summary>
  28:     /// <returns></returns>
  29:     public static bool PluginInstalled() {
  30:  
  31:         string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
  32:             @"Common Files\Microsoft Shared\OFFICE12\EXP_XPS.DLL");
  33:  
  34:         return File.Exists(path);
  35:     }
  36:  
  37:     /// <summary>
  38:     /// Konweruje plik DOCX do PDF z użyciem aplikacji Microsoft Office Word 2007.
  39:     /// </summary>
  40:     /// <param name="sourceFilePath"></param>
  41:     /// <param name="destinationFilePath"></param>
  42:     /// <returns></returns>
  43:     public static string ConvertDocxToPdf(string sourceFilePath, string destinationFilePath) {
  44:         if (!WordIsInstalled()) {
  45:             throw new Exception("Aplikacja Microsoft Office Word 2007 nie została zainstalowana.");
  46:         }
  47:         if (!PluginInstalled()) {
  48:             throw new Exception("Dodatek pakietu Microsoft Office 2007 'Microsoft Zapisz jako PDF' nie został zainstalowany.");
  49:         }
  50:  
  51:         ApplicationClass app = null;
  52:         Document doc = null;
  53:  
  54:         try {
  55:             app = new ApplicationClass();
  56:             if (app!=null) {
  57:                 app.Visible = false;
  58:  
  59:                 app.Options.CheckGrammarAsYouType = false;
  60:                 app.Options.CheckGrammarWithSpelling = false;
  61:                 app.Options.CheckHangulEndings = false;
  62:                 app.Options.CheckSpellingAsYouType = false;
  63:  
  64:                 object fileName = sourceFilePath;
  65:  
  66:                 doc = app.Documents.Open(ref fileName, ref missing, ref missing
  67:                     , ref missing, ref missing, ref missing, ref missing, ref missing, ref missing
  68:                     , ref missing, ref missing, ref missing, ref missing, ref missing, ref missing
  69:                     , ref missing);
  70:  
  71:                 if (doc!=null) {
  72:  
  73:                     WdSaveFormat saveFormat = WdSaveFormat.wdFormatPDF;
  74:                     if (String.IsNullOrEmpty(destinationFilePath))
  75:                         destinationFilePath = sourceFilePath.Replace(".docx", ".pdf");
  76:                     object o = destinationFilePath;
  77:                     object sf = saveFormat;
  78:                     doc.SaveAs(ref o, ref sf, ref missing, ref missing, ref missing, ref missing
  79:                         , ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
  80:                         ref missing, ref missing, ref missing);
  81:                     ((_Document)doc).Close(ref missing, ref missing, ref missing);
  82:                 }
  83:             }
  84:             else {
  85:                 throw new Exception("Nie udało się utworzyć instancji dokumentu Word.");
  86:             }
  87:         }
  88:         finally {
  89:             try {
  90:                 app.Quit(ref False, ref missing, ref missing);
  91:  
  92:                 Marshal.FinalReleaseComObject(doc);
  93:                 Marshal.FinalReleaseComObject(app);
  94:  
  95:                 app = null;
  96:                 doc = null;
  97:             }
  98:             catch { }
  99:         }
 100:         return destinationFilePath;
 101:     }
 102: }

Tagi: | Kategorie: Programowanie

Wyślij link na adres e-mail | Link do tego postu | RSSRSS comment feed