[英]Insert Picture to an existing shape
I encounter a problem when inserting an image in PPTX document.我在 PPTX 文档中插入图像时遇到问题。
I want to create a new slide based on slide mask.我想创建一个基于幻灯片蒙版的新幻灯片。 I used this code to set picture in existing a shape我使用此代码在现有形状中设置图片
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();
}
}
}
What I get in the generated file is a shape with background picture and a placeholder.我在生成的文件中得到的是一个带有背景图片和占位符的形状。 You can find all resources in this link MasterSlide+Logos+Output您可以在此链接MasterSlide+Logos+Output中找到所有资源
I have observed the requirements shared by you and like to share that placeholder is not an actual shape but gives a skeleton for shape.我已经观察到您分享的要求,并且喜欢分享占位符不是实际形状,而是给出形状的骨架。 I have created a sample code for your convenience that may help you in achieving your requirements.为了您的方便,我创建了一个示例代码,可以帮助您实现您的要求。
public static void TestPlaceholderImage()
{
String path = "C:\\Aspose Data\\";
Presentation pres = new Presentation(path + "TestPicture.pptx");
foreach (ISlide slide in pres.Slides)
{
foreach (IShape shape in slide.Shapes)
{
if (shape.Placeholder != null)
{
if (shape.Placeholder.Type == PlaceholderType.Picture)
{
// Instantiate the ImageEx class
System.Drawing.Image img = (System.Drawing.Image)new Bitmap(@"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg");
IPPImage imgx = pres.Images.AddImage(img);
shape.FillFormat.FillType = FillType.Picture;
shape.FillFormat.PictureFillFormat.Picture.Image = imgx;
if (shape is AutoShape)
{
ITextFrame text = ((IAutoShape)shape).TextFrame;
text.Text = " ";
text.Paragraphs[0].ParagraphFormat.Bullet.Type = BulletType.None;
}
}
}
}
}
pres.Save(path + "Addedpic.pptx", Aspose.Slides.Export.SaveFormat.Pptx);
}
I am working as Support developer/ Evangelist at Aspose.我在 Aspose 担任支持开发人员/传播者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.