Introduction
In combination with the previous project and the current project, office files are used for online preview. Currently, ntko control purchased by the company is used in the project, this control prompts you to install the trusted plug-in every time you browse the file, which is cumbersome and has poor browsing performance. When it comes to online preview of office files, the best effect should be the effect of Baidu Library, so today I am busy with myself.
Knowledge points used
1. Convert the Office file to a PDF file. Directly use the. NET Class Library: Microsoft. Office. InterOP. Excel, Microsoft. Office. InterOP. PowerPoint, Microsoft. Office. InterOP. Word, and office. I have installed office2013 on my local machine, so I chose 12.0.
2. Use swftools to convert a PDF file to a SWF file.
3. Use the well-known flexpaper to browse SWF files (watermarks are available during previewing and I don't know how to remove them ).
Problems encountered during the demo process
1. Prompt: "The interoperability type Microsoft. Office. InterOP. Word. applicationclass cannot be embedded. Use the interface that you want to use"
Solution: Right-click the DLL and change the embedded interoperability type to false.
2. Use office. dll when msotristate. msotrue Enumeration type parameters are used. I didn't reference this file at the beginning.
3. When generating the SWF file, you need to pass in the complete path. At the beginning, I only passed in the path without the SWF file name. I tried it several times but failed.
Convert code
Public class officehelper {// <summary> // word to PDF /// </Summary> /// <Param name = "srcfilepath"> </param> // /<Param name = "targetfilepath"> </param> // <returns> </returns> Public static bool wordtopdf (string srcfilepath, string targetfilepath) {bool rs = false; Microsoft. office. interOP. word. wdexportformat exportformat = Microsoft. office. interOP. word. wdexportformat. wdexportformatpdf; microsof T. 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 (srcfilepath); document. saveas (); document. exportasfixedformat (targetfilepath, exportformat); RS = true;} catch (exception) {rs = false; throw;} Finally {If (document! = NULL) {document. Close (); document = NULL;} If (application! = NULL) {application. quit (); Application = NULL;} GC. collect (); GC. waitforpendingfinalizers ();} Return Rs ;} /// <summary> // EXCEL to PDF /// </Summary> /// <Param name = "srcfilepath"> </param> // <Param name = "targetfilepath"> </param> // <returns> </returns> Public static bool exceltopdf (string srcfilepath, string targetfilepath) {bool rs = false; Microsoft. office. interOP. excel. xlfixedformatty PE exportformat = Microsoft. office. interOP. excel. xlfixedformattype. xltypepdf; Microsoft. office. interOP. excel. applicationclass application = NULL; Microsoft. office. interOP. excel. workbook document = NULL; try {application = new Microsoft. office. interOP. excel. applicationclass (); application. visible = false; document = application. workbooks. open (srcfilepath); document. saveas (); document. exportasfixed Format (exportformat, targetfilepath); RS = true;} catch (exception) {rs = false; throw;} finally {If (document! = NULL) {document. Close (); document = NULL;} If (application! = NULL) {application. quit (); Application = NULL;} GC. collect (); GC. waitforpendingfinalizers ();} Return Rs ;} /// <summary> // PPT to PDF /// </Summary> /// <Param name = "srcfilepath"> </param> // <Param name = "targetfilepath"> </param> // <returns> </returns> Public static bool ppttopdf (string srcfilepath, string targetfilepath) {bool result; Microsoft. office. interOP. powerpoint. ppsaveasfiletype t Argetfiletype = 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. applicationclass (); persentation = application. presentations. open (srcfilepath, msotr Istate. msotrue, msotristate. msofalse, msotristate. msofalse); persentation. saveas (targetfilepath, 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> /// PDF to SwF /// </Summary> /// <Param name = "swftools"> SWF Conversion Tool Path </param> // /<Param name = "srcfilepath"> </param> // <Param name = "targetfilepath"> </param> // <returns> </returns> Public static bool implements toswf (string toolspath, string cmd) {bool ISS = false; // determines whether the conversion is successful. The default conversion fails. Try {using (PROCESS p = new process () {processstartinfo psi = new processstartinfo (toolspath, CMD); p. startinfo = PSI; p. start (); p. waitforexit (); ISS = true; // conversion successful} catch {} return ISS ;}}
/// <Summary> /// convert the PDF file to SwF /// </Summary> /// <Param name = "swftools"> Conversion Tool Path </param> // /<Param name = "pdfpath"> pdf file directory </param> // <Param name = "pdffilename"> pdf file name </param> // <Param name =" despath "> Save SWF path </param> // <returns> </returns> protected string protected toswf (string swftools, string pdfpath, string pdffilename, string despath) {string filefullname = path. combine (pdfpath, pdffilename); string filefullnamewithoutex = path. getfilenamewithoutextension (pdffilename); string ext = path. getextension (pdffilename ). tolower (); string saveswfpath = despath + filefullnamewithoutex + ". SWF "; string rs = filefullnamewithoutex + ". SWF "; string comment STR ="-T \ "" + filefullname + "\"-S flashversion = 9-O \ "" + saveswfpath + "\""; bool ISS = officehelper. export toswf (swftools, writable Str); Return Rs ;} /// <summary> /// convert the Office file to a PDF file /// </Summary> /// <Param name = "officepath"> Save the Office file path </param>/ // <Param name = "officefilename"> Office file name </param> // <Param name = "pdfpath"> Save the PDF path </param> protected string officetopdf (string officepath, string officefilename, string pdfpath) {string fullpathname = path. combine (officepath, officefilename); string filenamewithoutex = path. getfilenamewithoutextension (officefilename); string ext = path. getextension (officefilename ). tolower (); string savepdfpath = pdfpath + filenamewithoutex + ". PDF "; string retvalue = filenamewithoutex + ". PDF "; Switch (EXT) {Case ". doc ": officehelper. wordtopdf (fullpathname, savepdfpath); break; case ". docx ": officehelper. wordtopdf (fullpathname, savepdfpath); break; case ". xls ": officehelper. exceltopdf (fullpathname, savepdfpath); break; case ". XLSX ": officehelper. exceltopdf (fullpathname, savepdfpath); break; case ". ppt ": officehelper. ppttopdf (fullpathname, savepdfpath); break; case ". pptx ": officehelper. ppttopdf (fullpathname, savepdfpath); break;} return retvalue ;}
Reference
During the demo process, I learned and referenced the articles of the two bloggers. I would like to express my gratitude to you.
Wolfy: http://www.cnblogs.com/wolf-sun/p/3569960.html
Quiet to SLIM: http://www.cnblogs.com/zzPrince/p/3378336.html
Online Preview of office files [similar to Baidu Library]