Different Images on Different Pages

Hi Team,

We are using aspose.word API on enterprise level and getting an issue to show different images on different pages in word document. Is this any way to put a condition within Builder.InsertImage() method to get it right. For example if it is last page then we need to insert another image in footer instead of previous image.

Or any other way to do this?

We need this on urgent basis. It would be great help from your side.

Thanks,
Ahmed Shair

Hi Ahmed,

Thanks for your inquiry. We have PAGE field and NUMPAGES field. PAGE field represents number of current page while NUMPAGES represents the number of pages in document. You can make use of IF field in footer to detect last page e.g.

{ IF "{PAGE}" = "{NUMPAGES}" "Image 1" "Image 2" }

Here is sample code to meet this requirement:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
// { IF "{PAGE}" = "{NUMPAGES}" "Image 1" "Image 2" }
Field field = builder.InsertField("IF \"", null);
builder.MoveTo(field.Start.NextSibling.NextSibling);
builder.InsertField("PAGE", null);
builder.Write("\" = \"");
builder.InsertField("NUMPAGES", null);
builder.Write("\" \"");
BookmarkStart bmTrue = builder.StartBookmark("bmTrue");
builder.EndBookmark("bmTrue");
builder.Write("\" \"");
BookmarkStart bmFalse = builder.StartBookmark("bmFalse");
builder.EndBookmark("bmFalse");
builder.Write("\" ");
builder.MoveToBookmark("bmTrue", true, true);
builder.InsertImage(MyDir + "img1.jpg");
builder.MoveToBookmark("bmFalse", true, true);
builder.InsertImage(MyDir + "img2.jpg");
bmTrue.Bookmark.Remove();
bmFalse.Bookmark.Remove();
doc.Save(MyDir + @"16.12.0.docx");

Hope, this helps.

Best regards,

Hi Awais,

Thank you so much for your help. This is very important piece of code for mine.

By using this code, I’m getting another issue that when we generate word format ‘file.doc’ then footer is working fine and all the images are in exact position but when we generate ‘file.docx’ word format then many of the images in footer are not showing.

Our required format is .docx. Can you please let us know what the issue behind this? Or any other solution to rectify this issue?

Hi Ahmed,

Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input document
  • Aspose.Words generated output DOCX file showing the undesired behavior
  • Aspose.Words generated output DOC file showing the correct behavior
  • Sample images if required
  • Please create a standalone console application (source code without compilation errors) that helps us reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start further investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip them and Click ‘Reply’ button that will bring you to the ‘reply page’ and there at the bottom you can include any attachments with that post by clicking the ‘Add/Update’ button.

Best regards,

Hi Awais,

Thanks you so much for your response.

Please find attached folder containing input document (original.html) file, Output Docx file, Output Doc file, two images already included in the project directory and Standalone Console Application.

Original.html and two images, are in project root directory.

In Docx format footer icons are working fine as you can see in sample file but in doc file icons are not displaying besides last page. But if we click on footer portion to edit this then images are showing which is not correct.

Our required format is .Docx and we can’t use doc format. Please expedite us to rectify this issue as soon as possible because our customers are continuously complaining us for this issue.

Hi Ahmed,

Thanks for your inquiry. This seems to be a glitch in MS Word. However, you can make your output DOCX appear correctly in MS Word 2016 by using the following code:

Document doc = new Document(MyDir + @"Output.DOCX");
doc.Save(new MemoryStream(), SaveFormat.Doc);
doc.Save(MyDir + @"17.1.0.docx");

Best regards,

Hello Awais,

We have consult with MS Team, as you have said it is a glitch in MS word, Please see the whole conversation on this topic.

https://answers.microsoft.com/en-us/msoffice/forum/all/difference-between-doc-and-docx-word-formats/d681ab71-b685-463d-b7cf-1c5a9ab5fc11

It doesn’t seems to be like that. Can you please investigate this issue in Aspose API?

Thanks,
Ahmed Shair

Hi Ahmed,

Thanks for your inquiry. We are working over your query and will get back to you soon.

Best regards,

Hi Ahmed,

Thanks for your inquiry. We tested the scenario and have managed to reproduce the same problem on our end. For the sake of correction, we have logged this problem in our issue tracking system. The ID of this issue is WORDSNET-14780. Our product team will further look into the details of this problem and we will keep you updated on the status of this issue. We apologize for your inconvenience.

Best regards,

Thanks Awais!

Please try to rectify this issue as soon as possible because our many customers are reporting this issue. We are curious about this.

Thanks,
Ahmed Shair

Hi Ahmed,

Thanks for your inquiry. Unfortunately, your issue is not resolved yet. Currently it is pending for analysis and is in the queue. We will inform you via this thread as soon as this issue is resolved. We apologize for any inconvenience.

Best regards,

Hi Ahmed,

Regarding WORDSNET-14780, it turns out to be a strange MS Word behavior. But you can try using one of the following ways to work this problem around:

1- Insert images as VML – please see the following code:

builder.MoveToBookmark("bmTrue", true, true);
Shape footerStop = new Shape(doc);
footerStop.ImageData.SetImage(@"FooterStop.PNG");
builder.CurrentNode.ParentNode.InsertAfter(footerStop, builder.CurrentNode);
builder.MoveToBookmark("bmFalse", true, true);
Shape footerarrow = new Shape(doc);
footerarrow.ImageData.SetImage(@"Footerarrow.PNG");

2- Or you can save your document to DOC and then to DOCX:

// Save to DOC and back to DOCX to get expected result.
MemoryStream docStream = new MemoryStream();
doc.Save(docStream, SaveFormat.Doc);
doc = new Document(docStream);
doc.Save("out.docx");

Hope, this helps.

Best regards,

Hi Ahmed,

Regarding WORDSNET-14780, our product team has completed the work on your issue and has come to a conclusion that this issue and the undesired behavior you’re observing is actually not a bug in Aspose.Words for .NET. So, we had closed this issue as ‘Not a Bug’. Please use workarounds from my previous post.

Best regards,