Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
Presentation .NET
>
Tutorial
> Insert picture
The following example shows you how to insert a picture in a new slide.
C# example
using System; using Independentsoft.Office; using Independentsoft.Office.Presentation; using Independentsoft.Office.Presentation.Drawing; namespace Sample { class Program { static void Main(string[] args) { Presentation presentation = new Presentation(); GroupShape shapeTree = new GroupShape(); shapeTree.ID = "1"; shapeTree.Name = "ShapeTree"; shapeTree.ShapeProperties.TransformGroup2D = new Independentsoft.Office.Drawing.TransformGroup2D(); Independentsoft.Office.Drawing.TextRun run1 = new Independentsoft.Office.Drawing.TextRun("Hello World"); Independentsoft.Office.Drawing.TextParagraph paragraph1 = new Independentsoft.Office.Drawing.TextParagraph(); paragraph1.Alignment = Independentsoft.Office.Drawing.TextAlignmentType.Center; paragraph1.Content.Add(run1); ShapeTextBody textBody = new ShapeTextBody(); textBody.Paragraphs.Add(paragraph1); Shape shape1 = new Shape(); shape1.ID = "2"; shape1.Name = "Title 1"; shape1.Locking = new Independentsoft.Office.Drawing.ShapeLocking(); shape1.Locking.DisallowGrouping = true; shape1.Placeholder = new Placeholder(PlaceholderType.CenteredTitle); shape1.TextBody = textBody; Unit offsetX = new Unit(2, UnitType.Inch); Unit offsetY = new Unit(1, UnitType.Inch); Unit width = new Unit(6, UnitType.Inch); Unit height = new Unit(2, UnitType.Inch); shape1.ShapeProperties.Transform2D = new Independentsoft.Office.Drawing.Transform2D(); shape1.ShapeProperties.Transform2D.Offset = new Independentsoft.Office.Drawing.Offset(offsetX, offsetY); shape1.ShapeProperties.Transform2D.Extents = new Independentsoft.Office.Drawing.Extents(width, height); shape1.ShapeProperties.PresetGeometry = new Independentsoft.Office.Drawing.PresetGeometry(Independentsoft.Office.Drawing.ShapeType.Rectangle); Picture picture = new Picture("c:\\test\\image.jpg"); picture.ID = "3"; picture.Name = "Image1"; picture.Stretch = new Independentsoft.Office.Drawing.Stretch(); picture.Stretch.FillRectangle = new Independentsoft.Office.Drawing.FillRectangle(); picture.Locking = new Independentsoft.Office.Drawing.PictureLocking(); picture.Locking.DisallowAspectRatioChange = true; picture.ShapeProperties.PresetGeometry = new Independentsoft.Office.Drawing.PresetGeometry(Independentsoft.Office.Drawing.ShapeType.Rectangle); Unit pictureWidth = new Unit(640, UnitType.Pixel); Unit pictureHeight = new Unit(480, UnitType.Pixel); Unit pictureOffsetX = new Unit(2, UnitType.Inch); Unit pictureOffsetY = new Unit(2, UnitType.Inch); Independentsoft.Office.Drawing.Offset offset = new Independentsoft.Office.Drawing.Offset(pictureOffsetX, pictureOffsetY); Independentsoft.Office.Drawing.Extents extents = new Independentsoft.Office.Drawing.Extents(pictureWidth, pictureHeight); picture.ShapeProperties.Transform2D = new Independentsoft.Office.Drawing.Transform2D(); picture.ShapeProperties.Transform2D.Offset = offset; picture.ShapeProperties.Transform2D.Extents = extents; shapeTree.Elements.Add(shape1); shapeTree.Elements.Add(picture); CommonSlideData commonSlideData = new CommonSlideData(); commonSlideData.ShapeTree = shapeTree; SlideLayout layout1 = new SlideLayout(); layout1.CommonSlideData = GetLayoutCommonSlideData(); Slide slide1 = new Slide(); slide1.CommonSlideData = commonSlideData; slide1.Layout = layout1; SlideMaster master1 = new SlideMaster(); master1.CommonSlideData = GetLayoutCommonSlideData(); master1.Layouts.Add(layout1); master1.TextStyles = new SlideMasterTextStyles(); master1.TextStyles.TitleStyle = new SlideMasterTitleTextStyle(); master1.TextStyles.TitleStyle.ListLevel1TextStyle.DefaultTextRunProperties.FontSize = 44; presentation.Slides.Add(slide1); presentation.SlideMasters.Add(master1); presentation.SlideSize = new SlideSize(9144000, 6858000, SlideSizeType.Screen4x3); presentation.NotesSlideSize = new NotesSlideSize(6858000, 9144000); presentation.Save("c:\\test\\output.pptx", true); } static CommonSlideData GetLayoutCommonSlideData() { GroupShape shapeTree = new GroupShape(); shapeTree.ID = "1"; shapeTree.Name = "layout"; shapeTree.ShapeProperties.TransformGroup2D = new Independentsoft.Office.Drawing.TransformGroup2D(); CommonSlideData commonSlideData = new CommonSlideData(); commonSlideData.ShapeTree = shapeTree; return commonSlideData; } } }
VB example
Imports System Imports Independentsoft.Office Imports Independentsoft.Office.Presentation Imports Independentsoft.Office.Presentation.Drawing Module Module1 Sub Main(ByVal args As String()) Dim presentation As New Presentation() Dim shapeTree As New GroupShape() shapeTree.ID = "1" shapeTree.Name = "ShapeTree" shapeTree.ShapeProperties.TransformGroup2D = New Independentsoft.Office.Drawing.TransformGroup2D() Dim run1 As New Independentsoft.Office.Drawing.TextRun("Hello World") Dim paragraph1 As New Independentsoft.Office.Drawing.TextParagraph() paragraph1.Alignment = Independentsoft.Office.Drawing.TextAlignmentType.Center paragraph1.Content.Add(run1) Dim textBody As New ShapeTextBody() textBody.Paragraphs.Add(paragraph1) Dim shape1 As New Shape() shape1.ID = "2" shape1.Name = "Title 1" shape1.Locking = New Independentsoft.Office.Drawing.ShapeLocking() shape1.Locking.DisallowGrouping = True shape1.Placeholder = New Placeholder(PlaceholderType.CenteredTitle) shape1.TextBody = textBody Dim offsetX As New Unit(2, UnitType.Inch) Dim offsetY As New Unit(1, UnitType.Inch) Dim width As New Unit(6, UnitType.Inch) Dim height As New Unit(2, UnitType.Inch) shape1.ShapeProperties.Transform2D = New Independentsoft.Office.Drawing.Transform2D() shape1.ShapeProperties.Transform2D.Offset = New Independentsoft.Office.Drawing.Offset(offsetX, offsetY) shape1.ShapeProperties.Transform2D.Extents = New Independentsoft.Office.Drawing.Extents(width, height) shape1.ShapeProperties.PresetGeometry = New Independentsoft.Office.Drawing.PresetGeometry(Independentsoft.Office.Drawing.ShapeType.Rectangle) Dim picture As New Picture("c:\test\image.jpg") picture.ID = "3" picture.Name = "Image1" picture.Stretch = New Independentsoft.Office.Drawing.Stretch() picture.Stretch.FillRectangle = New Independentsoft.Office.Drawing.FillRectangle() picture.Locking = New Independentsoft.Office.Drawing.PictureLocking() picture.Locking.DisallowAspectRatioChange = True picture.ShapeProperties.PresetGeometry = New Independentsoft.Office.Drawing.PresetGeometry(Independentsoft.Office.Drawing.ShapeType.Rectangle) Dim pictureWidth As New Unit(640, UnitType.Pixel) Dim pictureHeight As New Unit(480, UnitType.Pixel) Dim pictureOffsetX As New Unit(2, UnitType.Inch) Dim pictureOffsetY As New Unit(2, UnitType.Inch) Dim offset As New Independentsoft.Office.Drawing.Offset(pictureOffsetX, pictureOffsetY) Dim extents As New Independentsoft.Office.Drawing.Extents(pictureWidth, pictureHeight) picture.ShapeProperties.Transform2D = New Independentsoft.Office.Drawing.Transform2D() picture.ShapeProperties.Transform2D.Offset = offset picture.ShapeProperties.Transform2D.Extents = extents shapeTree.Elements.Add(shape1) shapeTree.Elements.Add(picture) Dim commonSlideData As New CommonSlideData() commonSlideData.ShapeTree = shapeTree Dim layout1 As New SlideLayout() layout1.CommonSlideData = GetLayoutCommonSlideData() Dim slide1 As New Slide() slide1.CommonSlideData = commonSlideData slide1.Layout = layout1 Dim master1 As New SlideMaster() master1.CommonSlideData = GetLayoutCommonSlideData() master1.Layouts.Add(layout1) master1.TextStyles = New SlideMasterTextStyles() master1.TextStyles.TitleStyle = New SlideMasterTitleTextStyle() master1.TextStyles.TitleStyle.ListLevel1TextStyle.DefaultTextRunProperties.FontSize = 44 presentation.Slides.Add(slide1) presentation.SlideMasters.Add(master1) presentation.SlideSize = New SlideSize(9144000, 6858000, SlideSizeType.Screen4x3) presentation.NotesSlideSize = New NotesSlideSize(6858000, 9144000) presentation.Save("c:\test\output.pptx", True) End Sub Function GetLayoutCommonSlideData() As CommonSlideData Dim shapeTree As New GroupShape() shapeTree.ID = "1" shapeTree.Name = "layout" shapeTree.ShapeProperties.TransformGroup2D = New Independentsoft.Office.Drawing.TransformGroup2D() Dim commonSlideData As New CommonSlideData() commonSlideData.ShapeTree = shapeTree Return commonSlideData End Function End Module
Need help? Ask our developers:
Name*
Email*
Message*