private string CreateVSDFile(string filename, ref string visioInstall) { Microsoft.Office.Interop.Visio.Document doc; Microsoft.Office.Interop.Visio.Page page; Microsoft.Office.Interop.Visio.InvisibleApp app = null; string extension = ".vsd"; string filewoext = ""; try { if (filename.Length > 0) { visioInstall = ""; if (CheckVisioIsInstalled() == false) { //ShowInformationMessage("Please install Visio and try again!"); visioInstall = "Please install Visio and try again!"; return(""); } filewoext = Path.GetFileNameWithoutExtension(filename); ToolAdmin.picture = new System.IO.MemoryStream(System.IO.File.ReadAllBytes(filename)); File.Copy(filename, System.AppDomain.CurrentDomain.BaseDirectory + "" + filewoext, true); app = new Microsoft.Office.Interop.Visio.InvisibleApp(); //if (app == null) //{ // visioInstall = "Please install Visio and try again!"; // return ""; //} doc = app.Documents.Open(System.AppDomain.CurrentDomain.BaseDirectory + "" + filewoext); page = app.ActivePage; page.Export(System.AppDomain.CurrentDomain.BaseDirectory + "" + filewoext + ".bmp"); app.Quit(); } return(System.AppDomain.CurrentDomain.BaseDirectory + "" + filewoext + ".bmp"); } catch (Exception ex) { try { if (app != null) { app.Quit(); } throw ex.LogException(); } catch (Exception ex1) { return(""); throw ex1.LogException(); } throw ex.LogException(); } }
private Boolean SaveFile() { Microsoft.Office.Interop.Visio.Document doc; Microsoft.Office.Interop.Visio.Page page; Microsoft.Office.Interop.Visio.InvisibleApp app = null; try { if (ToolAdmin.FileType.ToUpper() == "VSD") { if (CheckVisioIsInstalled() == false) { Progress.End(); ShowInformationMessage("Please install Visio and try again!"); return(false); } if (ToolAdmin.MimeType.ToString().IndexOf("application") >= 0) { app = new Microsoft.Office.Interop.Visio.InvisibleApp(); //if (app == null) //{ // ShowInformationMessage("Please install Visio and try again!"); // return false; //} doc = app.Documents.Open(ToolAdmin.File_Name); page = app.ActivePage; page.Export(ToolAdmin.DisplayFile_Name); app.Quit(); } } return(true); } catch (Exception ex) { try { app.Quit(); return(false); } catch (Exception ex1) { return(false); throw ex1.LogException(); } throw ex.LogException(); } }
private static void ProcessRequest(TcpClient client, int clientNum) { Microsoft.Office.Interop.Visio.InvisibleApp VisioInst = null; try { // Buffer for reading data Byte[] bytes = new Byte[4096]; StringBuilder data = new StringBuilder(); // Get a stream object for reading and writing NetworkStream stream = client.GetStream(); int i; // Loop to receive all the data sent by the client. while (stream.DataAvailable) { i = stream.Read(bytes, 0, bytes.Length); // Translate data bytes to a ASCII string. String str = System.Text.Encoding.ASCII.GetString(bytes, 0, i); data.Append(str); } String allData = data.ToString(); Console.WriteLine("{0} - Received: {1} at {2}", clientNum, allData, DateTime.Now); //Now convert these vsd file pathes to vsdx Console.WriteLine("{0} - Converting ...", clientNum); String[] info = allData.Split('\n'); if (info.Length < 3) { throw new Exception("Invalid number of arguments"); } String[] files = new String[info.Length - 2]; for (var o = 2; o < info.Length; o++) { files[o - 2] = info[o]; } String srcExt = info[0]; String dstExt = info[1]; //Using COM to call visio to convert the files Type VisioType = Type.GetTypeFromProgID("Visio.InvisibleApp"); //TODO create a pool of visio instances VisioInst = (Microsoft.Office.Interop.Visio.InvisibleApp)Activator.CreateInstance(VisioType); foreach (String file in files) { if (file.Length > 0) { var doc = VisioInst.Documents.Open(file + srcExt); doc.SaveAs(file + dstExt); doc.Close(); } } VisioInst.Quit(); VisioInst = null; byte[] msg = System.Text.Encoding.ASCII.GetBytes("Done"); // Send back a response. stream.Write(msg, 0, msg.Length); Console.WriteLine("{0} - Sent: Done at {1}", clientNum, DateTime.Now); } catch (SocketException e) { Console.WriteLine("{0} - {1}: SocketException: {2}", clientNum, DateTime.Now, e); //the socket has errors so most probably we cannot send back to the client } catch (Exception any) { Console.WriteLine("{0} - {1}: Exception: {2}", clientNum, DateTime.Now, any); byte[] msg = System.Text.Encoding.ASCII.GetBytes("Error"); NetworkStream stream = client.GetStream(); // Send back error response. stream.Write(msg, 0, msg.Length); } finally { //Close visio if an unexpected error occured if (VisioInst != null) { VisioInst.Quit(); } // Shutdown and end connection client.Close(); } }
public bool LoadDocument(string filename, string typename, ListView parent) { this.Clear(); if (!File.Exists(filename)) { return(false); } // all windows media play formats if (typename == "WMP") { this.pdfViewer.Visible = false; this.fileView.Visible = false; this.zipView.Visible = false; this.imgView.Visible = false; this.docView.Visible = false; this.webBrowser.Visible = false; if (this.b_cfw) { this.axWMP.Visible = false; this.videoPlayerCtl.URL = filename; this.videoPlayerCtl.Location = new System.Drawing.Point(this.ClientRectangle.X, this.ClientRectangle.Y); this.videoPlayerCtl.Size = this.Size; this.videoPlayerCtl.Visible = true; return(true); } if (this.b_wmp) { this.videoPlayerCtl.Visible = false; this.axWMP.URL = filename; this.axWMP.Visible = true; this.axWMP.Dock = DockStyle.Fill; return(true); } } // in case something goes wrong, we allow to fallback to b_asis and show the binary content bool asisTmp = this.b_asis; // ext drives most of the things string ext = System.IO.Path.GetExtension(filename).ToLower(); // all music if ((".mp3.wav.flac".IndexOf(ext) != -1) && this.b_mp3) { try { this.pdfViewer.Visible = false; this.fileView.Visible = false; this.zipView.Visible = false; this.imgView.Visible = false; this.webBrowser.Visible = false; this.videoPlayerCtl.Visible = false; this.docView.Visible = false; this.axWMP.Visible = true; this.axWMP.Dock = DockStyle.Fill; this.axWMP.URL = filename; asisTmp = false; } catch (System.Exception) { asisTmp = true; } } // initially I only found a way to convert Word to html (which is done here), other MSO Files conversations were found later and appear in the HTML section if ((".rtf.doc.docx".IndexOf(ext) != -1) && this.b_doc) { try { this.pdfViewer.Visible = false; this.fileView.Visible = false; this.zipView.Visible = false; this.imgView.Visible = false; this.webBrowser.Visible = false; this.videoPlayerCtl.Visible = false; this.axWMP.Visible = false; this.docView.LoadDocument(filename); this.docView.Visible = true; asisTmp = false; } catch (System.Exception) { asisTmp = true; } } else { // images if ((".ico.bmp.tif.jpg.jpeg.wmf.gif.png.exif.emf.tiff".IndexOf(ext) != -1) && this.b_img) { try { this.pdfViewer.Visible = false; this.fileView.Visible = false; this.zipView.Visible = false; this.webBrowser.Visible = false; this.docView.Visible = false; this.videoPlayerCtl.Visible = false; this.axWMP.Visible = false; this.imgView.Visible = true; this.imgView.LoadDocument(filename, parent); asisTmp = false; } catch (System.Exception) { asisTmp = true; } } else { // PDF and PowerPoint if ((".pdf.ppt.pptx.pptm.odp".IndexOf(ext) != -1) && (this.b_pdf || this.b_doc)) { // powerpoint formats via powerpoint interop converted to pdf if ((".ppt.pptx.pptm.odp".IndexOf(ext) != -1) && this.b_doc) { string outpath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "temp.pdf"); if (System.IO.File.Exists(outpath)) { System.IO.File.Delete(outpath); } try { Microsoft.Office.Interop.PowerPoint.Application app = new Microsoft.Office.Interop.PowerPoint.Application(); Microsoft.Office.Interop.PowerPoint.Presentations pres = app.Presentations; Microsoft.Office.Interop.PowerPoint.Presentation file = pres.Open(filename, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse); file.SaveCopyAs(outpath, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPDF, Microsoft.Office.Core.MsoTriState.msoTrue); app.Quit(); filename = outpath; asisTmp = false; } catch (Exception) { asisTmp = true; } } // show pdf try { this.pdfViewer.ZoomMode = PdfViewerZoomMode.FitWidth; PdfDocument pdoc = PdfDocument.Load(filename); this.pdfViewer.Document = pdoc; this.fileView.Visible = false; this.zipView.Visible = false; this.imgView.Visible = false; this.webBrowser.Visible = false; this.docView.Visible = false; this.videoPlayerCtl.Visible = false; this.axWMP.Visible = false; this.pdfViewer.Visible = true; asisTmp = false; } catch (Exception) { asisTmp = true; } } else { // ZIP-Viewer simply shows the content of zip file if ((".zip".IndexOf(ext) != -1) && this.b_zip) { try { this.zipView.LoadZip(filename); this.pdfViewer.Visible = false; this.fileView.Visible = false; this.imgView.Visible = false; this.webBrowser.Visible = false; this.docView.Visible = false; this.videoPlayerCtl.Visible = false; this.axWMP.Visible = false; this.zipView.Visible = true; asisTmp = false; } catch (Exception) { asisTmp = true; } } else { // collector for everything, which could be converted into HTML if ((".htm.html.msg.eml.mht.xls.xlsx.xlsb.xlsm.vsd.vsdx.vsdm" /*.mpp.mpx"*/.IndexOf(ext) != -1) && (this.b_htm || this.b_doc)) { // visio via visio interop to html if ((".vsd.vsdx.vsdm".IndexOf(ext) != -1) && this.b_doc) { string outpath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "temp.html"); if (System.IO.File.Exists(outpath)) { System.IO.File.Delete(outpath); } asisTmp = true; try { Microsoft.Office.Interop.Visio.IVInvisibleApp visio = null; visio = new Microsoft.Office.Interop.Visio.InvisibleApp(); Microsoft.Office.Interop.Visio.SaveAsWeb.VisSaveAsWeb saveAsWeb = (Microsoft.Office.Interop.Visio.SaveAsWeb.VisSaveAsWeb)visio.Application.SaveAsWebObject; saveAsWeb.AttachToVisioDoc(visio.Documents.OpenEx(filename, (short)Microsoft.Office.Interop.Visio.VisOpenSaveArgs.visOpenRO)); Microsoft.Office.Interop.Visio.SaveAsWeb.VisWebPageSettings webPageSettings = (Microsoft.Office.Interop.Visio.SaveAsWeb.VisWebPageSettings)saveAsWeb.WebPageSettings; webPageSettings.TargetPath = outpath; webPageSettings.PageTitle = outpath; webPageSettings.DispScreenRes = Microsoft.Office.Interop.Visio.SaveAsWeb.VISWEB_DISP_RES.res768x1024; webPageSettings.QuietMode = 1; webPageSettings.SilentMode = 1; webPageSettings.NavBar = 1; webPageSettings.PanAndZoom = 1; webPageSettings.Search = 1; webPageSettings.OpenBrowser = 0; webPageSettings.PropControl = 0; saveAsWeb.CreatePages(); this.webBrowser.Navigate(outpath); asisTmp = false; } catch (Exception) { asisTmp = true; } } // MS Project formats via project interop to xls: // - not working with 2003 Interops on a system running MS-Project 2003 (--> 2003 converter is shown) // - not working with 2007 Interops on a system running MS-Project 2003 (--> no html support in 2003) //if ((".mpp.mpx".IndexOf(ext) != -1) && b_doc) { // string outpath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "temp.xls"); // if (System.IO.File.Exists(outpath)) { // System.IO.File.Delete(outpath); // } // asisTmp = true; // try { // var app = new Microsoft.Office.Interop.MSProject.Application(); // app.Visible = false; // var proj = app.ActiveProject; // app.FileOpenx(filename, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.MSProject.PjPoolOpen.pjPoolReadOnly, Type.Missing, Type.Missing, Type.Missing, Type.Missing); // //app.FileOpenEx(filename, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.MSProject.PjPoolOpen.pjPoolReadOnly, Type.Missing, Type.Missing, Type.Missing, Type.Missing); // // save MS-Project as Excel file (MS-Project interopt has no HTML exporter) and let Excel viewer do the job // proj.SaveAs(outpath, PjFileFormat.pjXLS, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); // app.Quit(Microsoft.Office.Interop.MSProject.PjSaveType.pjDoNotSave); // // set temporary Excel filename as "real" filename --> let Excel viewer do the work // filename = outpath; // // adjust extension --> let Excel viewer do the work // ext = System.IO.Path.GetExtension(filename).ToLower(); // asisTmp = false; // } catch (System.Reflection.TargetInvocationException x) { // asisTmp = true; // } //} // excel formats via excel interop to html if ((".xls.xlsx.xlsb.xlsm".IndexOf(ext) != -1) && this.b_doc) { string outpath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "temp.html"); if (System.IO.File.Exists(outpath)) { System.IO.File.Delete(outpath); } this.Cursor = Cursors.WaitCursor; try { // http://www.codeproject.com/Articles/507068/Microsoft-Interop-API-to-convert-the-doc-docx-dot Microsoft.Office.Interop.Excel.Application excel = null; Microsoft.Office.Interop.Excel.Workbook xls = null; excel = new Microsoft.Office.Interop.Excel.Application(); object missing = Type.Missing; object trueObject = true; excel.Visible = false; excel.DisplayAlerts = false; xls = excel.Workbooks.Open(filename, missing, trueObject, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); object format = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml; System.Collections.IEnumerator wsEnumerator = excel.ActiveWorkbook.Worksheets.GetEnumerator(); while (wsEnumerator.MoveNext()) { this.Cursor = Cursors.WaitCursor; Microsoft.Office.Interop.Excel.Workbook wsCurrent = xls; wsCurrent.SaveAs(outpath, format, missing, missing, missing, missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing); } excel.Quit(); this.webBrowser.Navigate(outpath); asisTmp = false; } catch (Exception cex) { GrzTools.AutoMessageBox.Show(cex.Message, "Exception", 2000); asisTmp = true; } Cursor.Current = Cursors.Default; } // msg files are converted via MsgReader to html if (".msg".IndexOf(ext) != -1) { try { string body = ""; Reader msgReader = new Reader(); using (StreamReader streamReader = new StreamReader(filename)) { body = msgReader.ExtractMsgEmailBody(streamReader.BaseStream, true, "text/html; charset=utf-8"); } this.webBrowser.DocumentText = body; asisTmp = false; } catch (Exception) { asisTmp = true; } } // html-style formatted files if ((".htm.html.eml.mht".IndexOf(ext) != -1) && this.b_htm) { this.webBrowser.Url = new Uri(filename); asisTmp = false; } if (!asisTmp) { this.pdfViewer.Visible = false; this.fileView.Visible = false; this.zipView.Visible = false; this.imgView.Visible = false; this.docView.Visible = false; this.axWMP.Visible = false; this.videoPlayerCtl.Visible = false; this.webBrowser.Visible = true; } } } } } } // standard file view if (asisTmp) { this.fileView.LoadDocument(null, filename, parent); this.pdfViewer.Visible = false; this.zipView.Visible = false; this.imgView.Visible = false; this.webBrowser.Visible = false; this.docView.Visible = false; this.videoPlayerCtl.Visible = false; this.axWMP.Visible = false; this.fileView.Visible = true; } return(true); }
// Start the file conversion private void Convert(object sender, DoWorkEventArgs e) { Application.Current.Dispatcher.Invoke(() => { infoBox.AppendText(Environment.NewLine); infoBox.AppendText("------------------------------"); infoBox.AppendText(Environment.NewLine); infoBox.AppendText("Starting conversion process"); infoBox.AppendText(Environment.NewLine); infoBox.AppendText(Environment.NewLine); }); Microsoft.Office.Interop.Visio.InvisibleApp VisioInst = null; try { // Using COM to call visio to convert the files Type VisioType = Type.GetTypeFromProgID("Visio.InvisibleApp"); // Create visio instance to open all the files VisioInst = (Microsoft.Office.Interop.Visio.InvisibleApp)Activator.CreateInstance(VisioType); // If an Output Folder was selected, the save to that folder // Otherwise, save in the same location of the source file Func <string, string> SaveLocation = null; if (OUTPUT_FILES_PATH == null) { SaveLocation = (filePath) => string.Format("{0}x", filePath); } else { SaveLocation = (filePath) => string.Format("{0}\\{1}x", OUTPUT_FILES_PATH, Path.GetFileName(filePath)); } //foreach (String file in VSD_FILES) for (int i = 0; i < VSD_FILES.Length; i++) { var file = VSD_FILES[i]; try { if (file.Length > 0) { string openLocation = Path.GetFullPath(file); Application.Current.Dispatcher.Invoke(() => { infoBox.AppendText(string.Format(openingText, openLocation)); infoBox.AppendText(Environment.NewLine); }); // Open .vsd file var doc = VisioInst.Documents.Open(openLocation); string saveLocation = SaveLocation(openLocation); // Add wait time to avoid the "The RPC server is unavailable" exception Thread.Sleep(1000); Application.Current.Dispatcher.Invoke(() => { infoBox.AppendText(string.Format(savingText, saveLocation)); infoBox.AppendText(Environment.NewLine); }); // Save .vsdx file doc.SaveAs(saveLocation); // Close document doc.Close(); Application.Current.Dispatcher.Invoke(() => { infoBox.AppendText("Done!"); infoBox.AppendText(Environment.NewLine); infoBox.AppendText(Environment.NewLine); }); } } catch (Exception loopException) { CreateLog(loopException.ToString()); String logPath = AppDomain.CurrentDomain.BaseDirectory + "error.log"; ShowMessage("An error occurred while while converting this file\nA log file has been created in " + logPath + "\nClick OK to continue.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); Application.Current.Dispatcher.Invoke(() => { infoBox.AppendText(Environment.NewLine); infoBox.AppendText(string.Format("An error occurred while converting this file, please check the logs."));; infoBox.AppendText(Environment.NewLine); infoBox.AppendText(Environment.NewLine); }); } (sender as BackgroundWorker).ReportProgress((i + 1) * 100 / VSD_FILES.Length); } Application.Current.Dispatcher.Invoke(() => { infoBox.AppendText("All the files were converted!"); infoBox.AppendText(Environment.NewLine); infoBox.AppendText("The application can now be closed."); infoBox.AppendText(Environment.NewLine); }); } catch (ArgumentNullException) { ShowMessage("This application requires Visio to make the conversion.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); Application.Current.Dispatcher.Invoke(() => { infoBox.AppendText(Environment.NewLine); infoBox.AppendText("------------------------------");; infoBox.AppendText(Environment.NewLine); infoBox.AppendText("The conversion process failed.");; infoBox.AppendText(Environment.NewLine); }); } catch (Exception exception) { CreateLog(exception.ToString()); String logPath = AppDomain.CurrentDomain.BaseDirectory + "error.log"; ShowMessage("An error occurred while checking the processes\nA log file has been created in " + logPath, "Error", MessageBoxButton.OK, MessageBoxImage.Error); Application.Current.Dispatcher.Invoke(() => { infoBox.AppendText(Environment.NewLine); infoBox.AppendText("------------------------------");; infoBox.AppendText(Environment.NewLine); infoBox.AppendText("The conversion process failed.");; infoBox.AppendText(Environment.NewLine); }); } finally { // Close visio if an unexpected error occured if (VisioInst != null) { // Close visio instance VisioInst.Quit(); VisioInst = null; } } Application.Current.Dispatcher.Invoke(() => { infoBox.AppendText(Environment.NewLine); infoBox.AppendText("------------------------------");; infoBox.AppendText(Environment.NewLine); infoBox.AppendText("All tasks finished");; infoBox.AppendText(Environment.NewLine); }); }