Generally, there is a need to create a document based on some template document dynamically using c# , hence, the search on internet starts for an optimum solution.So here you are, at the right place where we cater all appropriate and feasible solutions to your queries.Probing towards the technical part :
Source Code for Project can be downloaded at Document Generator Source Code
1) There are number of ways to perform this task but here we will be using Open XML for creating and editing documents in C#, OpenXML is widely used for creating/updating Office documents. It has a predefined structure of XML for document.
2) To work with Open XML you have to download Open XML SDK , you can download the same from http://www.microsoft.com/en-in/download/details.aspx?id=5124
3) You will need 1 more dll i.e "WindowsBased.dll" which you can download from internet or you will find in DLL folder in zipped source code which is attached along this blog.
4) You can download the zip source code in which Resume based on given template placed inside "Template" folder is created, its a window form project so you can provide custom data for candidate using form and once you clicked on "Genereate Docx" new document with form values will be created and placed inside "New Documents" folder with any name you want to specify.
5) Now I am assuming you have downloaded the SDK tool kit then , you can add reference to "DocumentFormat.OpenXml.dll" which you will find in "C:\Program Files (x86)\Open XML SDK\V2.0\lib" if you have 64-bit machine else in "C:\Program Files\Open XML SDK\V2.0\lib" if you have 32-bit machine.
6) Lets Start with actual code description which is provided in zip, start visual studio and create Window Form Project. Now drag Label for "Resume Generator" as heading and 3 more labels for Candidate Name, Qualification and Experience and now drag 3 textboxes for values which is inserted by user. Click on textbox and select properties and rename textbox from default "TextBox1" to an appropriate name I have used "nameTxt", "qualfTxt","expTxt" for name,qualification and experience respectively. Now place a button and change its text to "Generate Docx" and double click on that button then an automatic event handler for the button will be created in "Form1.cs".
7) I have used a separate class to generate resume, class is "WordDocGenerator.cs" , this class will have 3 properties i.e CandidateName, Qualification and Experience and one public method i.e "CreateResume" which will be called by main Form1.cs class.You need to use given namespaces in this class
using System.IO;
using System.Diagnostics;
using DocumentFormat.OpenXml.Packaging;
using System.Text.RegularExpressions;
8) In Form1.cs class you set propertied for WordDocGenerator.cs class with textbox values which user have entered and call CreateResume with 2 parameters i,e targetFilePath (path where predefined template for document is placed) and newDocPath (where new document with values is to be placed).
9) In CreateResume method first we copy the original document at new document location and then fill a dictionay with key/value pair in which key specifies the word which is to be replace in new document and value specifies the word which will come in place of key words for example, in given template document we have to replace the word "CandidateName" with the actual name of candidate which is provided using Name textbox so key here will be "CandidateName" and value will be "nameTxt.Text"(value of Name textbox) , in our case we will need 3 key/value pairs for name,qualification and experience. Dictionary in code is created using "FillDictionary" method.
10) Now you have created dictionary and new document, all need to be done now is replace predefined words with user specified values, hence, for that I have used Rgex Expressions for that. First simply new document is opened using WordProcessingDocument Class in "DocumentFormat.OpenXml.packaging" namespace. Then simply read whole file in stream and replace dictionary values in stream and then write the modified stream back to new document and here you go with new Document with customized values.
In case of any doubts or query you all are welcomed to comment and ask anything.
Happy Coding !!!
Source Code for Project can be downloaded at Document Generator Source Code
1) There are number of ways to perform this task but here we will be using Open XML for creating and editing documents in C#, OpenXML is widely used for creating/updating Office documents. It has a predefined structure of XML for document.
2) To work with Open XML you have to download Open XML SDK , you can download the same from http://www.microsoft.com/en-in/download/details.aspx?id=5124
3) You will need 1 more dll i.e "WindowsBased.dll" which you can download from internet or you will find in DLL folder in zipped source code which is attached along this blog.
4) You can download the zip source code in which Resume based on given template placed inside "Template" folder is created, its a window form project so you can provide custom data for candidate using form and once you clicked on "Genereate Docx" new document with form values will be created and placed inside "New Documents" folder with any name you want to specify.
5) Now I am assuming you have downloaded the SDK tool kit then , you can add reference to "DocumentFormat.OpenXml.dll" which you will find in "C:\Program Files (x86)\Open XML SDK\V2.0\lib" if you have 64-bit machine else in "C:\Program Files\Open XML SDK\V2.0\lib" if you have 32-bit machine.
6) Lets Start with actual code description which is provided in zip, start visual studio and create Window Form Project. Now drag Label for "Resume Generator" as heading and 3 more labels for Candidate Name, Qualification and Experience and now drag 3 textboxes for values which is inserted by user. Click on textbox and select properties and rename textbox from default "TextBox1" to an appropriate name I have used "nameTxt", "qualfTxt","expTxt" for name,qualification and experience respectively. Now place a button and change its text to "Generate Docx" and double click on that button then an automatic event handler for the button will be created in "Form1.cs".
7) I have used a separate class to generate resume, class is "WordDocGenerator.cs" , this class will have 3 properties i.e CandidateName, Qualification and Experience and one public method i.e "CreateResume" which will be called by main Form1.cs class.You need to use given namespaces in this class
using System.IO;
using System.Diagnostics;
using DocumentFormat.OpenXml.Packaging;
using System.Text.RegularExpressions;
8) In Form1.cs class you set propertied for WordDocGenerator.cs class with textbox values which user have entered and call CreateResume with 2 parameters i,e targetFilePath (path where predefined template for document is placed) and newDocPath (where new document with values is to be placed).
9) In CreateResume method first we copy the original document at new document location and then fill a dictionay with key/value pair in which key specifies the word which is to be replace in new document and value specifies the word which will come in place of key words for example, in given template document we have to replace the word "CandidateName" with the actual name of candidate which is provided using Name textbox so key here will be "CandidateName" and value will be "nameTxt.Text"(value of Name textbox) , in our case we will need 3 key/value pairs for name,qualification and experience. Dictionary in code is created using "FillDictionary" method.
10) Now you have created dictionary and new document, all need to be done now is replace predefined words with user specified values, hence, for that I have used Rgex Expressions for that. First simply new document is opened using WordProcessingDocument Class in "DocumentFormat.OpenXml.packaging" namespace. Then simply read whole file in stream and replace dictionary values in stream and then write the modified stream back to new document and here you go with new Document with customized values.
In case of any doubts or query you all are welcomed to comment and ask anything.
Happy Coding !!!
No comments:
Post a Comment