Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Wed Jun 01, 2016 1:01 pm

I can not figure out how to replace text in title
Here [AgreementNumber] is located in Title (Header Section), but it does not change.
Also it loses formating in one document, but not in another.

Code: Select all
public virtual ActionResult AgreementDoc(int id, string lang = "geo")
{
string path = "";
if (lang == "rus")
{
    path = "~/ExcelTemplates/VendorAgreementRus.docx";
}
else if (lang == "eng")
{
    path = "~/ExcelTemplates/VendorAgreementEng.docx";
}
else
{
    path = "~/ExcelTemplates/VendorAgreementGeo.docx";
}

Spire.Doc.Document doc = new Spire.Doc.Document(Server.MapPath(path));

var response = _contractorService.GetContractorGeneralInfo(new GetContractorGeneralInfoRequest { ContractorId = id });
doc.Replace("[VendorName]", response.Name, false, true);
doc.Replace("[PaymentDay]", response.PaymentDay.GetValueOrDefault().ToString(), false, true);
doc.Replace("[DateToday]", DateTime.Now.ToShortDateString(), false, true);
doc.Replace("[AgreementNumber]", "1", false, true);

using (MemoryStream ms2 = new MemoryStream())
{
    doc.SaveToStream(ms2, Spire.Doc.FileFormat.Docx);
    //save to byte array
    byte[] toArray = ms2.ToArray();

    return File(toArray, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", response.Name + "_" + DateTime.Now.ToGeorgian().ToShortDateString() + ".docx");
}
}

xurca
 
Posts: 4
Joined: Wed Jun 01, 2016 9:23 am

Thu Jun 02, 2016 8:21 am

Hello,

Thanks for your feedback and your sharing.
The string "[AgreementNumber]" is located in StructureDocumentTag, however, the StructureDocumentTag has been bound to the Title property of the document. So, no matter how you change it, the value is always the Title value, so, you have to change the title with the following method.
Code: Select all
doc.BuiltinDocumentProperties.Title = doc.BuiltinDocumentProperties.Title.Replace("[AgreementNumber]", "1");

Regarding the issue that "it loses formating in one document, but not in another.", I test the two files with the latest version Spire.Doc Pack Version:5.7 and don't find any issue in the generated docx files. Please try the version.

Sincerely,
Caroline
E-iceblue support team
User avatar

caroline.zhang
 
Posts: 291
Joined: Mon Mar 07, 2016 9:22 am

Fri Jun 03, 2016 1:35 pm

Thanks for reply. Title is fixed now but
I am using FreeSpire.Doc and I guess thats why it loses formatting and styles in some documents

xurca
 
Posts: 4
Joined: Wed Jun 01, 2016 9:23 am

Mon Jun 06, 2016 1:34 am

Hi,

Thank you for the response.
Sorry that the issue that caused by using the free Spire.Doc brought the inconvenience to you. At present we don't plan to update these changes into the free version of Spire.Doc. I recommend you to test our pro version.

Sincerely,
Caroline
E-iceblue support team
User avatar

caroline.zhang
 
Posts: 291
Joined: Mon Mar 07, 2016 9:22 am

Return to Spire.Doc