locked
Generate PDF file from Word Template RRS feed

Answers

  • User665608656 posted

    Hi md_refay,

    md_refay

    Kindly i want to know how to generate PDF file from Word Template with data from database

    I follow steps https://www.c-sharpcorner.com/UploadFile/38268a/create-word-document-from-win-form-with-users-data/

    But what if i have repeated data and i want to display it inside table

    If you want to show table in your word through Spire.Doc, first, do you want to replace a table or just to show a table?

    If you want to replace some data, I recommend you can delete the original table , then create a new table which data is from database.

    md_refay

    also how can i replace value in word footer

    For this issue, the same method, you can try to delete the original footer in your word , then create new footer to your word by using Spire.Doc.

    For these functions, you can refer to the following code, i have made a simple example :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Spire.Doc;
    using Spire.Doc.Fields;
    using System.Drawing;
    using Spire.Doc.Documents;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Data;

    Here is the code:

                //load your word
                Document doc = new Document();
                doc.LoadFromFile(@"C:\Users\yongqy\Desktop\WordTable.docx");
                Section section = doc.Sections[0];
    
                //delete footer
                section.PageSetup.DifferentFirstPageHeaderFooter = true;
                section.HeadersFooters.FirstPageFooter.Paragraphs.Clear();
    
    // create new footer HeaderFooter footer = section.HeadersFooters.Footer; //Create a paragraph as the Footer. Paragraph footerParagraph = footer.AddParagraph(); //Input the text in the paragraph. footerParagraph.AppendText("New Footer");// write the footer text you want //Set Horizon of the paragraph. footerParagraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center; //Put the paragraph into the Footer. section.HeadersFooters.FirstPageFooter.Paragraphs.Add(footerParagraph); //remove table section.Tables.RemoveAt(0); //create new table Table table = section.AddTable(true); //get data from database SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); con.Open(); SqlDataAdapter adpbp = new SqlDataAdapter("select * from Employee ", con); DataSet dsbp = new DataSet(); adpbp.Fill(dsbp); DataTable dt = dsbp.Tables[0];
    //Add Cells table.ResetCells(dt.Rows.Count + 1, dt.Columns.Count); //Header Row TableRow FRow = table.Rows[0]; FRow.IsHeader = true; //Row Height FRow.Height = 23; //Header Format FRow.RowFormat.BackColor = Color.AliceBlue; for (int i = 0; i < dt.Columns.Count; i++) { //Cell Alignment Paragraph p = FRow.Cells[i].AddParagraph(); FRow.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle; p.Format.HorizontalAlignment = HorizontalAlignment.Center; //Data Format TextRange TR = p.AppendText(dt.Columns[i].ColumnName.ToString()); TR.CharacterFormat.FontName = "Calibri"; TR.CharacterFormat.FontSize = 14; TR.CharacterFormat.TextColor = Color.Teal; TR.CharacterFormat.Bold = true; } //Data Row for (int r = 0; r < dt.Rows.Count; r++) { TableRow DataRow = table.Rows[r + 1]; //Row Height DataRow.Height = 20; //C Represents Column. for (int c = 0; c < dt.Rows[r].ItemArray.Count(); c++) { //Cell Alignment DataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle; //Fill Data in Rows Paragraph p2 = DataRow.Cells[c].AddParagraph(); TextRange TR2 = p2.AppendText(dt.Rows[r][c].ToString()); //Format Cells p2.Format.HorizontalAlignment = HorizontalAlignment.Center; TR2.CharacterFormat.FontName = "Calibri"; TR2.CharacterFormat.FontSize = 12; TR2.CharacterFormat.TextColor = Color.Brown; } }
    //Save and Launch doc.SaveToFile(@"C:\Users\yongqy\Desktop\WordTable.docx", FileFormat.Docx2013);
    //Convert to PDF 
    doc.SaveToFile(@"C:\Users\yongqy\Desktop\WordTable.pdf", FileFormat.PDF); System.Diagnostics.Process.Start(@"C:\Users\yongqy\Desktop\WordTable.pdf");

    You can also refer to this website for more details about Spire.Doc.

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 25, 2019 8:08 AM