我如何使用OpenXML C#在powerpoint中添加图像?
任何人都有一个如何在特定位置和大小插入图像的示例? 以及如何编辑文本框以定位和调整大小?
答案 0 :(得分:1)
您可以使用OpenXML SDK Productivity Tool将powerpoint文件反编译为c#。使用Image和TextBox创建文件,反编译并查看如何在c#中完成。
答案 1 :(得分:0)
答案 2 :(得分:0)
使用以下代码:
public static void AddImage(string file, string image)
{
using (var presentation = PresentationDocument.Open(file, true))
{
var slidePart = presentation
.PresentationPart
.SlideParts
.First();
var part = slidePart
.AddImagePart(ImagePartType.Png);
using (var stream = File.OpenRead(image))
{
part.FeedData(stream);
}
var tree = slidePart
.Slide
.Descendants<DocumentFormat.OpenXml.Presentation.ShapeTree>()
.First();
var picture = new DocumentFormat.OpenXml.Presentation.Picture();
picture.NonVisualPictureProperties = new DocumentFormat.OpenXml.Presentation.NonVisualPictureProperties();
picture.NonVisualPictureProperties.Append(new DocumentFormat.OpenXml.Presentation.NonVisualDrawingProperties
{
Name = "My Shape",
Id = (UInt32)tree.ChildElements.Count - 1
});
var nonVisualPictureDrawingProperties = new DocumentFormat.OpenXml.Presentation.NonVisualPictureDrawingProperties();
nonVisualPictureDrawingProperties.Append(new DocumentFormat.OpenXml.Drawing.PictureLocks()
{
NoChangeAspect = true
});
picture.NonVisualPictureProperties.Append(nonVisualPictureDrawingProperties);
picture.NonVisualPictureProperties.Append(new DocumentFormat.OpenXml.Presentation.ApplicationNonVisualDrawingProperties());
var blipFill = new DocumentFormat.OpenXml.Presentation.BlipFill();
var blip1 = new DocumentFormat.OpenXml.Drawing.Blip()
{
Embed = slidePart.GetIdOfPart(part)
};
var blipExtensionList1 = new DocumentFormat.OpenXml.Drawing.BlipExtensionList();
var blipExtension1 = new DocumentFormat.OpenXml.Drawing.BlipExtension()
{
Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}"
};
var useLocalDpi1 = new DocumentFormat.OpenXml.Office2010.Drawing.UseLocalDpi()
{
Val = false
};
useLocalDpi1.AddNamespaceDeclaration("a14", "http://schemas.microsoft.com/office/drawing/2010/main");
blipExtension1.Append(useLocalDpi1);
blipExtensionList1.Append(blipExtension1);
blip1.Append(blipExtensionList1);
var stretch = new DocumentFormat.OpenXml.Drawing.Stretch();
stretch.Append(new DocumentFormat.OpenXml.Drawing.FillRectangle());
blipFill.Append(blip1);
blipFill.Append(stretch);
picture.Append(blipFill);
picture.ShapeProperties = new DocumentFormat.OpenXml.Presentation.ShapeProperties();
picture.ShapeProperties.Transform2D = new DocumentFormat.OpenXml.Drawing.Transform2D();
picture.ShapeProperties.Transform2D.Append(new DocumentFormat.OpenXml.Drawing.Offset
{
X = 0,
Y = 0,
});
picture.ShapeProperties.Transform2D.Append(new DocumentFormat.OpenXml.Drawing.Extents
{
Cx = 1000000,
Cy = 1000000,
});
picture.ShapeProperties.Append(new DocumentFormat.OpenXml.Drawing.PresetGeometry
{
Preset = DocumentFormat.OpenXml.Drawing.ShapeTypeValues.Rectangle
});
tree.Append(picture);
}
}