1. Turn your word document needs to be replaced, adding "bookmark" in the follow-up where you want to replace field values.
2. Place the template document files stored in the Debug program.
3. Generate button click event code file:
String the templatePath Application.StartupPath + = " \\ template the .docx " ; // document templates physical path to the Document DOC = new new the Document (the templatePath); the try { the Hashtable Tables = new new the Hashtable (); tables.Add ( " A type " , aJ ); tables.Add ( " A value " , aF); tables.Add ( " B-type " , bJ); tables.Add ( " B monovalent" , BF); tables.Add ( " F. Type " , of fJ); tables.Add ( " F. Valence " , fF);
// Image insertion // Aspose.Words.DocumentBuilder builder1 = new new Aspose.Words.DocumentBuilder (DOC ); /// / Response.ContentType = pictureBox1.ImageLocation; // set the output file type // builder1.MoveToBookmark ( "Photo moderators"); // builder1.InsertImage ((byte []) GetPictureData (pictureBox1.ImageLocation ), 60, 30); tables.Add ( " date " , dtpBaogao.Value.ToString ( "yyyy Year MM Month dd Day ")); Tables.Add ( " Date 2 " , dtpJieshou.Value.ToString ( " yyyy.MM.dd " )); GetHTFile (DOC, Tables); String downname txtName.Text + = " ---- Report .pdf " ; // doc.Save (downname, SaveFormat.Pdf); SaveFileDialog the SaveData = new new SaveFileDialog (); // to save a file to open SaveData.RestoreDirectory = to true ; // open the directory for uploading stored SaveData.Title= " Select the path " ; // title // SaveData.InitialDirectory = @ "C: \"; // default path is C: \, you can change SaveData.Filter = " PDF files (* .pdf) | * .pdf " ; // can only be saved as sql file (can be changed on demand) String Script = " " ; SaveData.FileName = txtName.Text + " ---- report " ; IF (SaveData.ShowDialog () == DialogResult. OK) //If the selected path presses the save button { Script = SaveData.FileName; // Script assigned to the stored path selection doc.Save (Script, SaveFormat.Pdf); // file to generate word, it will change .word SaveFormat.Pdf MessageBox.Show ( " generation success! " );
the this .Close (); } } the catch (Exception EX) { }
}
public byte [] GetPictureData ( String ImagePath) { / ** / /// / open file stream used according to the path of the image file, and save the byte [] the FileStream FS = new new the FileStream (ImagePath, FileMode.Open); // can other overloads byte [] = byData new new byte [fs.Length]; fs.Read (byData, 0 , byData.Length); fs.Close (); return byData; } public static void GetHTFile (the Document DOC, the Hashtable Table) { BookmarkCollection Bookmarks = doc.Range.Bookmarks; the foreach (Mark Bookmarkin bookmarks) { if (table.ContainsKey(mark.Name)) { mark.Text = table[mark.Name].ToString(); } } }
Note: you need to reference Aspose.Words.dll