DocX icon indicating copy to clipboard operation
DocX copied to clipboard

Word finds unreadable content after adding multiple pictures

Open cdipierr opened this issue 7 years ago • 15 comments

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?

cdipierr avatar Jan 03 '19 15:01 cdipierr

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/

XceedBoucherS avatar Jan 11 '19 14:01 XceedBoucherS

Just tried with the latest from that site, and yes it still occurs.

cdipierr avatar Jan 11 '19 15:01 cdipierr

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");

XceedBoucherS avatar Jan 11 '19 20:01 XceedBoucherS

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?

cdipierr avatar Jan 11 '19 20:01 cdipierr

Yes please. You can attach your original document and even pictures so we can test it as you.

XceedBoucherS avatar Jan 14 '19 12:01 XceedBoucherS

Sure thing. Please see the template here, and the 3 photos (code is your exact code above). Photo Template - No Logo.docx cat1 cat2 leak3

cdipierr avatar Jan 14 '19 15:01 cdipierr

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.

VividSteph avatar Mar 05 '19 10:03 VividSteph

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;

XceedBoucherS avatar Apr 03 '19 16:04 XceedBoucherS

You can try and remove the line in the footer of your template to see if it helps.

XceedBoucherS avatar Apr 03 '19 19:04 XceedBoucherS

You can also try to reduce the size of your images.

XceedBoucherS avatar Apr 03 '19 19:04 XceedBoucherS

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!

cdipierr avatar May 21 '19 15:05 cdipierr

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.

GGJason avatar Jul 04 '19 16:07 GGJason

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.

chriscfox avatar Jun 21 '20 16:06 chriscfox

Has there been any progress with this issue? Even knowing what the maximum files size limit is would be helpful.

chriscfox avatar Apr 30 '21 15:04 chriscfox

Hi, We will try to see what could be the problem....or the file size limit. Thank you for your patience.

XceedBoucherS avatar May 05 '21 11:05 XceedBoucherS