We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Word file is getting corrupt after inserting table and image

Word file is getting corrupted, not sure what’s the issue? Please refer attached file.


Calling code:-
FormatWordFile(File.ReadAllBytes(path), CreateDataTableForFormatting(), true, “KAVITA_MANJUNATH_NAIK.docx”);


private DataTable CreateDataTableForFormatting()
{
// Initialize DataTable
using (DataTable dt = new DataTable())
{
dt.Columns.Add(“Candidate Name”);
dt.Columns.Add(“Email Id”);
dt.Columns.Add(“Phone Number”);
dt.Columns.Add(“Remarks”);

DataRow dr = dt.NewRow();

dr[0] = “Vikas Kumar Gupta”;
dr[1] = "XXX@YYY.com";
dr[2] = “XXXXXXXXXX”;
dr[3] = “Over 5 years of experience in .net platform developing Web-based and Windows-based applications.”;
dt.Rows.Add(dr);
return dt;
}
}

private static Document Load(byte[] content)
{
using (MemoryStream s = new MemoryStream(content))
{
return new Document(s);
}
}

public static Stream FormatWordFile(byte[] content, DataTable dt, bool IsResposeImmediate, string FileName)
{
Document doc = Load(content);

Section section = new Section(doc);
section.AppendChild(new Body(doc));

Table table = new Table(doc);

//section.Body.Paragraphs.Add(table);
doc.Sections.Insert(0, section);

//table.AutoFit(AutoFitBehavior.FixedColumnWidths);

for (int i = 0; i < dt.Columns.Count; i++)
{
Row row = new Row(doc);
row.RowFormat.AllowBreakAcrossPages = true;
table.AppendChild(row);

Cell cell = new Cell(doc);
//cell.CellFormat.Shading.BackgroundPatternColor = Color.LightBlue;
cell.CellFormat.TopPadding = 4;
cell.CellFormat.RightPadding = 4;
cell.CellFormat.BottomPadding = 4;
cell.CellFormat.LeftPadding = 4;
cell.CellFormat.Shading.Texture = TextureIndex.Texture20Percent;
cell.CellFormat.Shading.ForegroundPatternColor = Color.Black;
cell.CellFormat.WrapText = false;

cell.CellFormat.Width = 80;
cell.AppendChild(new Paragraph(doc));
cell.FirstParagraph.AppendChild(new Run(doc, dt.Columns[i].ColumnName));
row.AppendChild(cell);

cell = new Cell(doc);
//cell.CellFormat.Shading.BackgroundPatternColor = Color.LightBlue;
cell.CellFormat.Width = 80;
cell.AppendChild(new Paragraph(doc));
cell.FirstParagraph.AppendChild(new Run(doc, dt.Rows[0][i].ToString()));
row.AppendChild(cell);
}

table.Alignment = TableAlignment.Center;
table.Style.Font.Size = 10;
table.Style.Font.Color = Color.Black;
table.Style.Font.Name = “Tahoma”;
table.Style.ParagraphFormat.Alignment = ParagraphAlignment.Left;

DocumentBuilder builder = new DocumentBuilder(doc);

builder.InsertImage(TR_WEB.ClsCommon.ServerLogoPath,
Aspose.Words.Drawing.RelativeHorizontalPosition.LeftMargin,
section.PageSetup.LeftMargin,
Aspose.Words.Drawing.RelativeVerticalPosition.TopMargin,
section.PageSetup.TopMargin,
-1,
-1,
Aspose.Words.Drawing.WrapType.TopBottom
);

builder.InsertHtml(“Profile Snapshot”);

section.Body.Paragraphs.Add(table);

table.AutoFit(AutoFitBehavior.AutoFitToWindow);

if (IsResposeImmediate)
{
doc.Save(HttpContext.Current.Response, FileName, ContentDisposition.Attachment, null);
return null;
}
else
{
Stream stream = new MemoryStream();
doc.Save(stream, HandleSave(FileName));
return stream;
}
}

Problem is same when using trailing code. Looks like issue with “SaveOptions”.


string path = @“C:\Documents and Settings\shiv computers\Desktop\temp\KAVITA_MANJUNATH_NAIK.docx”;
string FileName = “KAVITA_MANJUNATH_NAIK.docx”;
Aspose.Words.Document doc = new Document(path);
doc.Save(HttpContext.Current.Response, FileName, ContentDisposition.Attachment, Aspose.Words.Saving.DocSaveOptions.CreateSaveOptions(Path.GetExtension(FileName)));

Looks that there is no issue with inserting table and image.

Our requirement is to insert one table and image and forward the file to the browser.

Hi Gupta,


Thanks for your inquiry. This does not seem to be an issue in Aspose.Words. You can resolve the problem with DOCX by simply adding Response.End() after sending document to client browser. For some reasons, when you save a DOCX document to client browser, content of web page is added at the end of the document, you can see this if you open your DOCX document in any binary editor. That is why Microsoft Word cannot open this document. If you add Response.End(), content of web page will not be added and document will be valid. Hope, this helps.

Best regards,

Thank you for your help, it solved our problem