public static void ApplyThemeToPresentation(string presentationFile, string outputFile) { //Instantiate Presentation class to load the source presentation file Presentation srcPres = new Presentation(presentationFile); //Instantiate Presentation class for destination presentation (where slide is to be cloned) Presentation destPres = new Presentation(outputFile); //Instantiate ISlide from the collection of slides in source presentation along with //master slide ISlide SourceSlide = srcPres.Slides[0]; //Clone the desired master slide from the source presentation to the collection of masters in the //destination presentation IMasterSlideCollection masters = destPres.Masters; IMasterSlide SourceMaster = SourceSlide.LayoutSlide.MasterSlide; //Clone the desired master slide from the source presentation to the collection of masters in the //destination presentation IMasterSlide iSlide = masters.AddClone(SourceMaster); //Clone the desired slide from the source presentation with the desired master to the end of the //collection of slides in the destination presentation ISlideCollection slds = destPres.Slides; slds.AddClone(SourceSlide, iSlide, true); //Clone the desired master slide from the source presentation to the collection of masters in the//destination presentation //Save the destination presentation to disk destPres.Save(outputFile, SaveFormat.Pptx); }
/// <summary> /// 合并ppt:依次合并files中的ppt文件 /// </summary> /// <param name="files">要合并的文件名列表</param> /// <param name="desPPT">合并后的文件名</param> private void MergePPT(List <string> files, ref string desPPT) { using (Presentation destPres = new Presentation(dataDir + files[0])) { for (int i = 1; i < files.Count; i++) { using (Presentation srcPres = new Presentation(dataDir + files[i])) { //Instantiate ISlide from the collection of slides in source presentation along with //master slide ISlide SourceSlide = srcPres.Slides[0]; IMasterSlide SourceMaster = SourceSlide.LayoutSlide.MasterSlide; //Clone the desired master slide from the source presentation to the collection of masters in the //destination presentation IMasterSlideCollection masters = destPres.Masters; IMasterSlide DestMaster = SourceSlide.LayoutSlide.MasterSlide; //Clone the desired master slide from the source presentation to the collection of masters in the //destination presentation IMasterSlide iSlide = masters.AddClone(SourceMaster); //Clone the desired slide from the source presentation with the desired master to the end of the //collection of slides in the destination presentation ISlideCollection slds = destPres.Slides; slds.AddClone(SourceSlide, iSlide, true); //Clone the desired master slide from the source presentation to the collection of masters in the //destination presentation //Save the destination presentation to disk } } //destPres.Slides[0].Remove(); desPPT = DateTime.Now.ToString("yyyyMMddhhmmsss") + ".ppt"; destPres.Save(dataDir + desPPT, SaveFormat.Ppt); } }
public static void Run() { //ExStart:CloneToAnotherPresentationWithMaster // The path to the documents directory. string dataDir = RunExamples.GetDataDir_Slides_Presentations_CRUD(); // Instantiate Presentation class to load the source presentation file using (Presentation srcPres = new Presentation(dataDir + "CloneToAnotherPresentationWithMaster.pptx")) { // Instantiate Presentation class for destination presentation (where slide is to be cloned) using (Presentation destPres = new Presentation()) { // Instantiate ISlide from the collection of slides in source presentation along with // Master slide ISlide SourceSlide = srcPres.Slides[0]; IMasterSlide SourceMaster = SourceSlide.LayoutSlide.MasterSlide; // Clone the desired master slide from the source presentation to the collection of masters in the // Destination presentation IMasterSlideCollection masters = destPres.Masters; IMasterSlide DestMaster = SourceSlide.LayoutSlide.MasterSlide; // Clone the desired master slide from the source presentation to the collection of masters in the // Destination presentation IMasterSlide iSlide = masters.AddClone(SourceMaster); // Clone the desired slide from the source presentation with the desired master to the end of the // Collection of slides in the destination presentation ISlideCollection slds = destPres.Slides; slds.AddClone(SourceSlide, iSlide, true); // Clone the desired master slide from the source presentation to the collection of masters in the // Destination presentation // Save the destination presentation to disk destPres.Save(dataDir + "CloneToAnotherPresentationWithMaster_out.pptx", SaveFormat.Pptx); } } //ExEnd:CloneToAnotherPresentationWithMaster }
/// <summary> /// 根据模板生成ppt /// </summary> /// <param name="?"></param> /// <returns></returns> private bool CreatePPT(string code, string jclb, string wtms) { bool result = true; try { using (Presentation destPres = new Presentation()) { using (Presentation srcPres = new Presentation(dataDir + "问题检查模板.ppt")) { //Instantiate ISlide from the collection of slides in source presentation along with //master slide ISlide SourceSlide = srcPres.Slides[0]; IMasterSlide SourceMaster = SourceSlide.LayoutSlide.MasterSlide; //Clone the desired master slide from the source presentation to the collection of masters in the //destination presentation IMasterSlideCollection masters = destPres.Masters; IMasterSlide DestMaster = SourceSlide.LayoutSlide.MasterSlide; //Clone the desired master slide from the source presentation to the collection of masters in the //destination presentation IMasterSlide iSlide = masters.AddClone(SourceMaster); //Clone the desired slide from the source presentation with the desired master to the end of the //collection of slides in the destination presentation ISlideCollection slds = destPres.Slides; slds.AddClone(SourceSlide, iSlide, true); //Clone the desired master slide from the source presentation to the collection of masters in the //destination presentation //Save the destination presentation to disk destPres.Slides[0].Remove(); ISlide slide = destPres.Slides[0]; IAutoShape ashp = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 50, 100, 50); // Remove any fill style associated with the AutoShape ashp.FillFormat.FillType = FillType.NoFill; ashp.Hidden = true; ashp.Name = "code"; ashp.AlternativeText = "hiddenfield"; // Access the TextFrame associated with the AutoShape ITextFrame tf = ashp.TextFrame; tf.Text = "#code#" + code; //Iterating through all shapes inside the slide for (int i = 0; i < slide.Shapes.Count; i++) { if (slide.Shapes[i].AsISlideComponent.GetType() == typeof(Aspose.Slides.AutoShape)) { if (((IAutoShape)slide.Shapes[i]).TextFrame.Text == "检查类别") { ((IAutoShape)slide.Shapes[i]).TextFrame.Text = jclb; } if (((IAutoShape)slide.Shapes[i]).TextFrame.Text == "问题描述") { ((IAutoShape)slide.Shapes[i]).TextFrame.Text = wtms; } /*** 2003版本slide.Shapes[i]).Name值会丢失 * if (((IAutoShape)slide.Shapes[i]).Name == "code") * { * * code= ((IAutoShape)slide.Shapes[i]).TextFrame.Text; * } ****/ if (((IAutoShape)slide.Shapes[i]).TextFrame.Text.Contains("#code#")) { code = ((IAutoShape)slide.Shapes[i]).TextFrame.Text.Replace("#code#", ""); } } } destPres.Save(dataDir + code + ".ppt", SaveFormat.Ppt); } } } catch (Exception ex) { result = false; } return(result); }
/************************** * 如模板文件如为pptx格式,生成文件为ppt,生成后的ppt中的问题描述字体颜色会发生变化,因此需要将模板文件更改为ppt格式,但ppt格式中的 * 隐藏shape.name会在生成的ppt中丢失,采取TextFrame.Text添加标记来区分是否是隐藏元素,并将需要存储的值存于TextFrame.Text中 ***************************/ private void btn_singlegenarate_Click(object sender, EventArgs e) { using (Presentation destPres = new Presentation()) { using (Presentation srcPres = new Presentation(dataDir + "问题检查模板.ppt")) { //Instantiate ISlide from the collection of slides in source presentation along with //master slide ISlide SourceSlide = srcPres.Slides[0]; IMasterSlide SourceMaster = SourceSlide.LayoutSlide.MasterSlide; //Clone the desired master slide from the source presentation to the collection of masters in the //destination presentation IMasterSlideCollection masters = destPres.Masters; IMasterSlide DestMaster = SourceSlide.LayoutSlide.MasterSlide; //Clone the desired master slide from the source presentation to the collection of masters in the //destination presentation IMasterSlide iSlide = masters.AddClone(SourceMaster); //Clone the desired slide from the source presentation with the desired master to the end of the //collection of slides in the destination presentation ISlideCollection slds = destPres.Slides; slds.AddClone(SourceSlide, iSlide, true); //Clone the desired master slide from the source presentation to the collection of masters in the //destination presentation //Save the destination presentation to disk destPres.Slides[0].Remove(); foreach (ISlide slide in slds) { //Iterating through all shapes inside the slide for (int i = 0; i < slide.Shapes.Count; i++) { //If the alternative text of the slide matches with the required one then //return the shape //if (slide.Shapes[i].AlternativeText.CompareTo(alttext) == 0) // return slide.Shapes[i]; //if (slide.Shapes[i].AlternativeText == "检查类别") // slide.Shapes[i].AlternativeText = "123456"; if (slide.Shapes[i].Placeholder != null) { //Change the text of each placeholder ((IAutoShape)slide.Shapes[i]).TextFrame.Text = "This is Placeholder"; } } } ISlide sld = destPres.Slides[0]; //// Add an AutoShape of Rectangle type //IAutoShape ashp = sld.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 200, 50); //// Remove any fill style associated with the AutoShape //ashp.FillFormat.FillType = FillType.NoFill; ////ashp.UseBackgroundFill = true; ////ashp.LineFormat.Width = 0; //ashp.LineFormat.FillFormat.FillType = FillType.NoFill;//设置无边框 ////ashp.LineFormat.FillFormat.SolidFillColor.Color = Color.Red; //// Access the TextFrame associated with the AutoShape //ITextFrame tf = ashp.TextFrame; ////tf.TextFrameFormat. //tf.Text = "Aspose TextBox22"; //// Access the Portion associated with the TextFrame //IPortion port = tf.Paragraphs[0].Portions[0]; //// Set the Font for the Portion //port.PortionFormat.LatinFont = new FontData("Times New Roman"); //// Set Bold property of the Font //port.PortionFormat.FontBold = NullableBool.True; //// Set Italic property of the Font //port.PortionFormat.FontItalic = NullableBool.True; //// Set Underline property of the Font //port.PortionFormat.FontUnderline = TextUnderlineType.Single; //// Set the Height of the Font //port.PortionFormat.FontHeight = 25; //// Set the color of the Font //port.PortionFormat.FillFormat.FillType = FillType.Solid; //port.PortionFormat.FillFormat.SolidFillColor.Color = Color.Blue; // ExEnd:SetTextFontProperties // Write the PPTX to disk //presentation.Save(dataDir + "SetTextFontProperties.pptx", SaveFormat.Pptx); IAutoShape ashp2 = sld.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 50, 100, 50); // Remove any fill style associated with the AutoShape ashp2.FillFormat.FillType = FillType.NoFill; ashp2.Hidden = true; ashp2.Name = "code"; ashp2.AlternativeText = "hiddenfield"; // Access the TextFrame associated with the AutoShape ITextFrame tf2 = ashp2.TextFrame; tf2.Text = "#code#这是code值"; destPres.Save(dataDir + "single.ppt", SaveFormat.Ppt); } } }