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;
}
}