Advertisement
Guest User

Converter Code

a guest
Jan 27th, 2011
660
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.22 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Text;
  8. using System.Xml;
  9. using System.Xml.Xsl;
  10. using System.Windows.Forms;
  11. using Ionic.Zip;
  12.  
  13. namespace DWConverter
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         String saveDir = "";
  18.         String openDir = "";
  19.         public Form1()
  20.         {
  21.             InitializeComponent();
  22.         }
  23.  
  24.         private void btnOpen_Click(object sender, EventArgs e)
  25.         {
  26.             openFileDialog1.ShowDialog();
  27.         }
  28.  
  29.         private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
  30.         {
  31.             openDir = openFileDialog1.FileName;
  32.             txtBoxOpen.Text = openDir;
  33.             saveDir = openDir.Replace(openFileDialog1.SafeFileName, "");
  34.             txtBoxSave.Text = saveDir;
  35.         }
  36.  
  37.         private void btnSave_Click(object sender, EventArgs e)
  38.         {
  39.             saveFileDialog1.ShowDialog();
  40.         }
  41.  
  42.         private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
  43.         {
  44.             saveDir = saveFileDialog1.FileName;
  45.             txtBoxSave.Text = saveDir;
  46.         }
  47.  
  48.         private void btnConvert_Click(object sender, EventArgs e)
  49.         {
  50.             String workingDir = Directory.GetCurrentDirectory() + @"\working\";
  51.             using (ZipFile zip = ZipFile.Read(openDir))
  52.             {    
  53.                 zip.ExtractAll(workingDir);
  54.             }
  55.             try
  56.             {
  57.                 // strXMLFilePath holds the absolute path of the extracted //“Document.xml” file from the word document(*.docx)
  58.                 string strXMLFilePath = workingDir + @"\word\document.xml";
  59.                 // Lets save the XSLT (shown in the article) to a file called //“OpenXml_XSLT.xsl”
  60.                 // strXSLFilePath holds the absolute path of the “OpenXml_XSLT.xsl”
  61.                 //string strXSLFilePath = Directory.GetCurrentDirectory() + @"\DocX2Html.xsl";
  62.                 // strHtmlFilePath holds the file name & path of the HTML file to be
  63.                 //    generated.In this case we save it inside the folder that holds  
  64.                 //   the Document.xml
  65.                 if(File.Exists(saveDir)){
  66.                 saveDir += 1;
  67.                 }
  68.                 string strHtmlFilePath = saveDir;
  69.                 //Load the Document.xml file into  XmlDocument object
  70.                 XmlDocument objXmlDom = new XmlDocument();
  71.                 objXmlDom.Load(strXMLFilePath);
  72.                 //Load the XSL file(OpenXml_XSLT.xsl) into the XslCompiledTransform object
  73.                 XslCompiledTransform objXSLT = new XslCompiledTransform();
  74.                 objXSLT.Load(System.Reflection.Assembly.Load("DocX2Html").GetType("DocX2Html"));
  75.                 // The Transform method converts the XML to HTML and stores the HTML file
  76.                 // in the path specified in the variable strHtmlFilePath        
  77.                 objXSLT.Transform(strXMLFilePath, strHtmlFilePath);
  78.             }
  79.             catch (Exception ex)
  80.             {
  81.                 MessageBox.Show(ex.Message);
  82.             }
  83.             Directory.Delete(workingDir, true);
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement