public void Export(String path, String width, String height) { if (!HasActivePresentation()) { return; } String Path = String.IsNullOrWhiteSpace(path) ? ActivePresentation.Path + "/" + ActivePresentation.Name.Split('.')[0] : path; //目录不存在则创建 if (!Directory.Exists(Path)) { Directory.CreateDirectory(Path); } int ScaleWidth = String.IsNullOrWhiteSpace(width) || !Uint_Regex.IsMatch(width) ? 0 : Convert.ToInt32(width); int ScaleHeight = String.IsNullOrWhiteSpace(height) || !Uint_Regex.IsMatch(height) ? 0 : Convert.ToInt32(height); try { ActivePresentation.Export(Path.Replace("/", "\\"), "jpg", ScaleWidth, ScaleHeight); } catch (Exception e) { WriteError("导出失败:" + e.Message, _OutputFormat, MethodBase.GetCurrentMethod()); } }
internal static void CreateSlides(string PPTpath) { try { PowerPointApp = new PowerPoint.Application(); var presentation = PowerPointApp.Presentations; Presentation = presentation.Open2007(PPTpath, MsoTriState.msoCTrue, MsoTriState.msoCTrue, MsoTriState.msoFalse, MsoTriState.msoFalse); var imageSlidesDirectory = System.IO.Directory.GetParent(PPTpath) + "\\ImageSlides"; if (System.IO.Directory.Exists(imageSlidesDirectory)) { System.IO.Directory.Delete(imageSlidesDirectory, true); } System.IO.Directory.CreateDirectory(imageSlidesDirectory); Presentation.Export(imageSlidesDirectory, "JPG", 0, 0); } catch (Exception ex) { throw ex; } finally { Close(); } }
private void PowerpointToJPG(string file) { FileInfo inf = new FileInfo(file); string folder = GetFolderName(inf); string directory = inf.DirectoryName + "\\" + folder; Console.WriteLine("Final directory: " + directory); if (Directory.Exists(directory)) { if (MessageBox.Show("A folder with the name '" + folder + "' already exists in the current directory.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) == DialogResult.OK) { return; } } else { textBox1.Text += "Processing : " + inf.Name + "\r\n"; Powerpoint.Application pptApp = new Powerpoint.Application(); Powerpoint.Presentation pptPresentation = pptApp.Presentations.Open(file, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse); pptPresentation.Export(directory, "JPG"); } }
//private static OfficeToXpsConversionResult ConvertFromWord(string sourceFilePath, ref string resultFilePath) //{ // object pSourceDocPath = sourceFilePath; // string pExportFilePath = string.IsNullOrEmpty(resultFilePath) ? GetTempXpsFilePath() : resultFilePath; // try // { // var pExportFormat = Word.WdExportFormat.wdExportFormatXPS; // bool pOpenAfterExport = false; // var pExportOptimizeFor = Word.WdExportOptimizeFor.wdExportOptimizeForOnScreen; // var pExportRange = Word.WdExportRange.wdExportAllDocument; // int pStartPage = 0; // int pEndPage = 0; // var pExportItem = Word.WdExportItem.wdExportDocumentContent; // var pIncludeDocProps = true; // var pKeepIRM = true; // var pCreateBookmarks = Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks; // var pDocStructureTags = true; // var pBitmapMissingFonts = true; // var pUseISO19005_1 = false; // Word.Application wordApplication = null; // Word.Document wordDocument = null; // try // { // wordApplication = new Word.Application(); // } // catch (Exception exc) // { // return new OfficeToXpsConversionResult(ConversionResult.ErrorUnableToInitializeOfficeApp, "Word", exc); // } // try // { // try // { // wordDocument = wordApplication.Documents.Open(ref pSourceDocPath); // } // catch (Exception exc) // { // return new OfficeToXpsConversionResult(ConversionResult.ErrorUnableToOpenOfficeFile, exc.Message, exc); // } // if (wordDocument != null) // { // try // { // wordDocument.ExportAsFixedFormat( // pExportFilePath, // pExportFormat, // pOpenAfterExport, // pExportOptimizeFor, // pExportRange, // pStartPage, // pEndPage, // pExportItem, // pIncludeDocProps, // pKeepIRM, // pCreateBookmarks, // pDocStructureTags, // pBitmapMissingFonts, // pUseISO19005_1 // ); // } // catch (Exception exc) // { // return new OfficeToXpsConversionResult(ConversionResult.ErrorUnableToExportToXps, "Word", exc); // } // } // else // { // return new OfficeToXpsConversionResult(ConversionResult.ErrorUnableToOpenOfficeFile); // } // } // finally // { // // Close and release the Document object. // if (wordDocument != null) // { // wordDocument.Close(); // wordDocument = null; // } // // Quit Word and release the ApplicationClass object. // if (wordApplication != null) // { // wordApplication.Quit(); // wordApplication = null; // } // GC.Collect(); // GC.WaitForPendingFinalizers(); // GC.Collect(); // GC.WaitForPendingFinalizers(); // } // } // catch (Exception exc) // { // return new OfficeToXpsConversionResult(ConversionResult.ErrorUnableToAccessOfficeInterop, "Word", exc); // } // resultFilePath = pExportFilePath; // return new OfficeToXpsConversionResult(ConversionResult.OK, pExportFilePath); //} // private static OfficeToXpsConversionResult ConvertFromExcel(string sourceFilePath, ref string resultFilePath) // { // string pSourceDocPath = sourceFilePath; // string pExportFilePath = string.IsNullOrEmpty(resultFilePath) ? GetTempXpsFilePath() : resultFilePath; // try // { // var pExportFormat = Excel.XlFixedFormatType.xlTypeXPS; // var pExportQuality = Excel.XlFixedFormatQuality.xlQualityStandard; // var pOpenAfterPublish = false; // var pIncludeDocProps = true; // var pIgnorePrintAreas = true; // Excel.Application excelApplication = null; // Excel.Workbook excelWorkbook = null; // try // { // excelApplication = new Excel.Application(); // } // catch (Exception exc) // { // return new OfficeToXpsConversionResult(ConversionResult.ErrorUnableToInitializeOfficeApp, "Excel", exc); // } // try // { // try // { // excelWorkbook = excelApplication.Workbooks.Open(pSourceDocPath); // } // catch (Exception exc) // { // return new OfficeToXpsConversionResult(ConversionResult.ErrorUnableToOpenOfficeFile, exc.Message, exc); // } // if (excelWorkbook != null) // { // try // { // excelWorkbook.ExportAsFixedFormat( // pExportFormat, // pExportFilePath, // pExportQuality, // pIncludeDocProps, // pIgnorePrintAreas, // OpenAfterPublish : pOpenAfterPublish // ); // } // catch (Exception exc) // { // return new OfficeToXpsConversionResult(ConversionResult.ErrorUnableToExportToXps, "Excel", exc); // } // } // else // { // return new OfficeToXpsConversionResult(ConversionResult.ErrorUnableToOpenOfficeFile); // } // } // finally // { // // Close and release the Document object. // if (excelWorkbook != null) // { // excelWorkbook.Close(); // excelWorkbook = null; // } // // Quit Word and release the ApplicationClass object. // if (excelApplication != null) // { // excelApplication.Quit(); // excelApplication = null; // } // GC.Collect(); // GC.WaitForPendingFinalizers(); // GC.Collect(); // GC.WaitForPendingFinalizers(); // } // } // catch (Exception exc) // { // return new OfficeToXpsConversionResult(ConversionResult.ErrorUnableToAccessOfficeInterop, "Excel", exc); // } // resultFilePath = pExportFilePath; // return new OfficeToXpsConversionResult(ConversionResult.OK, pExportFilePath); // } private static OfficeToXpsConversionResult ConvertFromPowerPoint(string sourceFilePath, ref string resultFilePath, ref int resultCount) { resultCount = 0; string pSourceDocPath = sourceFilePath; string pExportFilePath = string.IsNullOrEmpty(resultFilePath) ? GetTempXpsFilePath() : resultFilePath; try { PowerPoint.Application pptApplication = null; PowerPoint.Presentation pptPresentation = null; try { pptApplication = new PowerPoint.Application(); } catch (Exception exc) { return(new OfficeToXpsConversionResult(ConversionResult.ErrorUnableToInitializeOfficeApp, "PowerPoint", exc)); } try { try { pptPresentation = pptApplication.Presentations.Open(pSourceDocPath, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse); } catch (Exception exc) { return(new OfficeToXpsConversionResult(ConversionResult.ErrorUnableToOpenOfficeFile, exc.Message, exc)); } if (pptPresentation != null) { try { pptPresentation.Export( pExportFilePath, "JPG", (int)System.Windows.SystemParameters.PrimaryScreenWidth, (int)System.Windows.SystemParameters.PrimaryScreenHeight ); resultCount = pptPresentation.Slides.Count; } catch (Exception exc) { System.Windows.MessageBox.Show(exc.ToString()); return(new OfficeToXpsConversionResult(ConversionResult.ErrorUnableToExportToXps, "PowerPoint", exc)); } } else { return(new OfficeToXpsConversionResult(ConversionResult.ErrorUnableToOpenOfficeFile)); } } finally { // Close and release the Document object. if (pptPresentation != null) { pptPresentation.Close(); pptPresentation = null; } // Quit Word and release the ApplicationClass object. if (pptApplication != null) { pptApplication.Quit(); pptApplication = null; } GC.Collect(); GC.WaitForPendingFinalizers(); } } catch (Exception exc) { return(new OfficeToXpsConversionResult(ConversionResult.ErrorUnableToAccessOfficeInterop, "PowerPoint", exc)); } resultFilePath = pExportFilePath; return(new OfficeToXpsConversionResult(ConversionResult.OK, pExportFilePath)); }