I am creating and saving a Word file using the following c#:
WordDocument document = new WordDocument();
//Adds a new section into the Word document
IWSection section = document.AddSection();
//Specifies the page margins
section.PageSetup.Margins.All = 50f;
//Adds a new simple paragraph into the section
IWParagraph Paragraph1 = section.AddParagraph();
////Sets the paragraph's horizontal alignment as center
Paragraph1.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
//Adds a text range into the paragraph
IWTextRange TextRange1 = Paragraph1.AppendText(title);
//sets the font formatting of the text range
TextRange1.CharacterFormat.Bold = true;
TextRange1.CharacterFormat.FontName = "Georgia";
TextRange1.CharacterFormat.FontSize = 20;
//Adds another text range into the paragraph
IWParagraph Paragraph2 = section.AddParagraph();
////Sets the paragraph's horizontal alignment as center
Paragraph2.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Left;
IWTextRange TextRange2 = Paragraph2.AppendText("Introduction");
TextRange2.CharacterFormat.Bold = true;
TextRange2.CharacterFormat.FontName = "Georgia";
TextRange2.CharacterFormat.FontSize = 14;
//Adds another text range into the paragraph
IWTextRange TextRange3 = Paragraph2.AppendText(newintro);
TextRange3.CharacterFormat.FontName = "Georgia";
TextRange3.CharacterFormat.FontSize = 12;
IWTextRange TextRange4 = Paragraph2.AppendText("Ingredients");
TextRange4.CharacterFormat.Bold = true;
TextRange4.CharacterFormat.FontName = "Georgia";
TextRange4.CharacterFormat.FontSize = 14;
//Adds another text range into the paragraph
IWTextRange TextRange5 = Paragraph2.AppendText(newingr);
TextRange5.CharacterFormat.FontName = "Georgia";
TextRange5.CharacterFormat.FontSize = 12;
TextRange5.CharacterFormat.Italic = true;
IWTextRange TextRange6 = Paragraph2.AppendText("Instructions");
TextRange6.CharacterFormat.Bold = true;
TextRange6.CharacterFormat.FontName = "Georgia";
TextRange6.CharacterFormat.FontSize = 14;
//Adds another text range into the paragraph
IWTextRange TextRange7 = Paragraph2.AppendText(newsteps);
TextRange7.CharacterFormat.FontName = "Georgia";
TextRange7.CharacterFormat.FontSize = 12;
TextRange7.CharacterFormat.Italic = true;
try
{
PermissionStatus status = await CheckAndRequestWritePermission();
Stream stream = new FileStream(path, FileMode.OpenOrCreate);
document.Save(stream, FormatType.Docx);
document.Close();
}
The Word file is created and saved without errors, but when I try to open it I get an error message:
Syncfusion.Compression.Zip.ZipException: 'Zip exception.Can't locate end of central directory record. Possible wrong file format or archive is corrupt.'
The c# to open the file is:
Stream stream = new FileStream(path, FileMode.OpenOrCreate);
WordDocument document = new WordDocument();
document.Open(stream, FormatType.Docx);
The path variabe is set for bot saving and opening to:
/storage/emulated/0/Documents/Fave con pecorino-Broad beans with pecorino.docx
Could you please investigate
Hi Paul,
We have tried to reproduce the reported issue using the given details at our
end.
From the given code snippet, we found that you haven’t closed the stream after saving
the Word document which leads to document corruption. So, when reopening the saved
corrupted Word document, the reported ZipException occurs.
In order to make this work, we suggest you close the stream after saving the Word
document and then reopen the saved Word document. Please refer the below highlighted
code snippet to resolve the reported issue.
//Adds another text range into the paragraph
IWTextRange TextRange7 = Paragraph2.AppendText(newsteps);
TextRange7.CharacterFormat.FontName = "Georgia";
TextRange7.CharacterFormat.FontSize = 12;
TextRange7.CharacterFormat.Italic = true;
try
{
PermissionStatus status = await CheckAndRequestWritePermission();
Stream stream = new FileStream(path, FileMode.OpenOrCreate);
document.Save(stream, FormatType.Docx);
document.Close();
stream.Close();
}
Regards,
Akash A.