DocX
DocX copied to clipboard
Error 'word found unreadable content' while opening generated document
Hi,
I am using DocX library to generate my Report Document from a predefined document template. While generating report I am replacing various placeholders with Text or Image(s). But once report docx is generated when I try to open it getting error 'Word found unreadable content... trusted source'. As per my requirement I have to generate a report by repeating the template contents multiple times (5 times max). Like I have data stored in DB for multiple locations and for each location it should read the template file, replace placeholders with appropriate location data and then append the modified template contents to the same generated document. To achieve this I am using reportDoc.InsertDocument('Generateddocument', append: true). With this it is generating the document for multiple locations but after export when I try to open the generated document, it gives me above mentioned error with some 'Style1' error msg in popup window.
What could be the reason for this? Kindly help. My work has stuck because of the error.
Thanks in Advance
Hello, Can you submit a code example and your docx so we can analyse the problem ? Thank you.
I won't be able to submit the docx file as it contains customers data. I am sharing my code below try {
if (investigationReportDetails == null || investigationReportDetails.Count() == 0)
return null;
var locations = (from id in investigationReportDetails
select new
{
LocID = id.LocationID,
LocName = id.Location
}).Distinct().AsEnumerable();
var investigationCase = investigationReportDetails.FirstOrDefault().InvestigationCase;
var reportFileName = investigationCase + "_Report.docx";
var templateFilePath = System.Web.HttpContext.Current.Server.MapPath("~/Resources/Report Format for Software (03-June-20).docx");
var reportTempFilePath = System.Web.HttpContext.Current.Server.MapPath("~/InvestigationReportsTemp/" + reportFileName);
if (!File.Exists(templateFilePath))
return null;
using (DocX reportDoc = DocX.Create(reportTempFilePath, Xceed.Document.NET.DocumentTypes.Document))
{
foreach (var loc in locations)
{
DocX templateDoc = DocX.Load(templateFilePath);
templateDoc.ReplaceText("{PresentDate}", DateTime.Now.ToShortDateString());
var invDetails = investigationReportDetails.Where(inv => inv.LocationID == loc.LocID);
if (invDetails != null && invDetails.Any(q => !string.IsNullOrWhiteSpace(q.QuestionAnswer)))
{
//Replace all choice type question paragraphs first
#region Choice type question replacement
var choiceQuestions = invDetails.Where(i => i.QuestionType == Entities.QuestionType.Choice);
if (choiceQuestions != null)
{
foreach (var invQuestion in choiceQuestions)
{
var placeholderText = invQuestion.QuestionPlaceholder;
var placeholderValue = !string.IsNullOrWhiteSpace(invQuestion.QuestionAnswer) ? invQuestion.QuestionAnswer : string.Empty;
var paraContents = "";
if (placeholderValue != string.Empty && invQuestion.OptionsForQuestion != null)
{
var qAnswerChoice = invQuestion.OptionsForQuestion.FirstOrDefault(o => o.ID == Convert.ToInt32(placeholderValue));
paraContents = qAnswerChoice != null ? qAnswerChoice.ParagraphContents ?? string.Empty : string.Empty;
var optionSummary = qAnswerChoice != null ? qAnswerChoice.SummaryStatement ?? string.Empty : string.Empty;
//invQuestion.OptionsForQuestion.FirstOrDefault(o => o.ID == Convert.ToInt32(placeholderValue)).SummaryStatement ?? string.Empty;
if (!string.IsNullOrWhiteSpace(optionSummary))
summaryStatements += !string.IsNullOrWhiteSpace(summaryStatements) ? "\n" + optionSummary : optionSummary;
}
templateDoc.ReplaceText(placeholderText, paraContents, false);
}
}
#endregion
//Replace all text type question values
#region Text type question replacement
var textQuestions = invDetails.Where(i => i.QuestionType == Entities.QuestionType.Text);
if (textQuestions != null)
{
foreach (var invQuestion in textQuestions)
{
var placeholderText = invQuestion.QuestionPlaceholder;
var placeholderValue = !string.IsNullOrWhiteSpace(invQuestion.QuestionAnswer) ? invQuestion.QuestionAnswer : string.Empty;
if (placeholderText == "{Mark}")
{
templateDoc.ReplaceText(placeholderText, "'" + placeholderValue + "'", false);
//headers.First.ReplaceText(placeholderText, "'" + placeholderValue + "'");
}
else
templateDoc.ReplaceText(placeholderText, placeholderValue, false);
}
}
#endregion
//Replace all Image type question with uploaded images
#region Image type question replacement
var imageQuestions = invDetails.Where(i => i.QuestionType == Entities.QuestionType.Image);
if (imageQuestions != null)
{
foreach (var invQuestion in imageQuestions)
{
var placeholderText = invQuestion.QuestionPlaceholder;
var placeholderValue = !string.IsNullOrWhiteSpace(invQuestion.QuestionAnswer) ? invQuestion.QuestionAnswer : string.Empty;
var annexturePlaceholder = invQuestion.ImageAnnexure ?? string.Empty;
var docParagraphs = templateDoc.Paragraphs.Where(p => p.Text != null && p.Text != "" && p.Text.Contains(placeholderText)).ToList();
if (docParagraphs != null && docParagraphs.Count > 0)
{
foreach (var para in docParagraphs)
{
var imageList = !string.IsNullOrWhiteSpace(placeholderValue) ? placeholderValue.Split(',') : null;
if (imageList != null && imageList.Count() > 0 && imageList[0] != null)
{
List<Xceed.Document.NET.Picture> picList = new List<Xceed.Document.NET.Picture>();
foreach (var imageFile in imageList)
{
if (File.Exists(imageFile))
{
var image = templateDoc.AddImage(imageFile);
var questionPic = image.CreatePicture(200, 200);
//picList.Add(questionPic);
if (questionPic != null)
{
para.ReplaceTextWithObject(placeholderText, questionPic, false);
}
else
para.ReplaceText(placeholderText, "", false);
}
}
}
}
}
}
}
#endregion
reportDoc.InsertDocument(templateDoc, false);
}
}
reportDoc.SaveAs(reportTempFilePath);
reportDoc.Dispose();
}
return reportTempFilePath;
}
Couple of more issues facing:
- Final document does not have headers copied from template file. How to copy headers from template?
- Final document also shows Track changes history. how to hide track changes details in final report?
Also, I want to convert the generated report docx file to either Html or PDF for a Preview purpose. Could you please suggest any tool or option in DocX library to generate PDF or HTML out of docx file and send it to MVC view on Preview click. reference to any leads, pointers will also help. I am trying hard with different options but no luck so far.
Thanks in advance
There could be many reasons why the final document can't be opened with MS Word. Here are 3 possibles reasons, which will be fixed in the next release : v1.8: -Unreadable document with big Pictures : instead of using float, we now use long types internally. -Under .NET Core, merging documents in which we insert images throws System.IO.IOException: we now use using statements internally -Document : when merging documents, the styles are duplicated with new Ids: Many internal changes
Maybe there is something special in the templated files. Can you show this one ? Do you have the same problem when not using Document.InsertDocument(), but just replacing placeholders with pictures and trying to open the resulting document with MS Word ?
-Final document does not have headers copied from template file. How to copy headers from template? This will be fixed in v1.8.
-Final document also shows Track changes history. how to hide track changes details in final report? Does the template document shows the track changes history ? Using ReplaceTextWithObject() with a false parameter for the trackChanges parameter should not add the track changes history.
-Convert the generated report docx file to either Html or PDF A docx can be convert to PDF with the commercial version "Xceed Words for .NET". You can try it for free for 45 days here : https://xceed.com/en/our-products/product/words-for-net. You can find the difference between the commercial version and the free version here : https://github.com/xceedsoftware/DocX
I believe I have stumbled across the same issue, and I think it's due to the reuse of the "DocumentElement" in the file.
For my use case there is a document that has multiple places for an image of a signature to be added. <projectmanager_signature> appears twice in the document that gives the error, and only once in the document that does not.
var sig = wordDoc.AddImage(@"Path\To\Image\signatureimage.gif");
wordDoc.ReplaceTextWithObject("<projectmanager_signature>", sig.CreatePicture());
If I simply go into the document and change the second iteration of <projectmanager_signature> with <projectmanager_signaturetwo> and rewrite the code to:
var sig = wordDoc.AddImage(@"Path\To\Image\signatureimage.gif");
wordDoc.ReplaceTextWithObject("<projectmanager_signature>", sig.CreatePicture());
wordDoc.ReplaceTextWithObject("<projectmanager_signaturetwo>", sig.CreatePicture());
It works in this scenario.
P.S. Don't ask why it's a gif, and I won't tell.
Hi nrylee, I've made a test by opening MS Word, input some texts and 2 "<projectmanager_signature>". Then, I created a new project in Visual Studio and include DocX v1.7.1 from NuGet. I then run the following code:
`var wordDoc = DocX.Load("test.docx");
var sig = wordDoc.AddImage( @"balloon.jpg" );
wordDoc.ReplaceTextWithObject( "<projectmanager_signature>", sig.CreatePicture() );
wordDoc.SaveAs("output.docx");`
When I open the resulting "output.docx", I can see the 2 images correctly displayed and the document is not in error. Maybe your document has something special ? Thank you