C # converting MS-Office documents to Pdf (Word, Execel, PowerPoint, Visio, Project)

Source: Internet
Author: User

Directly provide the source code, runtime environment: Office2010, Project2010 (if needed), Visio2010 (if needed ),. net framework2.0 [csharp] using System; using Microsoft. office. core; namespace Office {class Util {private Util () {} // <summary> // convert a Word file to a PDF file // </summary> // <param name = "sourcePath"> source file path </param> /// <param name = "targetPath"> target file path </param> /// <returns> true = Conversion successful </returns> public static bool WordToPDF (string sourcePath, st Ring targetPath) {bool result = false; Microsoft. office. interop. word. wdExportFormat exportFormat = Microsoft. office. interop. word. wdExportFormat. wdExportFormatPDF; Microsoft. office. interop. word. applicationClass application = null; Microsoft. office. interop. word. document document = null; try {application = new Microsoft. office. interop. word. applicationClass (); application. visible = false; document = Application. documents. open (sourcePath); document. saveAs2 (); document. exportAsFixedFormat (targetPath, exportFormat); result = true;} catch (Exception e) {Console. writeLine (e. message); result = false;} finally {if (document! = Null) {document. Close (); document = null;} if (application! = Null) {application. quit (); application = null;} GC. collect (); GC. waitForPendingFinalizers (); GC. collect (); GC. waitForPendingFinalizers ();} return result;} // <summary>. office. interop. convert an Excel file to a PDF file /// </summary> /// <param name = "sourcePath"> source file path </param> /// <param name = "targetPath "> target file path </param> // <returns> true = Conversion successful </returns> public static bool ExcelToPDF (string source Path, string targetPath) {bool result = false; Microsoft. office. interop. excel. xlFixedFormatType targetType = Microsoft. office. interop. excel. xlFixedFormatType. xlTypePDF; object missing = Type. missing; Microsoft. office. interop. excel. applicationClass application = null; Microsoft. office. interop. excel. workbook workBook = null; try {application = new Microsoft. office. interop. excel. applicationClass (); Application. visible = false; workBook = application. workbooks. open (sourcePath); workBook. saveAs (); workBook. exportAsFixedFormat (targetType, targetPath); result = true;} catch (Exception e) {Console. writeLine (e. message); result = false;} finally {if (workBook! = Null) {workBook. Close (true, missing, missing); workBook = null;} if (application! = Null) {application. quit (); application = null;} GC. collect (); GC. waitForPendingFinalizers (); GC. collect (); GC. waitForPendingFinalizers ();} return result ;} /// <summary> /// convert a PowerPoint file to a PDF file // </summary> /// <param name = "sourcePath"> source file path </param >/// <param name = "targetPath"> target file path </param> /// <returns> true = Conversion successful </returns> public static bool PowerPointToPDF (string sourcePath, string ta RgetPath) {bool result; Microsoft. office. interop. powerPoint. ppSaveAsFileType targetFileType = Microsoft. office. interop. powerPoint. ppSaveAsFileType. ppSaveAsPDF; object missing = Type. missing; Microsoft. office. interop. powerPoint. applicationClass application = null; Microsoft. office. interop. powerPoint. presentation persentation = null; try {application = new Microsoft. office. interop. powerPoint. appli CationClass (); // application. visible = MsoTriState. msoFalse; persentation = application. presentations. open (sourcePath, MsoTriState. msoTrue, MsoTriState. msoFalse, MsoTriState. msoFalse); persentation. saveAs (targetPath, targetFileType, Microsoft. office. core. msoTriState. msoTrue); result = true;} catch (Exception e) {Console. writeLine (e. message); 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 ;} /// <summary> /// convert the Visio file to a PDF file // </summary> /// <param name = "sourcePath"> source file path </param >/// <param name = "targetPath"> target file path </param> /// <returns> true = Conversion successful </returns> public static bool VisioToPDF (string sourcePath, string targetPath) {Bool result; Microsoft. office. interop. visio. visFixedFormatTypes targetType = Microsoft. office. interop. visio. visFixedFormatTypes. visFixedFormatPDF; object missing = Type. missing; Microsoft. office. interop. visio. applicationClass application = null; Microsoft. office. interop. visio. document document = null; try {application = new Microsoft. office. interop. visio. applicationClass (); application. visible = False; document = application. documents. open (sourcePath); document. save (); document. exportAsFixedFormat (targetType, targetPath, Microsoft. office. interop. visio. visDocExIntent. visDocExIntentScreen, Microsoft. office. interop. visio. visPrintOutRange. visPrintAll); result = true;} catch (Exception e) {Console. writeLine (e. message); result = false;} finally {if (application! = Null) {application. quit (); application = null;} GC. collect (); GC. waitForPendingFinalizers (); GC. collect (); GC. waitForPendingFinalizers ();} return result ;} /// <summary> /// convert the Project file to a PDF file // </summary> /// <param name = "sourcePath"> source file path </param >/// <param name = "targetPath"> target file path </param> /// <returns> true = Conversion successful </returns> public static bool ProjectToPDF (string sourcePath, string targetPa Th) {bool result; object missing = Type. missing; Microsoft. office. interop. MSProject. applicationClass application = null; try {application = new Microsoft. office. interop. MSProject. applicationClass (); application. visible = false; application. fileOpenEx (sourcePath); application. documentExport (targetPath, Microsoft. office. interop. MSProject. pjDocExportType. pjPDF); result = true; www.2cto.com} catch (Exception e) {Console. WriteLine (e. Message); result = false;} finally {if (application! = Null) {application. docClose (); application. quit (); application = null;} GC. collect (); GC. waitForPendingFinalizers (); GC. collect (); GC. waitForPendingFinalizers ();} return result ;}} If You Need To transcode other documents such as Publish or convert the document to another format (such as xps), refer to the above Code. The principle is actually very simple. For example, if you click "Save as", many formats will appear for you to choose from. This interface is called here. I have tried to use Apache OpenOffice for transcoding. Word, Execel, and Ppt can barely be converted (no complicated graphics), but the effect is not good. OpenOffice is powerless for other document types, later, we used the original Office interface for simplicity. The purpose of transcoding (Benefit): You can preview the document directly in the browser (because of the need for project team document sharing, so we developed such a thing, in combination with WebServer, transcode the file immediately after it is uploaded. Click this document to return to the transcoded file. The browser can directly browse the file, which is very convenient ~) This article briefly summarizes the advantages of using C #: 100% Perfect transcoding, Perfect! Insufficient: Only windows servers can be deployed ~ Advantages of using OpenOffice: cross-platform. Insufficient: the transcoding effect is not satisfactory ~ The preceding code is the core code. During the call, the following problems may occur, causing transcoding failure: 1. the document is in use. Solution: copy a copy before transcoding, and then transcode the copy ~ 2. The document is damaged. Solution: Change it after fixing ~ 3. The document has a password. Solution: no attempt ~ 4 .......

Related Article

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.