asp. net exports word pdf instance code to the specified template

  • 2020-06-23 00:09:00
  • OfStack


/// <summary>
        ///  export word file 
        /// </summary>
        /// <param name="templateFile"> The template path </param>
        /// <param name="fileNameWord"> Export file name </param>
        /// <param name="fileNamePdf">pdf The file name </param>
        /// <param name="bookmarks"> A collection of bookmarks in a template </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 signatures                     
                Range range = doc.Bookmarks.get_Item(ref BookMarkName).Range;// Table insertion location 
                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


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 and insert the bookmark where it needs to be replaced. Use the above method to replace the bookmark with the specified content and save as pdf


Related articles: