Free Support Forum - aspose.com

Render Aspose.Words docx in ReportViewer

I’m using Aspose.Words (10.2.0.0 - Trial Version) to convert an HTML document (see attached input.txt) into a DOCX document (see attached output2.docx) using the following code:

void export_Click(object sender, EventArgs e)
{
Document doc = new Document(Server.MapPath("/input.html"));
RemoveContentFromMergedCells(doc);
doc.Save(Response, “output2.docx”, ContentDisposition.Inline, null);

Response.End();

}

protected void RemoveContentFromMergedCells(Document doc)
{

// Remove content from merged cells.
// Get collection of cells in the docuemnt.
NodeCollection cells = doc.GetChildNodes(NodeType.Cell, true);

foreach (Cell cell in cells)
{
// Check whether cell is merged with previouse.
if (cell.CellFormat.HorizontalMerge == CellMerge.Previous ||
cell.CellFormat.VerticalMerge == CellMerge.Previous)
{
// Remove content from the cell.
cell.RemoveAllChildren();
}
}
}

Everything get converted properly, but then when I try to take the DOCX document and render it with a ReportViewer control, I loose almost all of my formatting in the table (see attached screenshot.png), and get an error message with the nested list. The code I’m using to get the docx into the report viewer is as follows:

static byte[] RenderStatement(string text, ref ContentType cType)
{
byte[] renderContent;
cType = ContentType.None;

if (text.IsWordDoc())
{
//Rich text, generate HTML
ReportViewer rptViewer = new ReportViewer();
rptViewer.ServerReport.ReportServerUrl = new Uri(
ConfigurationManager.AppSettings[String.Format("{0}.ActuarialReports.sqlreportserver.reportserverurl",
ConfigurationManager.AppSettings[“server.environment”])]);
rptViewer.ServerReport.ReportServerCredentials = new ReportServerCredentials();

//Load report definition
string fpath = Path.Combine(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath,
@“ReportDefinition\WordDoc.rdl”);
XDocument report = XDocument.Load(fpath);
RDLBuilder rbd = new RDLBuilder(report);

using (WordprocessingDocument wdoc = text.XDeSerialize())
{
rbd.InsertDocument(wdoc.MainDocumentPart.Document.Body);
MemoryStream ms = report.GetReportDefinition();

try
{
rptViewer.ServerReport.LoadReportDefinition(ms);
renderContent = rptViewer.RenderHTML();
//Success, set content type as HTML
cType = ContentType.HTML;
}
catch (ReportServerException ex)
{
renderContent = new ASCIIEncoding().GetBytes(String.Format(“Content rendering error: {0}”,
ex.Message));
}
}
}
else
{
renderContent = new ASCIIEncoding().GetBytes(text);
cType = ContentType.Text;
}
return renderContent;
}

Is there anything I can do with the Aspose.Words library to make it more compatible with the ReportViewer control?

Hi

Thanks for your request. I do not thin that the problem is in Aspose.Words. I suppose ReportViewer renders is quite restricted. Have you tried converting your HTML to DOCX using MS Word and then render it in ReportViewver? Is it rendered properly?

Also, if I understand your requirements properly, you are using ReportViewver to show document preview. Right? If so, you can consider using SWF to achieve this. Aspose.Words allows converting documents to SWF files. SWF files produced by Aspose.Words alredy contains a simple document viewer UI.

Best regards,