在 PPTX 文档中插入图像时遇到问题。
我想创建一个基于幻灯片蒙版的新幻灯片。我使用此代码在现有形状中设置图片
class Program
{
public static void Run()
{
string dataDir = ConfigurationManager.AppSettings["directoryToSave"];
string srcDir = String.Concat(ConfigurationManager.AppSettings["Source"], "Master.pptx");
string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
//string file = Path.Combine(appData, srcDir);
using (Presentation presentation = new Presentation(srcDir))
{
IMasterLayoutSlideCollection layoutSlides = presentation.Masters[0].LayoutSlides;
ILayoutSlide layoutSlide = null;
foreach (ILayoutSlide titleAndObjectLayoutSlide in layoutSlides)
{
if (titleAndObjectLayoutSlide.Name == "TITRE_CONTENU")
{
layoutSlide = titleAndObjectLayoutSlide;
break;
}
}
string filePath = String.Concat(ConfigurationManager.AppSettings["Source"], @"Logos\bp.png");
Image imgs = (Image)new Bitmap(filePath);
setShapePicture(presentation, layoutSlide, "picture", imgs);
presentation.Slides.InsertEmptySlide(0, layoutSlide);
presentation.Save(dataDir + "AddLayoutSlides_out.pptx", SaveFormat.Pptx);
}
}
public static void setShapePicture(Presentation doc, ILayoutSlide layoutSlide, string shapeName, Image image)
{
var logo_shape = layoutSlide.Shapes.SingleOrDefault(r => r.Name.Equals(shapeName));
IPPImage imgx = doc.Images.AddImage(image);
//Add Picture Frame with height and width equivalent of Picture
logo_shape.FillFormat.FillType = FillType.Picture;
// Set the picture fill mode
logo_shape.FillFormat.PictureFillFormat.PictureFillMode = PictureFillMode.Stretch;
// Set the picture
logo_shape.FillFormat.PictureFillFormat.Picture.Image = imgx;
}
static void Main(string[] args)
{
try
{
var path = ConfigurationManager.AppSettings["sourceAsposeLicensePath"];
License license = new License();
license.SetLicense(path);
Run();
}
catch (Exception ex)
{
Console.WriteLine("Error" + ex.Message);
}
finally
{
Console.WriteLine("Terminated");
Console.ReadKey();
}
}
}
我在生成的文件中得到的是一个带有背景图片和占位符的形状。您可以在此链接中找到所有资源MasterSlide+Logos+Output