We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

The Problem of Replacing/Updating Fields/Text Repeatedly at One Posistion

Hi,

I've a question when using Aspose.Slides. Supposing I have paragarphs in one textbox of PowerPoint template:

XXXXXXX [field 1] XXXXXXX [field 2] XXXXXXX.

field 1 and field 2 need to be replaced by real data values during generation.

By using Aspose.Slides, we can find text of "[field 1]" and "[field 2]", then use text replacement at Portion level. But I want to know whether we can replace new real values at the positions of [field 1] and [field 2] at second time, third time, and etc, which means at first time, "result output.ppt", for example, is generated based on one template, then, the data in "result output.ppt" needs to update, not generated from template again.

We know that in Word, bookmarks can be used as location and data filling using Aspose.Words. I just want to know whether PowerPoint has similar elements. "Alt Text" may be one option, but it is just for one textbox. If we put several fields to be filled into one textbox only, "Alt Text" won't work.

I just want to search for the solution of updating fields/text repeatedly in one position.

Many thanks.

Hi Sheng,


Thanks for inquiring Aspose.Slides.

I have observed your requirement that you want to maintain the position of the portion where you have replaced text in your last iteration. Am I right. I regret to share that that there is no directly available property to keep track of this in the form of bookmarks as you have shared. However, there is another solution that I may suggest to you in this regard that during the first iteration of text replacement at portion levels you can maintain list that will comprise of columns including Field Name, Slide_Index, Shape_Index, Paragraph_Index and Portion_Index. So, next time when you wish to change the same Field again, you have the right information related to that field and you can directly approach it using Following statement. I hope this will work on your end.

Portion port= pres.Slides[Slide_Index].Shapes[Shape_Index].TextFrame.Paragraphs[Paragraph_Index ].Portions[Portion_Index];

If you are using PPTX, you may use the similar mechanism there as well.

Many Thanks,

Hi Mudassir,

Thank you for your reply, but I’m afraid your solution cannot meet our requirement because our users are able to edit/modify the report after generation, then he/she could save it to database again. In sample of the first post, if the paragraph become
“XXXXXXX ABC(Field1) XXXXXXX 123(Field2) XXXXXXX” and user adds a carriage return before 123, it become
“XXXXXXX ABC(Field1) XXXXXXX
123(Field2) XXXXXXX”,
then both paragraph index and portion index may be changed, and the stored index value is not reliable, which is generally stored in database.

We just want an element which can provide some static name/tag, but it looks that PowerPoint hasn’t.

Hi Sheng,


I have further investigated the requirements shared by you and like to share that in case of PPTX, Aspose.Slides allows to provide BookmarkID as string associated with any portion. This is a string field and you can set any name to it. I have created a sample application for your convenience as under. In addParas() method, I am creating a sample presentation and adding bookmark id for added portions. Then I opened the generated presentation in PowerPoint and made some changes in it by removing extra paragraph. Then I used the checkBookmarks() method to access the modified presentation and observed the Bookmark Ids. It seems to work exactly as required. Please share, if I may help you further in this regard.

public static void addParas()
{
//Instantiate a PresentationEx class that represents a PPTX file

PresentationEx pres = new PresentationEx();

//Accessing first slide
SlideEx slide = pres.Slides[0];

//Add an AutoShape of Rectangle type
int idx = slide.Shapes.AddAutoShape(ShapeTypeEx.Rectangle, 50, 150, 300, 150);
AutoShapeEx ashp = (AutoShapeEx)slide.Shapes[idx];

//Access TextFrame of the AutoShape
TextFrameEx tf = ashp.TextFrame;

//Create Paragraphs and Portions with different text formats
ParagraphEx para0 = tf.Paragraphs[0];

PortionEx port01 = new PortionEx();
PortionEx port02 = new PortionEx();

para0.Portions.Add(port01);
para0.Portions.Add(port02);

ParagraphEx para1 = new ParagraphEx();
tf.Paragraphs.Add(para1);

PortionEx port10 = new PortionEx();
PortionEx port11 = new PortionEx();

para1.Portions.Add(port10);
para1.Portions.Add(port11);

for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
tf.Paragraphs[i].Portions[j].Text = “Portion”+i.ToString() + j.ToString();
string id = tf.Paragraphs[i].Portions[j].BookmarkId;
tf.Paragraphs[i].Portions[j].BookmarkId = “Field_para_” +i.ToString()+“portion”+ j.ToString();
id = tf.Paragraphs[i].Portions[j].BookmarkId;

if (j == 0)
{
tf.Paragraphs[i].Portions[j].FillFormat.FillType = FillTypeEx.Solid;
tf.Paragraphs[i].Portions[j].FillFormat.SolidFillColor.Color = Color.Red;
tf.Paragraphs[i].Portions[j].FontBold = NullableBool.True;
tf.Paragraphs[i].Portions[j].FontHeight = 15;
}

else if (j == 1)
{
tf.Paragraphs[i].Portions[j].FillFormat.FillType = FillTypeEx.Solid;
tf.Paragraphs[i].Portions[j].FillFormat.SolidFillColor.Color = Color.Blue;
tf.Paragraphs[i].Portions[j].FontItalic = NullableBool.True;
tf.Paragraphs[i].Portions[j].FontHeight = 18;

}
}
}

//Write PPTX to Disk
pres.Write(“d:\Aspose Data\multiParaPort.pptx”);

}
public static void checkBookmarks()
{
PresentationEx pres = new PresentationEx(“d:\Aspose Data\multiParaPort_modified.pptx”);

SlideEx slide = pres.Slides[0];

AutoShapeEx ashp = (AutoShapeEx)slide.Shapes[0];

TextFrameEx tf = ashp.TextFrame;

int paraCount = tf.Paragraphs.Count;

ParagraphEx para = null;
PortionEx portion = null;

for (int i = 0; i < paraCount; i++)
{
para = tf.Paragraphs[i];
for (int j = 0; j < para.Portions.Count; j++)
{
portion = para.Portions[j];

Console.WriteLine(" Portion Text is :"+portion.Text);
Console.WriteLine(" Paragraph :" + i.ToString() + " Portion :" + j.ToString() + " has Bookmard ID :" + portion.BookmarkId);
}

}

}

Many Thanks,

Hi Mudassir,

Thank you for your solution. I've tried your solution and run successfully. It looks that bookmarkId is wonderful. But I've a question that whether bookmarkId is introduced by Aspose only, or we could find the corresponding elements in PowerPoint directly(pptx). If we can set "bookmarkId" in pptx template directly, I think this is the exact approach what I want because in our system, report generation is "template-driven" which means template will be completed before report generation development. Currently a lot of template jobs are done by business anaylsts, not developers. So if "bookmarkId" can be found in pptx, I can instruct them to do so and we can continue to do our jobs, otherwise, I'm afraid it still cannot solve our issue.

Maybe portion index/order (within a shape, I can mark those field portions as special portions such as giving them shadow) is still an approach we can try, but if there are much better solutions, it will be appreicated.

Many thanks!

Hi Sheng,


I like to share that bookmarkID is in fact internal property of PowerPoint presentation and Aspose.Slides uses the same. However, there is no front end associated with this property in PowerPoint GUI. So, if the use of bookmark ID is not viable for you, I can offer another work around that may help you.

You can set some unique color and text properties (like boldness, italicize text, font height) for the Field text to be replaced when working in PowerPoint and then look for the same using Aspose.Slides. This is one last solution that may serve the purpose for you.

Many Thanks,