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 Sep 11, 2019 11:08 am

Hello, i'm trying to fulfill document with data from my database. I gave some placeholders in word document e.g. {{customerName}}. I replace this values with document.Replace().
Now, fifth row in table should be dynamical as this property is List of objects. Please help me how to render rows in particular position in table and fill it with data.
Thank you in advance,
Danilo


using (var document = new Document())
{
var assembly = Assembly.GetAssembly(typeof(GeneratePdfQuery));
var pom = (TypeOfDocumentEnum)query.TypeOfDocument;
Stream stream = assembly.GetManifestResourceStream($"G7.Domain.StatementTemplates.{pom.ToString()}.docx");

document.LoadFromStream(stream, FileFormat.Docx2013);


foreach (PropertyInfo prop in query.GetType().GetProperties())
{
document.Replace(FormatString(prop.Name), prop.GetValue(query).ToString().Trim(), true, true);
}

using (var writeStream = new MemoryStream())
{
document.SaveToFile($"{query.FileName}.pdf", FileFormat.PDF);
document.SaveToStream(writeStream, FileFormat.PDF);
return writeStream.ToArray();
}
}

mosurovicdanilo
 
Posts: 2
Joined: Wed Sep 11, 2019 10:49 am

Thu Sep 12, 2019 10:51 am

Hi,

Thanks for your inquiry.
Please refer to the code below to add data into the particular row in table.

Code: Select all
            List<string> values = new List<string>();
            values.Add("1");
            values.Add("2");
            values.Add("3");

            //load a file which has 4 X 3 table
            Document doc = new Document(FilePath + "filename.docx");
            TextSelection ts = doc.FindString("{{customerName}}", true, true);

            //get the row which includes the placeholders
            TableRow row = ts.GetAsOneRange().OwnerParagraph.Owner.Owner as TableRow;

            for (int i = 0; i < row.Cells.Count; i++)
            {
                row.Cells[i].Paragraphs[0].Text = values[i];
            }

            doc.SaveToFile("result.docx", FileFormat.Docx);


If the code couldn’t solve your issue, to help us better investigate it, please offer us your input Word file and the List you use as well as your desired result.

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Thu Dec 26, 2019 10:44 am

Hi,

Greetings from E-iceblue.
How's your issue doing? Could you please give us some feedback at your convenience?

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Return to Spire.Doc