1024programmer Asp.Net Convert Office documents to pdf format (1)

Convert Office documents to pdf format (1)

Convert Office documents to pdf format (1)

There are many ways to convert Office documents (Word, Excel, Powerpoint) into PDF format, the most common one is to use Microsoft Office components. The steps are as follows:

① Open the NuGet package manager and reference the following four components:

MicrosoftOfficeCore

Microsoft.Office.Interop.Word

Microsoft.Office.Interop.Excel

Microsoft.Office.Interop.PowerPoint

② Write Office help class

public static class  OfficeHelper
     {
         /// 
         /// Word to PDF
         /// 
         /// Source file path
         /// Target file path
         /// Export file format
         /// 
         public static bool Convert(string sourcePath,  string targetPath, Word.WdExportFormat exportFormat)
         {
             bool result;
             object paramMissing = Type  .Missing;
             Word.ApplicationClass wordApplication = newWord.ApplicationClass();
             Word.Document wordDocument = newWord.Document();
             try
             {
                 object paramSourceDocPath = sourcePath  ;
                 string paramExportFilePath = targetPath  ;

                 Word.WdExportFormat paramExportFormat = exportFormat;
                 bool paramOpenAfterExport = false  ;
                 Word.WdExportOptimizeFor paramExportOptimizeFor =
                         Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
                 Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;
                 int paramStartPage = 0  ;
                 int paramEndPage = 0  ;
                 Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;
                 bool paramIncludeDocProps = true  ;
                 bool paramKeepIRM = true  ;
                 Word.WdExportCreateBookmarks paramCreateBookmarks =
                         Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
                 bool paramDocStructureTags = true  ;
                 bool paramBitmapMissingFonts = true  ;
                 bool paramUseISO19005_1 = false  ;
                
                 wordDocument = wordApplication.Documents.Open(
                         ref paramSourceDocPath, ref   paramMissing, ref paramMissing  ,
                         ref paramMissing, ref   paramMissing, ref paramMissing  ,
                         ref paramMissing, ref   paramMissing, ref paramMissing  ,
                         ref paramMissing, ref   paramMissing, ref paramMissing  ,
                         ref paramMissing, ref   paramMissing, ref paramMissing  ,
                         ref paramMissing);
                
                 if (wordDocument != null)
                     wordDocument.ExportAsFixedFormat(paramExportFilePath,
                             paramExportFormat, paramOpenAfterExport,
                             paramExportOptimizeFor, paramExportRange, paramStartPage,
                             paramEndPage, paramExportItem, paramIncludeDocProps,
                             paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
                             paramBitmapMissingFonts, paramUseISO19005_1,
                             ref paramMissing);
                 result = true;
             }
             finally
             {
                 if (wordDocument != null)
                 {
                     wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
                 }
                 if (wordApplication != null)
                 {
                     wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
                 }
                 GC.Collect();
                 GC.WaitForPendingFinalizers();
                 GC.Collect();
                 GC.WaitForPendingFinalizers();
             }
             return result;
         }

         /// 
         /// Excel to PDF
         /// 
         /// Source file path
         /// Target file path
         /// Export file format
         /// 
         public static bool Convert(string sourcePath,  string targetPath, Excel.XlFixedFormatType targetType)
         {
             bool result;
             object missing = Type  .Missing;
             Excel.ApplicationClass application = new Excel.ApplicationClass();
             Excel.Workbook workBook = new Excel.Workbook();
             try
             {
                 application = new Excel  .ApplicationClass();
                 object target = targetPath  ;
                 object type = targetType  ;
                 workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
                         missing, missing, missing, missing, missing, missing, missing, missing, missing);

                 workBook.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
                 result = true;
             }
             catch
             {
                 result = false;
             }
             finally
             {
                 if (workBook != null)
                 {
                     workBook.Close(true, missing, missing);
                 }
                 if (application != null)
                 {
                     application.Quit();
                 }
                 GC.Collect();
                 GC.WaitForPendingFinalizers();
                 GC.Collect();
                 GC.WaitForPendingFinalizers();tForPendingFinalizers();
                 GC.Collect();
                 GC.WaitForPendingFinalizers();
             }
             return result;
         }

         /// 
         /// Excel to PDF
         /// 
         /// Source file path
         /// Target file path
         /// Export file format
         /// 
         public static bool Convert(string sourcePath,  string targetPath, Excel.XlFixedFormatType targetType)
         {
             bool result;
             object missing = Type  .Missing;
             Excel.ApplicationClass application = new Excel.ApplicationClass();
             Excel.Workbook workBook = new Excel.Workbook();
             try
             {
                 application = new Excel  .ApplicationClass();
                 object target = targetPath  ;
                 object type = targetType  ;
                 workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
                         missing, missing, missing, missing, missing, missing, missing, missing, missing);

                 workBook.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
                 result = true;
             }
             catch
             {
                 result = false;
             }
             finally
             {
                 if (workBook != null)
                 {
                     workBook.Close(true, missing, missing);
                 }
                 if (application != null)
                 {
                     application.Quit();
                 }
                 GC.Collect();
                 GC.WaitForPendingFinalizers();
                 GC.Collect();
                 GC.WaitForPendingFinalizers();
             }
             return result;
         }

         /// 
         /// Convert PPT to PDF
         /// 
         /// Source file path
         /// Target file path
         /// Export file format
         /// 
         public static bool Convert(string sourcePath,  string targetPath, PowerPoint.PpSaveAsFileType targetFileType)
         {
             bool result;
             object missing = Type  .Missing;
             PowerPoint.ApplicationClass application = new PowerPoint.ApplicationClass();
             PowerPoint.Presentation presentation = null;
             try
             {
                 application = new PowerPoint  .ApplicationClass();
                 presentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
                 presentation.SaveAs(targetPath, targetFileType, MsoTriState.msoTrue);

                 result = true;
             }
             catch
             {
                 result = false;
             }
             finally
             {
                 if (persentation != null)
                 {
                     presentation.Close();
                 }
                 if (application != null)
                 {
                     application.Quit();
                 }
                 GC.Collect();
                 GC.WaitForPendingFinalizers();
                 GC.Collect();
                 GC.WaitForPendingFinalizers();
             }
             return result;
         }
     }

View Code

Finally, you can call it for conversion.

Note:

①This method is currently only available for Windows systems

②This method relies on Microsoft Office software

③Can be used in both .net framework and .net core projects (taking the Win Form project as an example)

}
return result;
}

///


/// Convert PPT to PDF
///


/// Source file path
/// Target file path
/// Export file format
///
public static bool Convert(string sourcePath, string targetPath, PowerPoint.PpSaveAsFileType targetFileType)
{
bool result;
object missing = Type .Missing;
PowerPoint.ApplicationClass application
= new PowerPoint.ApplicationClass();
PowerPoint.Presentation presentation
= null;
try
{
application
= new PowerPoint .ApplicationClass();
presentation
= application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
presentation.SaveAs(targetPath, targetFileType, MsoTriState.msoTrue);

result = true;
}
catch
{
result
= false;
}
finally
{
if (persentation != null)
{
presentation.Close();
}
if (application != null)
{
application.Quit();
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}
}

View Code

Finally, you can call it for conversion.

Note:

①This method is currently only available for Windows systems

②This method relies on Microsoft Office software

③Can be used in both .net framework and .net core projects (taking the Win Form project as an example)

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/811438

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索