|
| 1 | +using Syncfusion.Presentation; |
| 2 | +using Syncfusion.OfficeChart; |
| 3 | + |
| 4 | +class Program |
| 5 | +{ |
| 6 | + public static void Main(string[] args) |
| 7 | + { |
| 8 | + // Load or open a PowerPoint presentation |
| 9 | + using (FileStream inputStream = new(Path.GetFullPath(@"Data/Template.pptx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) |
| 10 | + { |
| 11 | + using (IPresentation presentation = Presentation.Open(inputStream)) |
| 12 | + { |
| 13 | + // Iterate through each slide in the presentation |
| 14 | + foreach (ISlide slide in presentation.Slides) |
| 15 | + { |
| 16 | + // Iterate through each shape in the slide |
| 17 | + foreach (IShape shape in slide.Shapes) |
| 18 | + { |
| 19 | + // Modify the shape properties (text, size, hyperlinks, etc.) |
| 20 | + ModifySlideElements(shape, presentation); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + // Save the modified presentation to an output file |
| 25 | + using (FileStream outputStream = new(Path.GetFullPath(@"../../../Output/Result.pptx"), FileMode.Create, FileAccess.ReadWrite)) |
| 26 | + { |
| 27 | + presentation.Save(outputStream); |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Modifies slide elements based on their type. |
| 35 | + /// </summary> |
| 36 | + private static void ModifySlideElements(IShape shape, IPresentation presentation) |
| 37 | + { |
| 38 | + switch (shape.SlideItemType) |
| 39 | + { |
| 40 | + case SlideItemType.AutoShape: |
| 41 | + { |
| 42 | + // Modify text if present in the shape |
| 43 | + if (!string.IsNullOrEmpty(shape.TextBody.Text)) |
| 44 | + { |
| 45 | + ModifyTextPart(shape.TextBody); |
| 46 | + } |
| 47 | + // If shape is a rectangle, add a hyperlink |
| 48 | + else if (shape.AutoShapeType == AutoShapeType.Rectangle) |
| 49 | + { |
| 50 | + shape.SetHyperlink("www.google.com"); |
| 51 | + } |
| 52 | + break; |
| 53 | + } |
| 54 | + |
| 55 | + case SlideItemType.Placeholder: |
| 56 | + { |
| 57 | + // Modify text if present in the placeholder |
| 58 | + if (!string.IsNullOrEmpty(shape.TextBody.Text)) |
| 59 | + { |
| 60 | + ModifyTextPart(shape.TextBody); |
| 61 | + } |
| 62 | + break; |
| 63 | + } |
| 64 | + |
| 65 | + case SlideItemType.Picture: |
| 66 | + { |
| 67 | + // Resize the picture |
| 68 | + IPicture picture = shape as IPicture; |
| 69 | + picture.Height = 160; |
| 70 | + picture.Width = 130; |
| 71 | + break; |
| 72 | + } |
| 73 | + |
| 74 | + case SlideItemType.Table: |
| 75 | + { |
| 76 | + // Get the table shape |
| 77 | + ITable table = shape as ITable; |
| 78 | + |
| 79 | + // Iterate through rows and modify text in each cell |
| 80 | + foreach (IRow row in table.Rows) |
| 81 | + { |
| 82 | + foreach (ICell cell in row.Cells) |
| 83 | + { |
| 84 | + ModifyTextPart(cell.TextBody); |
| 85 | + } |
| 86 | + } |
| 87 | + break; |
| 88 | + } |
| 89 | + |
| 90 | + case SlideItemType.GroupShape: |
| 91 | + { |
| 92 | + // Get the group shape and iterate through child shapes |
| 93 | + IGroupShape groupShape = shape as IGroupShape; |
| 94 | + foreach (IShape childShape in groupShape.Shapes) |
| 95 | + { |
| 96 | + ModifySlideElements(childShape, presentation); |
| 97 | + } |
| 98 | + break; |
| 99 | + } |
| 100 | + |
| 101 | + case SlideItemType.Chart: |
| 102 | + { |
| 103 | + // Modify chart properties |
| 104 | + IPresentationChart chart = shape as IPresentationChart; |
| 105 | + chart.ChartTitle = "Purchase Details"; |
| 106 | + chart.ChartTitleArea.Bold = true; |
| 107 | + chart.ChartTitleArea.Color = OfficeKnownColors.Red; |
| 108 | + chart.ChartTitleArea.Size = 20; |
| 109 | + break; |
| 110 | + } |
| 111 | + |
| 112 | + case SlideItemType.SmartArt: |
| 113 | + { |
| 114 | + // Modify SmartArt content |
| 115 | + ISmartArt smartArt = shape as ISmartArt; |
| 116 | + ISmartArtNode smartArtNode = smartArt.Nodes[0]; |
| 117 | + smartArtNode.TextBody.Text = "Requirement"; |
| 118 | + break; |
| 119 | + } |
| 120 | + |
| 121 | + case SlideItemType.OleObject: |
| 122 | + { |
| 123 | + // Modify OLE object size |
| 124 | + IOleObject oleObject = shape as IOleObject; |
| 125 | + oleObject.Width = 300; |
| 126 | + break; |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + /// <summary> |
| 132 | + /// Modifies the text content within a text body. |
| 133 | + /// </summary> |
| 134 | + private static void ModifyTextPart(ITextBody textBody) |
| 135 | + { |
| 136 | + // Iterate through paragraphs in the text body |
| 137 | + foreach (IParagraph paragraph in textBody.Paragraphs) |
| 138 | + { |
| 139 | + // Iterate through text parts and modify the text |
| 140 | + foreach (ITextPart textPart in paragraph.TextParts) |
| 141 | + { |
| 142 | + textPart.Text = "Adventure Works"; |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments