C # detailed method for operating PowerPoint,
C # operate the basic code of PowerPoint, including opening the PPT file, reading the slides, inserting slides, and playing automatically.
Using System; using System. collections. generic; using System. linq; using System. text; using OFFICECORE = Microsoft. office. core; using POWERPOINT = Microsoft. office. interop. powerPoint; using System. windows; using System. collections; using System. windows. controls; namespace PPTDraw. PPTOperate {// PPT document operation implementation class. /// public class OperatePPT {# region ========= basic parameter information ===== POWERPOINT. application objApp = null; POWERP OINT. presentation objPresSet = null; POWERPOINT. slideShowWindows objSSWs; POWERPOINT. slideShowTransition objSST; POWERPOINT. slideShowSettings objSSS; POWERPOINT. slideRange objSldRng; bool bAssistantOn; double pixperPoint = 0; double offsetx = 0; double offsety = 0; # endregion # region =========== operation method ==========================///// open the PPT document and play the video. ///// Pptfile path public void PPTOpen (string filePath) {// prevent multiple PPT programs from being opened consecutively. if (this. objApp! = Null) {return;} try {objApp = new POWERPOINT. application (); // open it in Non-read-only mode to save the operation. objPresSet = objApp. presentations. open (filePath, OFFICECORE. msoTriState. msoFalse, OFFICECORE. msoTriState. msoFalse, OFFICECORE. msoTriState. msoFalse); // Prevent Office Assistant from displaying alert messages: bAssistantOn = objApp. assistant. on; objApp. assistant. on = false; objSSS = this. objPresSet. slideShowSettings; objSSS. Run ();} catch (Exception ex) {this. objApp. quit () ;}///// automatically play the PPT document. ///// PPTy file path. /// page flip interval. [In seconds] public void PPTAuto (string filePath, int playTime) {// prevent multiple PPT programs from being opened consecutively. if (this. objApp! = Null) {return;} objApp = new POWERPOINT. application (); objPresSet = objApp. presentations. open (filePath, OFFICECORE. msoTriState. msoCTrue, OFFICECORE. msoTriState. msoFalse, OFFICECORE. msoTriState. msoFalse); // The automatic playback code (start) int Slides = objPresSet. slides. count; int [] SlideIdx = new int [Slides]; for (int I = 0; I <Slides; I ++) {SlideIdx [I] = I + 1 ;}; objSldRng = objPresSet. slides. range (SlideIdx); objSST = ObjSldRng. SlideShowTransition; // set the page turning time. objSST. AdvanceOnTime = OFFICECORE. MsoTriState. msoCTrue; objSST. AdvanceTime = playTime; // special effects during page turning! ObjSST. entryEffect = POWERPOINT. ppEntryEffect. ppEffectCircleOut; // Prevent Office Assistant from displaying alert messages: bAssistantOn = objApp. assistant. on; objApp. assistant. on = false; // Run the Slide show from slides 1 thru 3. objSSS = objPresSet. slideShowSettings; objSSS. startingSlide = 1; objSSS. endingSlide = Slides; objSSS. run (); // Wait for the slide show to end. objSSWs = objApp. slideShowWindows; whi Le (objSSWs. count> = 1) System. threading. thread. sleep (playTime * 100); this. objPresSet. close (); this. objApp. quit () ;}///// next page of the PPT. /// Public void NextSlide () {if (this. objApp! = Null) this. objPresSet. SlideShowWindow. View. Next () ;}//// PPT Previous Page. /// Public void previusslide () {if (this. objApp! = Null) this. objPresSet. SlideShowWindow. View. Previous () ;}//// insert an image into the current PPT page. ///// Array of image object information /// Insert the length from the left of the image // ratio of distance /// whether the image is successfully added! Public bool InsertToSlide (List listObj) {bool InsertSlide = false; if (this. objPresSet! = Null) {this. slideParams (); int slipeint = objPresSet. slideShowWindow. view. currentShowPosition; foreach (PPTOBJ myobj in listObj) {objPresSet. slides [slipeint]. shapes. addPicture (myobj. path, // image Path OFFICECORE. msoTriState. msoFalse, OFFICECORE. msoTriState. msoTrue, (float) (myobj. x-this. offsetx)/this. pixperPoint), // The length (float) (myobj. y/this. pixperPoint), // The height (float) (myobj. width/this. pixp ErPoint), // Insert the image width (float) (myobj. height/this. pixperPoint) // Insert the Image Height);} InsertSlide = true;} return InsertSlide;} // calculate the offset parameter on the InkCanvas, and the image display parameters on the PPT. /// Used for PPT image loading // private void SlideParams () {double slideWidth = this. objPresSet. pageSetup. slideWidth; double slideHeight = this. objPresSet. pageSetup. slideHeight; double inkCanWidth = SystemParameters. primaryScreenWidth; // inkCan. actualWidth; double inkCanHeight = SystemParameters. primaryScreenHeight; // inkCan. actualHeight; if (slideWidth/slideHeight)> (inkCanWidth/inkCanHeight) {this. pixperPoint = InkCanHeight/slideHeight; this. offsetx = 0; this. offsety = (inkCanHeight-slideHeight * this. pixperPoint)/2;} else {this. pixperPoint = inkCanHeight/slideHeight; this. offsety = 0; this. offsetx = (inkCanWidth-slideWidth * this. pixperPoint)/2 ;}///// close the PPT document. /// Public void PPTClose () {// configure the PPT program. If (this. objPresSet! = Null) {// determines whether to exit the program. It can be left empty. // ObjSSWs = objApp. SlideShowWindows; // if (objSSWs. Count> = 1) // {if (MessageBox. Show ("whether to save the modified handwriting! "," Prompt ", MessageBoxButton. OKCancel) = MessageBoxResult. OK) this. objPresSet. save (); //} // this. objPresSet. close ();} if (this. objApp! = Null) this. objApp. Quit (); GC. Collect () ;}# endregion }}