When I use that specific template file I get an message that "PowerPoint can attempt to repair the presentation."
I am using the Spire.Presentation (7.1.0) library. But it is same for FreeSpire.Presentation (6.8.5). What is the problem and how can I fix it.
Thank you.
Template File:
Source Code:
- Code: Select all
using Spire.Presentation;
using System.Linq;
using Spire.Presentation.Charts;
using System;
namespace PptxTesterPaidVersion
{
class Program
{
static void Main(string[] args)
{
TestFileCorrupt();
}
private static void TestFileCorrupt()
{
//Load template presentation
Presentation templatePresentation = new Presentation();
templatePresentation.LoadFromFile("template02.pptx");
//Create New Presentation
Spire.Presentation.Presentation presentation = new Spire.Presentation.Presentation();
presentation.Slides.RemoveAt(0);
//Get slide from template presentation. index:1
ISlide cloneSlide = templatePresentation.Slides.ToArray().ElementAtOrDefault(0);
//---
//Get Chart
//IChart chart = templateSlide.Shapes.ToArray().FirstOrDefault(x => x.Name == "Content Placeholder 15") as IChart;
IChart chart = cloneSlide.Shapes.ToArray().FirstOrDefault(x => x.Name == "Content Placeholder 15") as IChart;
int seriesCount = 1;
int categoryCount = 9;
chart.ChartData.Clear(0, 0, categoryCount + 1, seriesCount + 1);
chart.ChartData[1, 0].Text = "Category AA";
chart.ChartData[2, 0].Text = "Category BB";
chart.ChartData[3, 0].Text = "Category CC";
chart.ChartData[4, 0].Text = "Category DD";
chart.ChartData[5, 0].Text = "Category EE";
chart.ChartData[6, 0].Text = "Category FF";
chart.ChartData[7, 0].Text = "Category GG";
chart.ChartData[8, 0].Text = "Category HH";
chart.ChartData[9, 0].Text = "Category KK";
chart.ChartData[0, 1].Text = "Serie 1-1";
chart.ChartData[1, 1].NumberValue = 0.69;
chart.ChartData[2, 1].NumberValue = 0.69;
chart.ChartData[3, 1].NumberValue = 0.79;
chart.ChartData[4, 1].NumberValue = 0.33;
chart.ChartData[5, 1].NumberValue = 0.33;
chart.ChartData[6, 1].NumberValue = 0.33;
chart.ChartData[7, 1].NumberValue = 0.33;
chart.ChartData[8, 1].NumberValue = 0.33;
chart.ChartData[9, 1].NumberValue = 0.33;
var startIndexAddSeries = chart.Series.Count + 1;
for (int i = startIndexAddSeries; i <= 4; i++)
{
chart.Series.Append(chart.ChartData[0, i]);
}
if (chart.Series.Count > seriesCount)
{
for (int i = chart.Series.Count - 1; i >= seriesCount; i--)
{
chart.Series.RemoveAt(i);
}
}
chart.Categories.CategoryLabels = chart.ChartData["A2", "A" + (categoryCount + 1)];
for (int i = 0; i < seriesCount; i++)
{
string letter = GetColumnName(i + 1);
chart.Series[i].Values = chart.ChartData[letter + "2", letter + (categoryCount + 1)];
}
presentation.Slides.Append(cloneSlide);
//Save and launch to view the PPTX document.
presentation.SaveToFile("TestFileCorrupt_01.pptx", Spire.Presentation.FileFormat.Pptx2010);
}
public static string GetColumnName(int index)
{
const string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var value = "";
if (index >= letters.Length)
value += letters[index / letters.Length - 1];
value += letters[index % letters.Length];
return value;
}
}
}