I am trying to load word template that has keywords to replace.
I replace this keywords and after this I save this word doc to MemoryStream and return it with FileResult in Asp.net MVC.
Problem that I countered is that if word document is in portrate mode it has no problem bu in landscape mode it loses all margins
here is my code
- Code: Select all
string path = "";
bool isLandscape = false;
if (lang == "rus")
{
isLandscape = true;
path = "~/ExcelTemplates/CustomerAgreementRus.docx";
}
else if (lang == "eng")
{
isLandscape = true;
path = "~/ExcelTemplates/CustomerAgreementEng.docx";
}
else
{
path = "~/ExcelTemplates/CustomerAgreementGeo.docx";
}
Spire.Doc.Document doc = new Spire.Doc.Document(Server.MapPath(path));
var response = _contractorService.GetContractorGeneralInfo(new GetContractorGeneralInfoRequest { ContractorId = id });
doc.Replace("[CustomerName]", response.Name, false, true);
doc.Replace("[DateToday]", DateTime.Now.ToShortDateString(), false, true);
doc.Replace("[AgreementNumber]", "1", false, true);
doc.Replace("[DirectorName]", response.Director ?? "", false, true);
doc.Replace("[AgreementNumber]", "1", false, true);
doc.Replace("[IdentificationCode]", response.IdentificationCode ?? "", false, true);
doc.Replace("[CustomerAddress]", response.LegalAddress.Address1 ?? "", false, true);
doc.Replace("[PhoneNumber]", response.PhoneNumber ?? "", false, true);
if (isLandscape)
{
//tried this too but still not working
//doc.Sections[0].PageSetup.Orientation = Spire.Doc.Documents.PageOrientation.Landscape;
//doc.Sections[0].PageSetup.Margins.Top = 40f;
//doc.Sections[0].PageSetup.Margins.Left = 40f;
}
using (MemoryStream ms2 = new MemoryStream())
{
doc.SaveToStream(ms2, Spire.Doc.FileFormat.Docx2010);
//save to byte array
byte[] toArray = ms2.ToArray();
return File(toArray, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", response.Name + "_" + DateTime.Now.ToGeorgian().ToShortDateString() + ".docx");
}