// Project Add References -.NET- then Microsoft.Office.Interop.Word. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using Spire.Doc; namespace CreateWordFromWinForm public partial class Form1 : Form //sample file path string samplepath = Application.StartupPath + Path.DirectorySeparatorChar + "Template.docx"; //result docs paths string docxpath = Application.StartupPath + Path.DirectorySeparatorChar + "Result.docx"; string pdfpath = Application.StartupPath + Path.DirectorySeparatorChar + "Result.pdf"; //word document object Document document = null; public Form1() InitializeComponent(); private void btnsubmit_click(object sender, EventArgs e) //initialize word object
document = new Document(); document.loadfromfile(samplepath); //get strings to replace Dictionary<string, string> dictreplace = GetReplaceDictionary(); //Replace text kvp : KeyValurPair foreach (KeyValuePair<string, string> kvp in dictreplace) document.replace(kvp.key, kvp.value, true, true); //Save doc file. document.savetofile(docxpath, FileFormat.Docx); //Convert to PDF document.savetofile(pdfpath, FileFormat.PDF); MessageBox.Show("All tasks are finished.", "doc processing", MessageBoxButtons.OK, MessageBoxIcon.Information); document.close(); private void btnexit_click(object sender, EventArgs e) this.close(); private void btnviewdoc_click(object sender, EventArgs e) ToViewFile(docxPath); private void btnviewpdf_click(object sender, EventArgs e) ToViewFile(pdfPath);
private void ToViewFile(string filename) try System.Diagnostics.Process.Start(fileName); catch Dictionary<string, string> GetReplaceDictionary() Dictionary<string, string> replacedict = new Dictionary<string, string>(); replacedict.add("#name#", txtname.text.trim()); replacedict.add("#age#",txtage.text); replacedict.add("#address#", txtaddress.text.trim()); replacedict.add("#phonenumber#",txtphonenumber.text); replacedict.add("#emailaddress#",txtemailaddress.text); replacedict.add("#experience#", txtexperience.text.trim()); replacedict.add("#position#", txtposition.text.trim()); replacedict.add("#salary#", txtsalary.text); replacedict.add("#applydate#",datetimepicker.text); string isemployed= this.radio_isemployed_yes.checked? "Yes" : "No"; replacedict.add("#isemployed#", isemployed); replacedict.add("#education#", txteducation.text.trim()); return replacedict; private void txtage_keypress(object sender, KeyPressEventArgs e) if (!char.isnumber(e.keychar)) e.handled = e.keychar!= (char)keys.back;
private void txtsalary_keypress(object sender, KeyPressEventArgs e) if (!char.isnumber(e.keychar)) e.handled = e.keychar!= (char)keys.back; private void txtphonenumber_keypress(object sender, KeyPressEventArgs e) if (!char.isnumber(e.keychar) && e.keychar!= '-' ) e.handled = e.keychar!= (char)keys.back;
C# Dictionary Generic Kullanımı Dictionary sınıfı içerisinde belirleyeceğiniz bir anahtara uygun bir değeri saklayabilecek listeler oluşturmaya yarar. HashTable ile de bunu yapabilirsiniz ama Dictionary sınıfında anahtar türünüzü belirtmek zorundasınız. Dictionary nesnesi oluştururken gireceğiniz anahtar türü object seçerseniz Hashtable ile aynı görevi yapacaktır. Gelelim Dictionary kullanımına. Anahtarımız isim, değerimiz ise bu isme uygun değer olsun. Değerleri girdikten sonra bunları foreach içerisinde konsola yazdırmak için KeyValuePair<key,value> şeklinde türü belirtmemiz gerekir. En can alıcı yer burası buna dikkat edelim: Bu veri yapısında oluşturduğumuz her Key benzersiz olmalıdır. Her anahtara karşılıkta bir değer girebilir, anahtarımız ile o değeri çağırabiliriz. Dictionary kullanımında stack yada queue lerde olduğu gibi belli bir sıralama yapısı yoktur. Burada parçalar anahtara göre verilen değerlerle tutulur. Dictionary yapısını özgürce kullanmak istersek, kullanmamız gereken yapı Hashtable dır. Dictionary kullanırken anahtar türlerini belirtmek durumundayken, Hashtable larda böyle bir kısıtlama yoktur. Trim. This method eliminates leading and trailing whitespace. We need to remove whitespace from the beginning or ending of a string. We use the Trim method to do this efficiently. Process.Start metodu uygulamamızda istenildiğinde başka uygulamaları (kullanılan bilgisayarda olan uygulamarı) açmamıza olanak sağlayan bir metottur. Bu metod ile pratik bir şekilde Excel, Word ve Internet Explorer gibi uygulamaları açmamız mümkün olmaktadır. Bir örnek ile açıklayayım. System.Diagnostics.Process.Start("Winword"); System.Diagnostics.Process.Start("Excel"); System.Diagnostics.Process.Start("IExplore.exe");