Free Support Forum - aspose.com

Want to insert dynamic images to the word document

Received : 2007/09/18 03:29:48
Message : Hi,

A folder contains some report images for different users. I am having a requirement to insert some images rom the folder to a word document dynamically based on the user.

Is there any help for this?

Sincerely,

Manikanta Jilakara


This message was posted using Aspose.Live 2 Forum

Hi

Thanks for your request. You can use the DocumentBuilder.InsertImage method to insert images into word document. See the following link for more information.
http://www.aspose.com/Products/Aspose.Words/Api/InsertinganImage.html

Best regards.

Thanks for quick reply! But previously i used to insert text to a microsoft word document by using an option called merge fields in the document.

The code which i am using for that purpose is

Aspose.Words.Document doc = new Aspose.Words.Document(strFilePath + @"\DossierTemplate.doc");

doc.MailMerge.ExecuteWithRegions(dtExecutiveIdleProfile);

doc.MailMerge.Execute(drNewLoc);

String[] str = doc.MailMerge.GetFieldNames();

doc.Save(@strFilePath + @"\Report\" + strFileName, Aspose.Words.SaveFormat.Doc);

But now i need to add some images in between the texts. So which option i need to take in the microsoft word document and what is the required code from aspose.

Sincerely,

Manikanta

Hi

Thanks for your request. You should insert into your template MergeField with name <> for example. Then use the following code.

public void TestImageMailMerge()

{

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

string[] keys = { "myImage" };

string[] values = { "test.jpg" };

doc.MailMerge.MergeImageField += new MergeImageFieldEventHandler(HandleMergeImage);

doc.MailMerge.Execute(keys, values);

doc.Save(@"046_ikarussoft\out.doc");

}

private void HandleMergeImage(object sender, MergeImageFieldEventArgs e)

{

if (e.FieldName == "myImage")

{

string shortFileName = e.FieldValue as string;

//you should get arrey of bytes from your database

e.ImageFileName = System.IO.Path.Combine(@"046_ikarussoft\images", shortFileName);

}

Also see the following link.
http://www.aspose.com/Products/Aspose.Words/Api/TheMergeImageFieldEvent.html

I hope that it will help you.

Best regards.

Hi Vitaly Shtanko,

This is Excelent, and one more thing the image which i have is a lanscape one and i want to show the image as portrait in the word document.

Will you please give me some idea/code to solve this?

Sincerely,

Manikanta Jilakara

Hi

Please do what you want using MS Word and attach the created document here. I will investigate it and try to help you.

Best regards.

Hi,

Please find the attachmet for the word document. In page three you will find one image. I want that image to be in potrate. It is currently in landscape.

Will you please give me some idea regarding this?

Sincerely,

Manikanta Jilakara

Hi

Thanks for additional information. Try to use the following code.

public void TestmailMarge_94341()

{

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

string[] keys = { "myImage" };

string[] values = { "test.bmp" };

doc.MailMerge.MergeImageField += new MergeImageFieldEventHandler(HandleMergeImage_94341);

doc.MailMerge.Execute(keys, values);

doc.Save(@"185_94341_Saahi\out1.doc");

}

private void HandleMergeImage_94341(object sender, MergeImageFieldEventArgs e)

{

if (e.FieldName == "myImage")

{

DocumentBuilder builder = new DocumentBuilder(e.Document);

builder.MoveToField(e.Field, true);

e.Field.Remove();

string shortFileName = e.FieldValue as string;

string filename = System.IO.Path.Combine(@"185_94341_Saahi\", shortFileName);

Image image = Image.FromFile(filename);

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

shape.ImageData.SetImage(image);

shape.WrapType = WrapType.Inline;

shape.Height = builder.CurrentSection.PageSetup.PageHeight - builder.CurrentSection.PageSetup.TopMargin - builder.CurrentSection.PageSetup.BottomMargin;

shape.Width = builder.CurrentSection.PageSetup.PageWidth - builder.CurrentSection.PageSetup.LeftMargin - builder.CurrentSection.PageSetup.RightMargin;

shape.Rotation = -90;

builder.InsertNode(shape);

}

}

I hope that it will help you.

Best regards.

Hi,

You suppoet is excellent! Now i got the report exactly. One more thing i want is, your code is working for one image but i am having 4 images one after other. Can you give me some information/code for this.

Sincerely,

Manikanta

Hi

You can modify this code like following.

if (e.FieldName == "myImage" || e.FieldName == "myImage1" || e.FieldName == "myImage3")

Or use the following method to resize image.

///

/// Resize source image to specifed sze

///

/// Source image

/// Target width

/// Target height

/// Resized image

private Image ResizeImage(Image sourceImage, int targetWidth, int targetHeight)

{

float ratioWidth = (float)sourceImage.Width / targetWidth;

float ratioHeight = (float)sourceImage.Height / targetHeight;

if (ratioWidth > ratioHeight)

targetHeight = (int)(targetHeight * (ratioHeight / ratioWidth));

else

{

if (ratioWidth < ratioHeight)

targetWidth = (int)(targetWidth * (ratioWidth / ratioHeight));

}

Image outputImage = sourceImage.GetThumbnailImage(targetWidth, targetHeight, null, new IntPtr());

return outputImage;

}

I hope that it will help you.

Best regards.

Hi,

Thanks for quick reply but it is not working. will you please see the following code and suggest me the changes?

public void TestImageMailMerge()
{

//for first image
string strFilePath = Server.MapPath(@".\DetailsReport");
string strFileName = "DetailReport" + Session["ExecutiveId"].ToString() + ".Doc";

Aspose.Words.Document doc = new Aspose.Words.Document(strFilePath + @"\DossierTemplate.doc");
XFilename3 = Server.MapPath(@"..\ImageMail\") + "PCGraphical" + Session["ExecutiveId"] + ".jpg";
string[] keys3 = { "Background" };

string[] values3 = { XFilename2 };
doc.MailMerge.MergeImageField += new MergeImageFieldEventHandler(HandleMergeImage);
doc.MailMerge.Execute(keys3, values3);

//for second image
strFilePath = Server.MapPath(@".\DetailsReport");
strFileName = "DetailReport" + Session["ExecutiveId"].ToString() + ".Doc";

XFilename = Server.MapPath(@"..\ImageMail\") + "ECGraphical" + Session["ExecutiveId"] + ".jpg";
string[] keys = { "Experience" };

string[] values = { XFilename };
doc.MailMerge.MergeImageField += new MergeImageFieldEventHandler(HandleMergeImage);
doc.MailMerge.Execute(keys, values);
//for third image
strFilePath = Server.MapPath(@".\DetailsReport");
strFileName = "DetailReport" + Session["ExecutiveId"].ToString() + ".Doc";


XFilename1 = Server.MapPath(@"..\ImageMail\") + "CCGraphical" + Session["ExecutiveId"] + ".jpg";
string[] keys1 = { "Competency" };

string[] values1 = { XFilename1 };
doc.MailMerge.MergeImageField += new MergeImageFieldEventHandler(HandleMergeImage);
doc.MailMerge.Execute(keys1, values1);
//for fourtn image
strFilePath = Server.MapPath(@".\DetailsReport");
strFileName = "DetailReport" + Session["ExecutiveId"].ToString() + ".Doc";


XFilename2 = Server.MapPath(@"..\ImageMail\") + "PCGraphical" + Session["ExecutiveId"] + ".jpg";
string[] keys2 = { "Personality" };

string[] values2 = { XFilename2 };
doc.MailMerge.MergeImageField += new MergeImageFieldEventHandler(HandleMergeImage);
doc.MailMerge.Execute(keys2, values2);

doc.Save(@strFilePath + @"\Report1\" + strFileName, Aspose.Words.SaveFormat.Doc);

}
private void HandleMergeImage(object sender, MergeImageFieldEventArgs e)
{
string strFilePath = Server.MapPath(@".\DetailsReport");
string strFileName = "DetailReport" + Session["ExecutiveId"].ToString() + ".Doc";

if (e.FieldName == "BackGround" || e.FieldName == "Experience" || e.FieldName == "Competency" || e.FieldName == "Personality")


{
DocumentBuilder builder = new DocumentBuilder(e.Document);
builder.MoveToField(e.Field, true);
e.Field.Remove();
string shortFileName = e.FieldValue as string;
//you should get arrey of bytes from your database
string filename = System.IO.Path.Combine(@strFilePath + @"\Report\" + strFileName, shortFileName);
System.Drawing.Image image = System.Drawing.Image.FromFile(filename);
Shape shape = new Shape(e.Document, ShapeType.Image);
shape.ImageData.SetImage(image);
shape.WrapType = WrapType.Inline;
shape.Height = builder.CurrentSection.PageSetup.PageHeight - builder.CurrentSection.PageSetup.TopMargin - builder.CurrentSection.PageSetup.BottomMargin;
shape.Width = builder.CurrentSection.PageSetup.PageWidth - builder.CurrentSection.PageSetup.LeftMargin - builder.CurrentSection.PageSetup.RightMargin;
shape.Rotation = -90;
builder.InsertNode(shape);
}


}

Sincerely,

Manikanta Jilakara

Can you also attach your template file?

Best regards.

Please find the attachement for the template.

Hi

I have modified your code. Try to use it.

public void TestImageMailMerge()

{

//for first image

string strFilePath = Server.MapPath(@".\DetailsReport");

string strFileName = "DetailReport" + Session["ExecutiveId"].ToString() + ".Doc";

Aspose.Words.Document doc = new Aspose.Words.Document(strFilePath + @"\DossierTemplate.doc");

XFilename1 = Server.MapPath(@"..\ImageMail\") + "PCGraphical" + Session["ExecutiveId"] + ".jpg";

XFilename2 = Server.MapPath(@"..\ImageMail\") + "ECGraphical" + Session["ExecutiveId"] + ".jpg";

XFilename3 = Server.MapPath(@"..\ImageMail\") + "CCGraphical" + Session["ExecutiveId"] + ".jpg";

XFilename4 = Server.MapPath(@"..\ImageMail\") + "PCGraphical" + Session["ExecutiveId"] + ".jpg";

string[] keys = { "Background", "Experience", "Competency", "Personality" };

string[] values = { XFilename1, XFilename2, XFilename3, XFilename4 };

doc.MailMerge.MergeImageField += new MergeImageFieldEventHandler(HandleMergeImage);

doc.MailMerge.Execute(keys3, values3);

doc.Save(@strFilePath + @"\Report1\" + strFileName, Aspose.Words.SaveFormat.Doc);

}

Best regards.

I have also modified your template. See the attachment.

Best regards.

Thank you very much for you good support!

Sincerely,

Manikanta Jilakara

Hi,

We are having a requirement that we need to add some text on the top of the image which is in landscape.

Please find the sample image as attachment. We are succeded to insert image but we need to add the highlited text. Red part is static text and the yellow part is the dynamic data from the database.

Please help me regarding this as this is a very urgent for us.

Hi

Thanks for your request. You can set page orientation = landscape. Also I have modified your template file. See the attachment.

Best regards.

Thanks for your replu. But where i can set the page orientation=lanscape. I have the image in landscape. I need that top text also should be in landscape direction. Can you be bit clear.

Please find the attachmet for the report. You can find the difference between the two image. I want the top one exactly equalent to bottom image. See the highlighted part of bottom for difference.

I think you know exactly what i want now. Will you please support me regarding this issue.

Sincerely,

Manikanta Jilakara

Hi

Sorry for my poor explanation. See the following link to learn how to set page orientation.

http://wordprocessing.about.com/od/wordprocessingsoftware/l/blOrientation.htm

Also, you can insert table into your template. Table should have one row with two cells. Click on the first cell of table, select “Text Direction” in the context menu and then select direction of text that you want. Insert your image in the second cell. Also disable borders of the table. See attached file.

Best regards.