I need some assistance in adding a background shape to an svg file converted to group of shapes. I’m using the following code to add svg file as background shape, and another svg file converted to a group of shape and fill the textbox with the svg(svg converted to group of shapes) file name. I need to group the three into one group and repeat this for all background shapes, svg files and textboxes. I’ve attached the current output I have and the expected output.
Here’s my current code. Attached is the pptx template I’m using, the current and expected output files.
FolderBrowserDialog fold = new FolderBrowserDialog();
if (fold.ShowDialog() == DialogResult.OK)
{
string svgFilePath = fold.SelectedPath;
DirectoryInfo dInfo = new DirectoryInfo(svgFilePath);
FileInfo[] sfiles = dInfo.GetFiles("*.svg");
if (sfiles.Any())
{
int svgFilesPerPage = 20;
string[] svgFiles = Directory.GetFiles(svgFilePath, "*.svg");
string bgContent = File.ReadAllText(bgFile);
ISvgImage bgSVG = new SvgImage(bgContent);
using (Presentation presentation = new Presentation(Application.StartupPath + @"\Templates\DefaultTemplate.pptx"))
{
int slidesNeeded = (int)Math.Ceiling((double)svgFiles.Length / svgFilesPerPage);
for (int i = 0; i < slidesNeeded && i < presentation.Slides.Count; i++)
{
float[] svgXPosition = { (float)66.79292, (float)254.5669, (float)442.3408, (float)630.1146, (float)817.8885, (float)66.79292, (float)254.5669, (float)442.3408, (float)630.1146, (float)817.8885, (float)66.79292, (float)254.5669, (float)442.3408, (float)630.1146, (float)817.8885, (float)66.79292, (float)254.5669, (float)442.08, (float)630.1146, (float)817.8885 };
float[] svgYPosition = { (float)110.5453, (float)110.751, (float)110.1687, (float)110.751, (float)110.1687, (float)219.1795, (float)219.3853, (float)218.8678, (float)219.3853, (float)218.803, (float)329.184, (float)329.3898, (float)328.8723, (float)329.3898, (float)328.8075, (float)441.1568, (float)441.3625, (float)440.64, (float)441.3625, (float)440.7802 };
float[] svgXWidth = { (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56 };
float[] svgYHeight = { (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56 };
//Fll Title and Subtitle
IShape title = SearchTextBox.FindShape(presentation.Slides[i], "Title1");
if (title != null)
{
title.FillFormat.FillType = FillType.NoFill;
ITextFrame titleFrame = ((IAutoShape)title).TextFrame;
titleFrame.Text = txtInfo.ToTitleCase(slideTitle);
IPortion titlePort = titleFrame.Paragraphs[0].Portions[0];
titlePort.PortionFormat.LatinFont = new FontData(titleString[0]);
titlePort.PortionFormat.FontHeight = float.Parse(titleString[1]);
}
IShape subTitle = SearchTextBox.FindShape(presentation.Slides[i], "Subtitle2");
if (subTitle != null)
{
subTitle.FillFormat.FillType = FillType.NoFill;
ITextFrame subtitleFrame = ((IAutoShape)subTitle).TextFrame;
subtitleFrame.Text = txtInfo.ToTitleCase(slideSubtitle);
IPortion subtitlePort = subtitleFrame.Paragraphs[0].Portions[0];
subtitlePort.PortionFormat.LatinFont = new FontData(subtitleString[0]);
subtitlePort.PortionFormat.FontHeight = float.Parse(subtitleString[1]);
}
for (int j = 0; j < svgFilesPerPage && (i * svgFilesPerPage + j) < svgFiles.Length; j++)
{
//Load SVG file
string svgContent = File.ReadAllText(svgFiles[i * svgFilesPerPage + j]);
ISvgImage svgImage = new Aspose.Slides.SvgImage(svgContent);
//Size and Postio for Backgroundshape
float pixelIncrease = 24f;
float posPixel = 12f;
// Use known positions for each SVG image
float svgX = svgXPosition[j];
float svgY = svgYPosition[j];
float svgWidth = svgXWidth[j];
float svgHeight = svgYHeight[j];
// Add background shape for each SVG image
IPPImage ppImageBg = presentation.Images.AddImage(bgSVG);
presentation.Slides[i].Shapes.AddPictureFrame(ShapeType.Rectangle, svgX - posPixel, svgY - posPixel, svgWidth + pixelIncrease, svgHeight + pixelIncrease, ppImageBg);
//Add SVG image as a shape to the slide
var groupshape = presentation.Slides[i].Shapes.AddGroupShape(svgImage, svgX, svgY, svgWidth, svgHeight);
foreach (IShape svgShape in groupshape.Shapes)
{
Color fillColor = shapeColors[0];
svgShape.FillFormat.FillType = FillType.Solid;
svgShape.FillFormat.SolidFillColor.Color = fillColor;
}
for (int k = 1; k <= svgFilesPerPage; k++)
{
string textBoxName = "TextBox" + k;
// Find the textbox by alternative text
IShape textBox = SearchTextBox.FindShape(presentation.Slides[i], textBoxName);
// Check if the textbox exists and update its text
if (textBox != null && (i * svgFilesPerPage + k) <= svgFiles.Length)
{
((IAutoShape)textBox).FillFormat.FillType = FillType.NoFill;
ITextFrame textFrame = ((IAutoShape)textBox).TextFrame;
var iconName = Regex.Replace(Path.GetFileNameWithoutExtension(svgFiles[i * svgFilesPerPage + k - 1]), @"[\d-]", string.Empty);
textFrame.Text = txtInfo.ToTitleCase(iconName);// Path.GetFileNameWithoutExtension(txtInfo.ToTitleCase(svgFiles[i * svgFilesPerPage+k-1]));
IPortion portion = textFrame.Paragraphs[0].Portions[0];
portion.PortionFormat.LatinFont = new FontData(textString[0]);
portion.PortionFormat.FontHeight = float.Parse(textString[1]);
}
else if (textBox != null)
{
presentation.Slides[i].Shapes.Remove(textBox);
k--;
}
}
}
}
//Remove extra slides
for (int i = slidesNeeded; i < presentation.Slides.Count; i++)
{
presentation.Slides.RemoveAt(i);
//adjust index after removal
i--;
}
// Save the modified PowerPoint presentation
Clear(presentation);
}
}
else { AlertNotification.ShowAlertMessage("No svg files found.", AlertNotification.AlertType.ERROR); }
}
Files.zip (779.6 KB)