Free Support Forum - aspose.com

Showing shapes/images dynamically

Hi,

I'm back with new query again...

Please find the attached template (Project_Results_template.doc). Currently it has a blank table with 4columns. In my database I have records (multiple rows) which supposed to be fetched accordingly...

In both table and template, the first column is the Project Description, second is Rating, third is summary, fourth is Remarks.

Ratings - It has 3 different ratings 1) Satisfactory 2) Unsatis 3) Out of Scope

So the data should be fetched to these appropriate columns. But the second column should have an image kind of stuff. Which means when the Rating is Satisfactory in the first row it should a small Ellipse symbol with Green color shading. If it is unsatisfactory then Black color shading and when Out of Scope it should be Red color.

Lastly, after retrieving these project description rows, I have a field Overall Rating in the template and which will have it in different table. This should be fetched along with the project descriptions. This should show only the Rating description (Satisfactory/Unsatis/Out of Scope) ratings. No images required for this.

Please find the attached Project_Results_output.doc). which depicts my expected output and also with explanation...

Hope this time i had given proper explanation... pls let knw if it is still confusing :-(

Thanx for your wonderful help!

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. I think, in this case, you can use Aspose.Words Mail Merge feature. Please follow the link to learn more:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/mail-merge-and-reporting.html

Please let me know in case of any issues, I will be glad to help you.

Best regards,

Hi Andrey,

Thanks for ur prompt response... I dont think Mail Merge option would be helpful to my requirment. If you notice in my expected output file, I will have display shape as per the records fetched... please once go through with my template and output docs once again and help me in giving some sample code... thanx once again!

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. Please try using the following code:

Document doc = new Document(@"Test050\in.doc");

// Perform several mail merge operations populating only part of the document each time.

// Use DataTable as a data source.

DataTable resultsTable = GetInfo();

// Add MergeImageField event handler. It is needed to insert images upon mail merge.

doc.MailMerge.MergeImageField += MailMerge_MergeImageField;

doc.MailMerge.ExecuteWithRegions(resultsTable);

doc.Save(@"Test050\out.doc");

void MailMerge_MergeImageField(object sender, MergeImageFieldEventArgs e)

{

string value = e.FieldValue.ToString();

// Insert appropriate image.

switch (value)

{

case "1":

e.ImageFileName = @"Test050\1.png";

break;

case "2":

e.ImageFileName = @"Test050\2.png";

break;

case "3":

e.ImageFileName = @"Test050\3.png";

break;

default:

e.ImageFileName = @"Test050\1.png";

break;

}

}

private DataTable GetInfo()

{

// Create data table

DataTable table = new DataTable("Results");

table.Columns.Add("ProjectName");

table.Columns.Add("Rating");

table.Columns.Add("Summary");

table.Columns.Add("Remarks");

// Add some dummy data.

table.Rows.Add(new object[] { "Project 1", "1", "Project1 Summary", "Reviewed1" });

table.Rows.Add(new object[] { "Project 2", "2", "Project2 Summary", "Reviewed2" });

table.Rows.Add(new object[] { "Project 3", "3", "Project3 Summary", "Reviewed3" });

table.Rows.Add(new object[] { "Project 4", "1", "Project4 Summary", "Reviewed4" });

table.Rows.Add(new object[] { "Project 5", "2", "Project5 Summary", "Reviewed5" });

table.Rows.Add(new object[] { "Project 6", "3", "Project6 Summary", "Reviewed6" });

return table;

}

I also attached sample documents.

Best regards,

hey thanx a lot for the sample code! one quick question… i have gone through the attachment… pls correct me if I’m wrong, I understand that i’ll have specify the locations using MailMerge? (as per the in.doc, there are some <> components)…could you pls tel me how can i name tht way? coz when i tried with a doc, it ws asking some specific formats (letter/email etc)… later it ws looking for some datasoure which is confusing me… can you pls give me step by step instructions on how to create such template using <> as per your in.doc format? thanx once again!

Thanks for your request. Please see the following link to learn how to prepare a document for mail merge

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/prepare-a-document.html

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Best regards,

Hi, thanx for quick guide on step by step instructions to create MergeField. I have another question, is there any possibility to use Shape (Ellipse) instead using an image (.png)? I would say using the Shape is much more feasible to me rather than image… hope you understand… can you please give me the line of code to the above code on how to include the shape(s) in it? Thanx!

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. You can do this in MergeImageFiled event handler. For example see the following code:

void MailMerge_MergeImageField(object sender, MergeImageFieldEventArgs e)

{

string value = e.FieldValue.ToString();

DocumentBuilder builder = new DocumentBuilder(e.Document);

builder.MoveToField(e.Field, true);

// Create shape

Shape shape = new Shape(e.Document, ShapeType.Ellipse);

shape.Width = 20;

shape.Height = 20;

shape.WrapType = WrapType.Inline;

// Set appropriate shape filling.

switch (value)

{

case "1":

shape.FillColor = Color.Black;

break;

case "2":

shape.FillColor = Color.Green;

break;

case "3":

shape.FillColor = Color.Red;

break;

default:

shape.FillColor = Color.Black;

break;

}

// Insert shape into the document

builder.InsertNode(shape);

// Remove field, since we already inserted necessary data.

e.Field.Remove();

}

Hope this helps.

Best regards,

hey thanx for the code man… but the code is failing at event handler… doc.MailMerge.MergeImageField += MailMerge_MergeImageField;<o:p></o:p> at this line it is not going to the MailMerge_MergeImageField event… wt could be the reason :-(? how to make it work?

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. The code works fine on my side. Could you please create simple application, which will demonstrate the problem on my side? I will check your code and template and provide you more information.

Best regards,

hi… i dont know wtz the problem then :-(… actually, i have placed the code in Entity Class (Aspose.cs) file as for other displaying purpose I’m using here… At this line the code is not being executed… pls help me

descDoc.MailMerge.MergeImageField += MailMerge_MergeImageField;

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. Have you got any exception? Could you please provide me details of the exception? Please attach stack trace of the exception.

Also, I attached the sample application which I use for testing on my side.

Best regards,

hi there is no exception as such... i have debugged the application and after this line of code

descDoc.MailMerge.MergeImageField += new Aspose.Words.Reporting.MergeImageFieldEventHandler(MailMerge_MergeImageField);

it is going to next line... i have put a break point at MailMerge_MergeImageField event but it is not getting fired :-(... after some search in your website

descDoc.MailMerge.ExecuteWithRegions(DataTable1);

i have added this line after the event handler added... but still no use :-(.... I really dont know whether ExecuteWithRegions is useful or not as i found which searching i tried using it... hope you understand the situation... the event is not fired and so cant display neither the images (Rating) nor the data (Summary/Remarks) :-(... plz help me

Also, is there something to do with this line

Document descDoc = new Document(this.templateDocumentPath); so d

So descDoc will have something like (D:\Projects\in.doc) and at the end the below lines are there

GeneratePDF(descDoc, outputPath + "\\" + pdffile);

descDoc.Save(outputPath + "\\" + guid.ToString() + ".doc"); Hope this helps to figure out wt the problem exactly... thanx!

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Have you tried using the sample application which I have attached in my latest post? MailMerge and MergeImageField are events and they are fired when mergefield is merged with the actually data. These events are fired during executing mail merge.

Best regards,

Hi yes i have tried ur sample application and it is working fine… but in my application i dont know wt could be the reason but it is not firing the event… it is just going to the next line when i debug :-(… how can i overcome with this problem? plz guide me…

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for additional information. Unfortunately, I cannot reproduce the problem. Could you please create sample application which will allow me to replicate the problem on my side? I will check the issue and provide you more information.

Best regards,

Hi, yes even i couldnt reproduce the issue from the sample code you had given me... however i have placed the same lines of code and somehow the even is not firing... Even i made the event to Public and still no luck... is there anything problem in this statement:

Document descDoc = new Document(this.templateDocumentPath);

this.templateDocumentPath has "D:\Projects\Application\Templates\in.doc"... is this the problem? hw can we resolve this problem?

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

I understand that you cannot reproduce the problem using my sample application. But I asked you to create sample application using your code and your template and attach it here. At least, please attach your template.

Best regards,

hi thanx for the reply... herez the lines of code and the template attached herewith...

public string ConstructPreview(ListDictionary docStreams, string outputPath, DataTable HeadersInfo, DataTable KeyRiskAreas)

{

outputPath += @"\output";

Document descDoc = new Document(this.templateDocumentPath);

DocumentBuilder builder = new DocumentBuilder(descDoc);

System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();

Guid guid = Guid.NewGuid();

string pdffile = guid.ToString() + ".pdf";

if (HeadersInfo != null)

{

builder.MoveToBookmark("Header");

builder.CellFormat.Width = 250;

int insertedCols = 0;

foreach (DataColumn column in HeadersInfo.Columns)

{

if (insertedCols != 0 && insertedCols % 2 == 0)

{

insertedCols = 0;

builder.EndRow();

}

string colData = HeadersInfo.Rows[0][column].ToString();

if (!string.IsNullOrEmpty(colData))

{

builder.InsertCell();

builder.PushFont();

builder.Font.Bold = true;

builder.Write(column.ColumnName + ": ");

builder.PopFont();

builder.Write(colData);

insertedCols++;

}

}

if (insertedCols != 0)

builder.EndRow();

builder.EndTable();

}

descDoc.MailMerge.MergeImageField += new Aspose.Words.Reporting.MergeImageFieldEventHandler(MailMerge_MergeImageField);

descDoc.MailMerge.ExecuteWithRegions(KeyRiskAreas);

int noOfPages = 0;

ICollection col = docStreams.Keys;

IEnumerator en = col.GetEnumerator();

while (en.MoveNext())

{

string bookmark = (string)en.Current;

Node bmNode = descDoc.Range.Bookmarks[bookmark].BookmarkStart;

if (((Paragraph)bmNode.ParentNode).GetText().Equals(string.Empty))

{

Document srcDoc = new Document(new MemoryStream(encoding.GetBytes(((string)docStreams[en.Current]).Replace("\a", " "))), null, LoadFormat.Html, null);

RemoveEvaluationText(srcDoc);

InsertDocument(bmNode, srcDoc);

}

else

{

builder.MoveToBookmark(bookmark, true, true);

builder.InsertHtml(((string)docStreams[en.Current]).Replace("\a", " "));

Aspose.Words.Paragraph para = descDoc.Range.Bookmarks[bookmark].BookmarkStart.ParentNode as Aspose.Words.Paragraph;

Aspose.Words.Cell cell;

if (para != null)

{

cell = para.ParentNode as Cell;

if (cell != null && cell.Paragraphs.Count > 1)

{

if (cell.LastParagraph.ToTxt() == "\r\n" && cell.LastParagraph.Runs.Count == 0)

cell.LastParagraph.Remove();

}

}

}

}

GeneratePDF(descDoc, outputPath + "\\" + pdffile);

descDoc.Save(outputPath + "\\" + guid.ToString() + ".doc");

return pdffile;

}

public void MailMerge_MergeImageField(object sender, Aspose.Words.Reporting.MergeImageFieldEventArgs e)

{

string value = e.Field.ToString();

DocumentBuilder builder = new DocumentBuilder(e.Document);

builder.MoveToField(e.Field, true);

Shape shape = new Shape(e.Document, ShapeType.Ellipse);

shape.Width = 20;

shape.Height = 20;

shape.WrapType = WrapType.Inline;

switch (value)

{

case "15":

shape.FillColor = Color.Green;

break;

case "16":

shape.FillColor = Color.Yellow;

break;

case "17":

shape.FillColor = Color.Red;

break;

default:

shape.FillColor = Color.Green;

break;

}

builder.InsertNode(shape);

e.Field.Remove();

}

Hope my code and the template would help us to find out the root cause of the issue :-(