Image caching on generated documents?

Hi there,

I am generating a Word document and replacing embedded images inside the document. I have come across a weird issue. I generate a document from a template and open it up from the response, it looks good. Now I leave document 1 open and generate document 2 using the same template but with different images. I open up the document 2 from the response again and it uses document 1's images, not the images that were suppose to be there.

It seems that if more than 1 generated document is opened, all documents show document 1's images. Is there a way to "disable" caching for the embedded images?

I have tried a few avenues including having unique filenames, flushing the response, setting "no-cache" on the response, and clearing the reponse after doc.Save()...but they dont seem to solve the problem. Any help would be appreciated. Thanks!

John

You probably mean saving documents to HTML?

When you save a document to HTML, images cannot be saved inside the HTML file and Aspose.Words writes them as separate files in the same folder as the HTML file. It gives them names like "myfilename.001.png" etc.

As you can see this could result in two problems if you do it on a web server:

1. The HTML and image files will remain on the server. You need implement some sort of a periodic process to cleanup directories where the generated files are saved to.

2. Multiple users requesting generation of an HTML file with the same name at the same time will have an issue with images from one document overwriting images of the other document. Unfortunately, it is responsibility of your code, not Aspose.Words' to deal with this issue.

The simples solution I see is to generate a unique directory name for each user's HTML generation request and save the file there. The images will be stored in that folder too, completely isolating output of each HTML generation by Aspose.Words.

Do something like this:

string myOutputPath = "blah\blah\" + Guid.New().ToString();

doc.Save(myOutputPath + "\myFileName.html");

Sorry for not attaching code to my post. Here is my save code..

doc.Save("Generated_" + fileName, Aspose.Words.SaveFormat.FormatDocument, Aspose.Words.SaveType.OpenInWord, Response);

I am saving documents in MS Word format with the images embedded into the Word document.

I have tried to reproduce the problem in my test, generating three successive documents based on one template with three different images inserted. I have left them all opened after generation and everything worked correctly.

Try to recheck you document generation routine, the problem should be there somewhere.

Or you can attach the test project reproducing the problem. We will research it and try to find what the problem is caused by.

Best regards,

Yes, if you save as DOC and images are inside the document there is nothing shared or cached inside Aspose.Words. Different document objects are completely separate and there can be no caching in between them. There must be something with the way you generate the images (do you generate them?) or caching on the client machine? But how could only an image can be cached, but not a whole document?

I am puzzled myself. All the text changes, but its the images that seem to remember itself. I am scanning thru the document looking for images, and replacing them. This ia all done on the server, and when the doc is ready, it is pushed to the client thru the response. Here is my image replacement code...

// Get all floating shapes in the document
NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);

// Iterate through all floating shapes in the document
ImageFileNames imageFileNames = new ImageFileNames(proposalInfo);
foreach (Aspose.Words.Drawing.Shape shape in shapes)
{
//replace image
if (shape.IsPicture)
{
try
{
//get the name of the new file
string imageName = imageFileNames.getImageFileName(shape.Alt, shape.ImageData.Src, shape.ImageData.Title);
if (!imageName.Equals(""))
{
Image image = Image.FromFile(server_path + imageName);
shape.ImageData.SetImage(image);

//fit the image inside the frame preserving the ratio
double imageWidth = ConvertUtil.PixelToPoint(image.Width, image.HorizontalResolution);
double ratio = shape.Width / imageWidth;
shape.Height = ConvertUtil.PixelToPoint(image.Height, image.VerticalResolution) * ratio;
}
}
catch (Exception ex)
{
simpleLogger.Error(ex.Message);
simpleLogger.Error(ex.StackTrace);
}
}
}

First of all check you are on the latest version of Aspose.Words. I don't think we have .Alt and .Src properties. They are AlternativeText and SourceFullName respectively. Maybe what you have is 4.0 Beta.

Next, I'm not sure what you have in ImageData.SourceFullName, but if you look at the API documentation you will see that If SourceFullName is not an empty string, the image is linked. Maybe this causes some problem, I suggest you set SourceFullName to empty string when you want the image to be just embedded in the Word document.

Finally, SetImage(Image) is probably not the most efficient way as the image needs to be loaded into memory and internally Aspose.Words saves it into bytes in memory again. If you are happy with performance, keep as is. But if you had many images in the document and wanted to squeese few seconds out of it, you should try using SetImage(string) that takes image file name. The code below, that changes the shape size will still be needed, but you can use ImageData.ImageSize property to get the image size, not Image.Width and Image.Height. There are already values in points available there: ImageSize.WidthPoints, ImageSize.HeightPoints.

Thanks for the tips. I will get the latest version of Aspose and make the changes suggested.

I've made the changes suggested, including upgrading to the new version of Aspose, and it still seems to be happening. Here is my new image replacement code..

// Get all floating shapes in the document

NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);

// Iterate through all floating shapes in the document

ImageFileNames imageFileNames = new ImageFileNames(proposalInfo);

foreach (Aspose.Words.Drawing.Shape shape in shapes)

{

//replace image

if (shape.IsImage)

{

try

{

//get the name of the new file

string imageName = imageFileNames.getImageFileName(shape.AlternativeText, shape.ImageData.SourceFullName, shape.ImageData.Title);

if (!imageName.Equals(""))

{

//change the image

shape.ImageData.SetImage(server_path + imageName);

//fit the image inside the frame preserving the ratio

double imageWidth = shape.ImageData.ImageSize.WidthPoints;

double ratio = shape.Width / imageWidth;

shape.Height = shape.ImageData.ImageSize.HeightPoints * ratio;

//embed the image into the work doc

shape.ImageData.SourceFullName = "";

}

}

catch (Exception ex)

{

simpleLogger.Error(ex.Message);

simpleLogger.Error(ex.StackTrace);

}

}

and my new doc.Save() code...

doc.Save("Generated_" + DateTime.Now.ToLongTimeString() + fileName, Aspose.Words.SaveFormat.Doc, Aspose.Words.SaveType.OpenInWord, Response);

I have attached 3 generated documents for reference. The are:

car.doc, bank.doc and tech.doc

All 3 documents are generated from the same template. There are 3 images in there that are being replaced, top left logo, top right copier image, and the main industry image. Car.doc has a car related image, bank.doc has a bank related image, and tech.doc has an IT related image.

To replicate the problem, open up any doc, keep it open and open the other 2 documents. You will notice that the later 2 documents show the first document's images and not their own images. However, if you open each document separately (only 1 instance of MS Word at any given point), then the correct images appear.

Seems like I can only attach 1 file per message. Here is car.doc

and finally tech.doc

Thanks for providing additional info. Now I can see the problem, although I do not understand yet what might be causing it. Looks more like MS Word bug to me, although we may probably be able to find a workaround.

I have logged it to our defect base as issue #1464. We will research it ASAP and let you know of the results.

Best regards,

I'm trying out other ways of replacing the image in hopes of it working. Is there a way to completely remove the image inside the shape?

We fixed this, it will come out in the next release soon.

Whoa, you guys are fast!! thanks!

Any estimates as to when the next release will happen?

Fixed in Aspose.Words 4.1.1 which is now available for download.