locked
How to create dynamic documents using .net RRS feed

  • Question

  • User1428336426 posted

    Dear friends,

    I have got  a  project where i am supposed to generate word document or pdf document with images, paragraphs, and lots of formatting for each document. These document should be generated on run time so the actual layouting of the document will be saved somewhere in the xslt or other format on database(Suggestion required on how to do) and xml will pass the necessary data to the placeholders and it will generate the document.

    Please help  me on how we can do this in .net C#

    regards
    Rima Gandhi.

    Thursday, June 20, 2013 3:55 AM

Answers

  • User260886948 posted

    Hi,

    The following tells how to dynamically generating word 2007 documents using .net:

    Step 1 – Create a Word 2007 template.

    Create a simple Word 2007 file with placeholders enclosed between two #’s (like #ORDERID#) and save it as ‘MyTemplate.docx
    Below is the content I used:

    From: My Company
    #ADDRESS#
    #CITY#, #REGION#, #POSTCODE#
    #COUNTRY#
    Dear #CUSTOMERID#
    We're pleased to inform you that your order number #ORDERID# is going to be shipped on #SHIPPEDDATE#.
    If you have any questions or comments, please feel free to give us a call at #HOMEPHONE#
    Regards,
    #FIRSRNAME# #LASTNAME#
    #TITLE#

    We will replace the place holders with values from the Orders table of the Northwind database.
    Step 2 – Create an OrderInformation class
    This class simply represents an Order object with properties for OrderID, CustomerID,….
    Step 3 – Create a GenerateDocument class
    A main method, public void GenerateWord( OrderInformation orderInfo), is implemented to generate the new document named after the value of OrderID of the current Order.
    In this class, System.IO.Package is used to replicate the template, replacing the place holder variables in the /word/document.xml file with the actual values from the current Order.
    Below is the code:
    public void GenerateWord( OrderInformation orderInfo)
    {
    string templateDoc = string.Format("{0}{1}", ConfigurationManager.AppSettings["TemplateFolder"],ConfigurationManager.AppSettings["TemplateFile"]);
    string filename = string.Format("{0}{1}.docx", ConfigurationManager.AppSettings["OutputFolder"], orderInfo.OrderID);

    // Copy a new file name from template file
    File.Copy(templateDoc, filename, true);

    // Open the new Package
    Package pkg = Package.Open(filenameFileMode.OpenFileAccess.ReadWrite);

    // Specify the URI of the part to be read
    Uri uri = new Uri("/word/document.xml", UriKind.Relative);
    PackagePart part = pkg.GetPart(uri);

    XmlDocument xmlMainXMLDoc = new XmlDocument();
    xmlMainXMLDoc.Load(part.GetStream(FileMode.Open,FileAccess.Read));

    xmlMainXMLDoc.InnerXml = ReplacePlaceHoldersInTemplate(orderInfo, xmlMainXMLDoc.InnerXml);

    // Open the stream to write document
    StreamWriter partWrt = new StreamWriter(part.GetStream(FileMode.OpenFileAccess.Write));
    //doc.Save(partWrt);
    xmlMainXMLDoc.Save(partWrt);

    partWrt.Flush();
    partWrt.Close();
    pkg.Close();
    }
    private string ReplacePlaceHoldersInTemplate(OrderInformation orderInfo, string templateBody)
    {
    //#ADDRESS#
    templateBody = templateBody.Replace("#ADDRESS#", orderInfo.FirstName);

    //#CITY#
    templateBody = templateBody.Replace("#CITY#", orderInfo.City);

    }
    Step 4 – Implement a web or WinForms application utilizing the above classes
    Here is a sample button_Click handler:
    protected void btnFrom_Click(object sender, EventArgs e)
    {
    string orderID = “1234”;// lstOrders.SelectedValue;

    //generate documet
    OrderInformation orderInfo;
    GenerateDocument doc = new GenerateDocument();
    orderInfo = doc.GetOrderInformation(orderID);
    doc.GenerateWord(orderInfo);

    }

    For more information, please try to refer to:

    http://geekswithblogs.net/DanBedassa/archive/2009/01/16/dynamically-generating-word-2007-.docx-documents-using-.net.aspx .

    http://www.codeproject.com/Articles/13966/ASP-NET-generating-Dynamic-word-Documents .

    Hope it can help you.

    Best Regards,
    Amy Peng 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 1, 2013 1:43 AM

All replies

  • User-1338060929 posted

    Hi

    can you provide more details actually what want you do ?

     

    Thursday, June 20, 2013 7:36 AM
  • User-1185172246 posted

    Have you considered using RDCL reports to create your documents? Then you can use the same technology to convert the rdlc to Word or PDF (all built-in Visual Studio)

    Thursday, June 20, 2013 5:51 PM
  • User1428336426 posted

    Can you give me some sample code and examples of it??

    Friday, June 21, 2013 1:14 AM
  • User260886948 posted

    Hi,

    The following tells how to dynamically generating word 2007 documents using .net:

    Step 1 – Create a Word 2007 template.

    Create a simple Word 2007 file with placeholders enclosed between two #’s (like #ORDERID#) and save it as ‘MyTemplate.docx
    Below is the content I used:

    From: My Company
    #ADDRESS#
    #CITY#, #REGION#, #POSTCODE#
    #COUNTRY#
    Dear #CUSTOMERID#
    We're pleased to inform you that your order number #ORDERID# is going to be shipped on #SHIPPEDDATE#.
    If you have any questions or comments, please feel free to give us a call at #HOMEPHONE#
    Regards,
    #FIRSRNAME# #LASTNAME#
    #TITLE#

    We will replace the place holders with values from the Orders table of the Northwind database.
    Step 2 – Create an OrderInformation class
    This class simply represents an Order object with properties for OrderID, CustomerID,….
    Step 3 – Create a GenerateDocument class
    A main method, public void GenerateWord( OrderInformation orderInfo), is implemented to generate the new document named after the value of OrderID of the current Order.
    In this class, System.IO.Package is used to replicate the template, replacing the place holder variables in the /word/document.xml file with the actual values from the current Order.
    Below is the code:
    public void GenerateWord( OrderInformation orderInfo)
    {
    string templateDoc = string.Format("{0}{1}", ConfigurationManager.AppSettings["TemplateFolder"],ConfigurationManager.AppSettings["TemplateFile"]);
    string filename = string.Format("{0}{1}.docx", ConfigurationManager.AppSettings["OutputFolder"], orderInfo.OrderID);

    // Copy a new file name from template file
    File.Copy(templateDoc, filename, true);

    // Open the new Package
    Package pkg = Package.Open(filenameFileMode.OpenFileAccess.ReadWrite);

    // Specify the URI of the part to be read
    Uri uri = new Uri("/word/document.xml", UriKind.Relative);
    PackagePart part = pkg.GetPart(uri);

    XmlDocument xmlMainXMLDoc = new XmlDocument();
    xmlMainXMLDoc.Load(part.GetStream(FileMode.Open,FileAccess.Read));

    xmlMainXMLDoc.InnerXml = ReplacePlaceHoldersInTemplate(orderInfo, xmlMainXMLDoc.InnerXml);

    // Open the stream to write document
    StreamWriter partWrt = new StreamWriter(part.GetStream(FileMode.OpenFileAccess.Write));
    //doc.Save(partWrt);
    xmlMainXMLDoc.Save(partWrt);

    partWrt.Flush();
    partWrt.Close();
    pkg.Close();
    }
    private string ReplacePlaceHoldersInTemplate(OrderInformation orderInfo, string templateBody)
    {
    //#ADDRESS#
    templateBody = templateBody.Replace("#ADDRESS#", orderInfo.FirstName);

    //#CITY#
    templateBody = templateBody.Replace("#CITY#", orderInfo.City);

    }
    Step 4 – Implement a web or WinForms application utilizing the above classes
    Here is a sample button_Click handler:
    protected void btnFrom_Click(object sender, EventArgs e)
    {
    string orderID = “1234”;// lstOrders.SelectedValue;

    //generate documet
    OrderInformation orderInfo;
    GenerateDocument doc = new GenerateDocument();
    orderInfo = doc.GetOrderInformation(orderID);
    doc.GenerateWord(orderInfo);

    }

    For more information, please try to refer to:

    http://geekswithblogs.net/DanBedassa/archive/2009/01/16/dynamically-generating-word-2007-.docx-documents-using-.net.aspx .

    http://www.codeproject.com/Articles/13966/ASP-NET-generating-Dynamic-word-Documents .

    Hope it can help you.

    Best Regards,
    Amy Peng 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 1, 2013 1:43 AM
  • User1561229253 posted

    Hi Rima, I would suggest you to also take a look at Docentric Toolkit. I think this is exactly what you are looking for, but know this is a commercial product.

    Monday, July 29, 2013 2:22 PM