Copy Code code as follows:
<summary>
Export Word files
</summary>
<param name= "templatefile" > Template path </param>
<param name= "Filenameword" > Export file name </param>
<param name= "filenamepdf" >pdf file name </param>
<param name= "bookmarks" > Template Bookmarks Collection </param>
<param name= "Invoiceline" > Invoice Entry List </param>
public static void Generateword (String templatefile, String Filenameword, String filenamepdf, Dictionary<string, string> bookmarks, list<invoicelineview> invoiceline)
{
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application ();
File.Copy (TemplateFile, Filenameword, true);
Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document ();
Object obj_filename = Filenameword;
Object Visible = false;
Object ReadOnly = false;
Object missing = System.Reflection.Missing.Value;
doc = App. Documents.Open (ref obj_filename, ref missing, ref ReadOnly, ref missing, ref missing, ref missing, ref missing,
Ref missing, ref missing, ref missing, ref missing, ref Visible, ref missing, ref missing, ref missing, ref missing);
Doc. Activate ();
foreach (String bookmarkname in bookmarks. Keys)
{
Object bookmarkname = bookmarkname;//Get book signature
Range range = Doc. Bookmarks.get_item (ref BookmarkName). range;//Table Insertion Position
Range. Text = Bookmarks[bookmarkname];
}
Object Issave = true;
Object FileName = filenamepdf;
Object FileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
Object lockcomments = false;
Object addtorecentfiles = true;
Object readonlyrecommended = false;
Object embedtruetypefonts = false;
Object SaveNativePictureFormat = true;
Object saveformsdata = false;
Object saveasaoceletter = false;
Object Encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingSimplifiedChineseGB18030;
Object insertlinebreaks = false;
Object allowsubstitutions = false;
Object lineending = Microsoft.Office.Interop.Word.WdLineEndingType.wdCRLF;
Object addbidimarks = false;
Doc. SaveAs (ref FileName, ref FileFormat, ref lockcomments,
Ref missing, ref addtorecentfiles, ref missing,
Ref readonlyrecommended, ref embedtruetypefonts,
Ref SaveNativePictureFormat, ref saveformsdata,
Ref saveasaoceletter, ref Encoding, ref insertlinebreaks,
Ref allowsubstitutions, ref lineending, ref addbidimarks);
Doc. Close (ref issave, ref missing, ref missing);
}
Call
Copy Code code as follows:
dictionary<string, string> bookmarks = new dictionary<string, string> ();
Bookmarks. ADD ("Contractduedatetime", invoice. Invoicetime.adddays (invoice. Contractduedate). ToString ("D"));
Bookmarks. ADD ("Customcontactemail", invoice. Customcontactemail);
Bookmarks. ADD ("Customcontactname", invoice. Customcontactname);
Bookmarks. ADD ("Contractduedate", invoice. Contractduedate.tostring ());
Bookmarks. ADD ("Customcontacttel", invoice. Customcontacttel);
Bookmarks. ADD ("Customaddress", invoice. customaddress);
Bookmarks. ADD ("Invoicetime", invoice. Invoicetime.tostring ());
Bookmarks. ADD ("Invoiceid", invoice. Invoiceid);
Bookmarks. ADD ("Customname", invoice. Customname);
Bookmarks. ADD ("CustomName2", invoice. Customname);
Bookmarks. ADD ("Total", invoice. Totalprice.tostring ("C"));
Bookmarks. ADD ("Total1", invoice. Totalprice.tostring ("C"));
Bookmarks. ADD ("Totaltax", invoice. Totaltax.tostring ("C"));
Bookmarks. ADD ("Totalprice", (Invoice). Totalprice + invoice. Totaltax). ToString ("C"));
Bookmarks. ADD ("TotalPrice1", (Invoice). Totalprice + invoice. Totaltax). ToString ("C"));
Bookmarks. ADD ("TotalPrice2", (Invoice). Totalprice + invoice. Totaltax). ToString ("C"));
Bookmarks. ADD ("TotalPrice3", (Invoice). Totalprice + invoice. Totaltax). ToString ("C"));
Bookmarks. ADD ("TotalPrice4", (Invoice). Totalprice + invoice. Totaltax). ToString ("C"));
Utility.generateword (TemplateFile, Filenameword, filenamepdf, bookmarks, invoiceline);
Create a new word, insert a bookmark where you want to replace it, use the above method to replace the bookmark with the specified content, and save as a PDF