Hi Awais,
I’ll tell you first how it is being done, first is i get a word doc which has text, form fields and tables.
string docPath = MapPath(“Vacation-Request Final.docx”);
Aspose.Words.Document myDoc= new Aspose.Words.Document(docPath);
And i have text in every cell of a table where i finds and insert the html, below is the code,
myDoc.Range.Replace(new Regex(@"{{january}}"), new ReplaceWithHtmlEvaluator(GenerateHTML(JanCalendar)), false);
myDoc.Range.Replace(new Regex(@"{{february}}"), new ReplaceWithHtmlEvaluator(GenerateHTML(FebCalendar)), false);
myDoc.Range.Replace(new Regex(@"{{march}}"), new ReplaceWithHtmlEvaluator(GenerateHTML(MarCalendar)), false);
The function GenerateHTML(MarCalendar) gets the string html from a control, parameter is a calendar control, this is how i get the INPUT HTML, see below function:
protected string GenerateHTML(Calendar cal)
{
StringWriter sw = new StringWriter();
HtmlTextWriter w = new HtmlTextWriter(sw);
cal.RenderControl(w);
string s = sw.GetStringBuilder().ToString();
s = s.Replace(“cellpadding=“2"”, “cellpadding=“0"”);
s = s.Replace(“border-width:1px;border-style:solid;border-collapse:collapse;”,
“border-width:1px;border-style:solid;border-collapse:collapse;font-size: 12px;”);
s = Regex.Replace(s, @”<a\b[^>]+>([^<](?:(?!</a)<[^<])*)”, “$1”);
return s;
}
And the class ReplaceWithHtmlEvaluator(GenerateHTML(JanCalendar)) inserts the html to the specified cell, before it was working well, it looks like what it is in the webpage. But when i add Response.End(); after saving the document, the calendar styles has been changed to default styles. Below is my code for the saving:
Aspose.Words.Saving.PdfSaveOptions pdfOptions = new Aspose.Words.Saving.PdfSaveOptions();
pdfOptions.PageIndex = 0;
pdfOptions.PageCount = 1;
pdfOptions.PreserveFormFields = true;
myDoc.Save(Response, “Vacation_Request.pdf”, Aspose.Words.ContentDisposition.Attachment, pdfOptions);
I attached here the input word document (Vacation-Request Final.docx), the pdf output using the same code before i run the RESPONSE.END(); (Vacation Request (17).pdf), and last is the pdf output now which has the undesired output (Vacation_Request (91).pdf). Please see the difference of the calendar.