Word finds unreadable content after adding multiple pictures
I have a Word doc that I'm adding pictures to following the pattern of: var document = DocX.Load() var img = document.AddImage var para = <find the paragraph I care about> var picture = image.CreatePicture() para.AppendPicture(picture); document.SaveAs();
This works fine if I do it once. If I do it twice, Word will pop up an error of "Word found unreadable content in ... Do you want to recover the contents of this document?"
If I tell Word yes, then it opens fine and the pictures look as expected in the spot I'd expect them.
Does anyone know how to get Word to tell me more details about the error it found or somehow prevent the error from occurring?
Hi, Is this happening in the latest version ? You can test it for free for 45 days from here : https://xceed.com/xceed-words-for-net/
Just tried with the latest from that site, and yes it still occurs.
Hi,
Your initial loaded document could have something not right ?
Here's my test with v1.2. The loaded document contains only simple text. The resulting document looks as expected with the 3 images.
var document = DocX.Load( "input.docx" );
var img = document.AddImage( "balloon.jpg" );
var para = document.InsertParagraph( "First " );
var picture = img.CreatePicture();
para.AppendPicture( picture );
var img2 = document.AddImage( "W.png" );
var para2 = document.InsertParagraph( "Second " );
var picture2 = img2.CreatePicture();
para2.AppendPicture( picture2 );
var img3 = document.AddImage( "WordsIcon.png" );
var para3 = document.InsertParagraph( "Third " );
var picture3 = img3.CreatePicture();
para3.AppendPicture( picture3 );
document.SaveAs("output.docx");
If I start with my original document and run your code above (with 3 random pictures I happen to have), I get the same issue. Would it make sense for me to provide more original document for you to give a look at?
Yes please. You can attach your original document and even pictures so we can test it as you.
Sure thing. Please see the template here, and the 3 photos (code is your exact code above).
Photo Template - No Logo.docx

Not sure if this is still an issue for you but we had a similar problem where adding multiple copies of the same image, over a certain number, would corrupt the docx. It would only happen with one template file and inspecting document.xml showed that we had duplicate IDs for our pictures.
IDs are assigned using DocX.GetNextFreeDocPrId which builds a HashSet of existing IDs and uses this to find the next available number. However, if the HashSet is not sequential then we end up with the "first highest" number which is then just incremented by one for each new picture and we eventually overlap with the remaining HashSet items. For example, existingIds = 1,2,3,4,7,8. The next available Id is 5 (then 6, then 7, and from this point we're overlapping IDs).
In our case we were able to use the max ID from the HashSet instead.
Hi,
I cannot reproduce the issue with the "Photo Template - No Logo.docx", the 3 images and the code above.
@VividSteph idea is not bad also. You can try it in DocX.GetNextFreeDocPrId() by replacing
while( existingIds.Contains( newDocPrId.ToString() ) ) newDocPrId++;
with:
if( existingIds.Count > 0 ) { newDocPrId = existingIds.Max( id => long.Parse( id ) ) + 1; }
You can also make sure you have an English culture :
var culture = new System.Globalization.CultureInfo( "en-US" ); Thread.CurrentThread.CurrentCulture = culture; Thread.CurrentThread.CurrentUICulture = culture;
You can try and remove the line in the footer of your template to see if it helps.
You can also try to reduce the size of your images.
Hey all --
Sorry it's taken so long to get back to this. I grabbed the latest XCeed.Words.NET (1.3 instead of 1.1) release and tried my example above w/o error. However, in my real use case I again got the issue.
However, removing the footer line as suggested above seems to have resolved. I don't really understand why that footer matters, but that seems like a good enough solve for now.
Thanks!
I have the same problem. After reading the thread above, I check my template and result. Finally I find that the text box in header can make this error, too.
I get this error with even a single image if the file is above a certain size. I am not sure what exactly the size limit is.
Has there been any progress with this issue? Even knowing what the maximum files size limit is would be helpful.
Hi, We will try to see what could be the problem....or the file size limit. Thank you for your patience.