Background: I'm taking large Strings (500-5000 characters) and put them in a PPTX file. My slides only have enough space for around 1000 characters each slide. I would like to split the text after the slide is full and continue on the next slide. As far as I know there is no way (pretty sure) to check the slide if the text is already outside of the slide/textbox (I'm using "OpenXML SDK in combination with "Open-XML PowerTools").
Process so far: I managed to write a method that splits the string after exactly 1000 characters without triggering an error when the maxLength is smaller than 1000 characers (as it happend with Substring(startIndex, maxLength)). This is my Truncate Method:
public static string Truncate(string text, int startIndex, int maxLength)
{
if (string.IsNullOrEmpty(text)) return text;
if (text.Length > startIndex + maxLength)
{
return text.Substring(startIndex, maxLength) + "-";
}
return text.Substring(startIndex);
}
Problematic: The problem now is that some strings have a lot of newlines and others have few or none. This results into some strings that take a lot of height and being to large to fit into the slide.
Possible idea: I thought about estimating the string height counting the newlines and adding 30 characters to the number of characters for each newline. This gives a more exact approximation. (A line full of characters contains usually about 50 characters but sometimes the newline is in the middle of the line so I thought +30 characters would be a good guess). This is my method so far:
public int CountCharacters(string text)
{
var numLines = text.Length;
numLines += (text.Split('\n').Length) * 30;
return numLines;
}
Resulting question: How do I now combine these approaches to split the string after I reach a 1000 taking the newlines into account? Or is this the wrong approach after all?