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); } }
private void button3_Click(object sender, EventArgs e) { Presentation presentation = new Presentation(); presentation.LoadFromFile(fileName); IMasterSlide master = presentation.Masters[0]; String image = @"logo.png"; RectangleF rff = new RectangleF(40, 40, 100, 80); IEmbedImage pic = master.Shapes.AppendEmbedImage(ShapeType.Rectangle, image, rff); pic.Line.FillFormat.FillType = FillFormatType.None; presentation.Slides.Append(); presentation.SaveToFile("result.pptx", FileFormat.Auto); System.Diagnostics.Process.Start("result.pptx"); }
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> /// Merge documents into one file, saves resulted file to out file with specified format. /// </summary> /// <param name="outFolder">Output folder.</param> /// <param name="format">Output format.</param> /// <param name="styleMasterFile">Master file for style in result file. When null, style not changed from source files.</param> /// <param name="sourceFiles">Source slides files to proceed.</param> /// <returns>Result file path.</returns> public string Merger( string outFolder, SlidesConversionFormat format, string styleMasterFile, params string[] sourceFiles ) { var files = new List <string>(); foreach (var sourceFile in sourceFiles) { if (Directory.Exists(sourceFile)) { files.AddRange(Directory.EnumerateFiles(sourceFile)); } else { files.Add(sourceFile); } } var fileName = Path.GetFileNameWithoutExtension(styleMasterFile ?? files.First()); var resultFile = Path.Combine(outFolder, $"{fileName}.pptx"); using (var resultPresentation = new Presentation()) { var slides = resultPresentation.Slides; while (slides.Any()) { slides.Remove(slides.First()); } var masters = resultPresentation.Masters; Presentation masterPresentation = null; IMasterSlide masterSlide = null; float maxWidth = 0, maxHeight = 0; if (styleMasterFile != null) { masterPresentation = new Presentation(styleMasterFile); foreach (var master in masterPresentation.Masters) { masterSlide = masters.AddClone(master); } } try { foreach (var sourceFile in files) { using (var sourcePresentation = new Presentation(sourceFile)) { var sourceSize = sourcePresentation.SlideSize.Size; maxWidth = Math.Max(maxWidth, sourceSize.Width); maxHeight = Math.Max(maxHeight, sourceSize.Height); var master = sourcePresentation.Masters.FirstOrDefault(); masters.AddClone(master); var layout = sourcePresentation.LayoutSlides; foreach (var slide in sourcePresentation.Slides) { if (masterSlide != null) { slides.AddClone(slide, masterSlide, true); } else { slides.AddClone(slide); } } } } if (styleMasterFile != null) { var masterSize = masterPresentation.SlideSize.Size; resultPresentation.SlideSize.SetSize(masterSize.Width, masterSize.Height, SlideSizeScaleType.DoNotScale); } else { resultPresentation.SlideSize.SetSize(maxWidth, maxHeight, SlideSizeScaleType.DoNotScale); } } finally { masterPresentation?.Dispose(); } resultPresentation.Save(resultFile, Aspose.Slides.Export.SaveFormat.Pptx); } if (format == SlidesConversionFormat.pptx) { return(resultFile); } else { return(Conversion( resultFile, outFolder, format )); } }
/// <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); } } }