C # Implementing Office documents into PDF format

Source: Internet
Author: User
Tags microsoft access database

You need to install Office 2007 and a office2007 plug-in Officesaveaspdfandxps

[Url]http://www.microsoft.com/downloads/details.aspx? Familyid=4d951911-3e7e-4ae6-b059-a2e79ed87041&displaylang=en[/url] This is a Microsoft official out of office plugin. OFFICE2010 seems to be able to save files directly as. PDF   After installation, open VS, take VS2005 as an example new Windows Application Project add a reference to the following COM component Microsoft Word 12.0 Object Librarymicrosoft PowerPoint 12.0 Object Librarymicrosoft Excel 12.0 Object library ------------------------------------------------------ Using Word = Microsoft.Office.Interop.Word; Using Excel = Microsoft.Office.Interop.Excel; Using PowerPoint = microsoft.office.interop.powerpoint;using microsoft.office.core;  We can use an enumeration type to determine the type of the generated file Word.wdexportformat wd = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF; Excel.xlfixedformattype exceltype = Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF; Powerpoint.ppsaveasfiletype pptype = Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPDF;

//Convert Word document to PDF format     private bool Convert (string SourcePath, String TargetPath, Word.wdexportformat ExportFormat)     {        bool result;          object parammissing = Type.Missing;         Word.applicationclass wordapplication = new Word.applicationclass ();         Word.Document worddocument = null;         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);                 worddocument = NULL;            }              if (wordapplication! = null)              {                Wordapplication.quit (ref parammissing, ref parammissing, ref parammissing);                 wordapplication = null ;            }              GC. Collect ();             GC. WaitForPendingFinalizers ();             GC. Collect ();             GC. WaitForPendingFinalizers ();        } &NBsp;       return result;    }

   //Convert an Excel document to PDF format     private bool Convert (string SourcePath, String TargetPath, Xlfixedformattype targetType)     {        bool result;          object missing = Type.Missing;         Excel.applicationclass application = null;         Workbook Workbook = null;         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, Xlfixedformatquality.xlqualitystandard, True, false, missing, missing, missing, missing);             result = true;        }         Catch          {            result = false;         }         finally          {            if (workBook! = null)  & nbsp;          {                 Workbook.close (True, missing, missing);                 workBook = null;            }              if (application! = NULL)              {                application. Quit ();                 application = null;            }              GC. Collect ();             GC. WaitForPendingFinalizers ();             GC. Collect ();             GC. WaitForPendingFinalizers ();        }         return result;    }

   //Convert PPT document to PDF format     private bool Convert (string SourcePath, String TargetPath, PpSaveAsFileType targetfiletype)     {        bool result;  & nbsp;      object missing = Type.Missing;         Powerpoint.applicationclass application = null;         Presentation persentation = null;         try         {             application = new Powerpoint.applicationclass ();             persentation = Application. Presentations.Open (SourcePath, Msotristate.msotrue, Msotristate.msofalse, Msotristate.msofalse);             persentation. SaveAs (TargetPath, Targetfiletype, Microsoft.Office.CorE.msotristate.msotrue);

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

    • Adding Permissions in Component Services
Click StartClick Run, and then type DCOMCNFG. Select the application that you want to run automatically. The application name is as follows: Microsoft Access 97-microsoft Access database Microsoft Access 2000/2002-microsoft Access application Microsoft Excel 9 7/2000/2002-microsoft Excel Application Microsoft Word 97-microsoft Word Basic Microsoft Word 2000/2002-microsoft Word document click PropertiesOpen the Properties dialog box for this application.

1) Mark-user account running this application-the following users; then enter a user in the Administrator user group.

Note: When you change the user password used by Component Services in the server, you must reenter the new password in the component to generate word correctly.

2) Security-launch and Activation permissions, select "Custom", add IIS_WPG user's local launch, local activation permission;

3) Security-access rights, select "Custom", add IIS_WPG user's local access rights;


C # Implementing Office documents into PDF format

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.