เทมเพลต Word หมายถึงเอกสารที่มีโครงสร้าง เนื้อหา และสไตล์ที่สร้างไว้ล่วงหน้า ผู้ใช้จำเป็นต้องเพิ่มหรือแทนที่เนื้อหาจำนวนเล็กน้อยเพื่อสร้างเอกสารของตนเอง ในบทความนี้ ฉันจะแนะนำวิธีคัดแยกเอกสาร Word จากเทมเพลต โดยทางโปรแกรมโดยการแทนที่ตัวยึดตำแหน่ง (สตริง) ด้วยสตริงหรือรูปภาพใหม่ โดยใช้ «Spire.Doc สำหรับ .สุทธิ".
ออกแบบเทมเพลตล่วงหน้าใน MS Word
ต่อไปนี้เป็นภาพหน้าจอของเทมเพลตที่สร้างโดย MS Word สตริง เช่น “#name#”, “#address#” คือตัวยึดตำแหน่งที่จะถูกแทนที่ด้วยสตริงใหม่ ตัวยึดตำแหน่ง “#photograph#” จะถูกแทนที่ด้วยรูปภาพ อย่าลืมใช้การจัดรูปแบบ (ขนาดแบบอักษร ลักษณะแบบอักษร การจัดตำแหน่ง ฯลฯ) กับตัวยึดตำแหน่งเพื่อให้เนื้อหาใหม่สามารถสืบทอดการจัดรูปแบบเหล่านี้ได้
เพิ่มข้อมูลอ้างอิง
วิธีที่ #1: ดาวน์โหลด Spire.Doc และคลายซิปแพ็คเกจที่ใดที่หนึ่งบนดิสก์ของคุณเพื่อค้นหาโฟลเดอร์ “BIN” Spire.Doc มี DLL ที่คอมไพล์สำหรับ .NET Framework หลายเวอร์ชัน รวมถึง .NET Core และแพลตฟอร์มอื่นๆ เลือก DLL จากโฟลเดอร์ที่คุณต้องการ และเพิ่มทั้งหมดเป็นการขึ้นต่อกันในโครงการของคุณ
วิธีที่ #2: สร้างแอปพลิเคชัน .NET ใน Visual Studio ของคุณ และติดตั้ง Spire.Doc โดยตรงผ่าน NuGet ตัวจัดการแพ็คเกจ NuGet จะติดตั้งเวอร์ชันที่ถูกต้องให้เหมาะกับแอปพลิเคชันของคุณโดยอัตโนมัติ
แทนที่ตัวยึดตำแหน่งในเทมเพลต Word ใน C# และ VB.NET
ขั้นตอนในการแทนที่ตัวยึดตำแหน่งในเทมเพลต Word โดยใช้ Spire.Doc สำหรับ .NET มีดังต่อไปนี้
- สร้างออบเจ็กต์เอกสาร
- โหลดเทมเพลต Word โดยใช้วิธี Document.LoadFromFile()
- รับตัวยึดตำแหน่งและสตริงใหม่จัดเก็บไว้ในพจนานุกรม
- แทนที่ตัวยึดตำแหน่งด้วยสตริงใหม่โดยใช้เมธอด Document.Replace(string matchString, string newValue, bool caseSensitive, bool wholeWord)
- แทนที่ตัวยึดตำแหน่งด้วยรูปภาพโดยใช้วิธีการที่กำหนดเอง ReplaceTextWithImage(เอกสารเอกสาร, String stringToReplace, String imagePath)
- บันทึกเอกสารเป็นไฟล์ Word โดยใช้วิธี Document.SaveToFile()
[C#]
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System; using System.Collections.Generic; using System.Drawing; namespace ReplacePlaceholdersInWord { class Program { static void Main(string[] args) { //Create a Document object Document document = new Document(); //Load the template document.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Template.docx"); //Store the old strings -"placeholders" and new strings in a dictionary Dictionary<string, string> replaceDict = new Dictionary<string, string>(); replaceDict.Add("#name#", "Kaila Smith"); replaceDict.Add("#address#", "123 Michigan Street, Bloomfield, MI94141"); replaceDict.Add("#telephone#", "537286"); replaceDict.Add("#mail#", "[email protected]"); replaceDict.Add("#nationality#", "United States"); replaceDict.Add("#birth#", "July 12th, 1992"); replaceDict.Add("#gender#", "Female"); //Replace strings with new strings foreach (KeyValuePair<string, string> kvp in replaceDict) { document.Replace(kvp.Key, kvp.Value, true, true); } //Get the image path String imagePath = "C:\\Users\\Administrator\\Desktop\\photo.jpg"; //Replace string in the template with image ReplaceTextWithImage(document, "#photograph#", imagePath); //Save doc file. document.SaveToFile("ReplacePlaceholders.docx", FileFormat.Docx); document.Close(); } //Replace string in a Word document with an image static void ReplaceTextWithImage(Document document, String stringToReplace, String imagePath) { TextSelection[] selections = document.FindAllString(stringToReplace, false, true); Image image = Image.FromFile(imagePath); int index = 0; TextRange range = null; foreach (TextSelection selection in selections) { DocPicture pic = new DocPicture(document); pic.LoadImage(image); range = selection.GetAsOneRange(); index = range.OwnerParagraph.ChildObjects.IndexOf(range); range.OwnerParagraph.ChildObjects.Insert(index, pic); range.OwnerParagraph.ChildObjects.Remove(range); } } } }
[VB.NET]
Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Imports System Imports System.Collections.Generic Imports System.Drawing Namespace ReplacePlaceholdersInWord Class Program static void Main(string() args) { 'Create a Document object Document document = New Document() 'Load the template document.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Template.docx") 'Store the old strings -"placeholders" and new strings in a dictionary Dictionary<string, string> replaceDict = New Dictionary<string, string>() replaceDict.Add("#name#", "Kaila Smith") replaceDict.Add("#address#", "123 Michigan Street, Bloomfield, MI94141") replaceDict.Add("#telephone#", "537286") replaceDict.Add("#mail#", "[email protected]") replaceDict.Add("#nationality#", "United States") replaceDict.Add("#birth#", "July 12th, 1992") replaceDict.Add("#gender#", "Female") 'Replace strings with new strings foreach (KeyValuePair<string, string> kvp in replaceDict) { document.Replace(kvp.Key, kvp.Value, True, True) } 'Get the image path String imagePath = "C:\\Users\\Administrator\\Desktop\\photo.jpg" 'Replace string in the template with image ReplaceTextWithImage(document, "#photograph#", imagePath) 'Save doc file. document.SaveToFile("ReplacePlaceholders.docx", FileFormat.Docx) document.Close() } 'Replace string in a Word document with an image static void ReplaceTextWithImage(Document document, String stringToReplace, String imagePath) { TextSelection() selections = document.FindAllString(stringToReplace, False, True) Image image = Image.FromFile(imagePath) Integer index = 0 TextRange range = Nothing foreach (TextSelection selection in selections) { DocPicture pic = New DocPicture(document) pic.LoadImage(image) range = selection.GetAsOneRange() index = range.OwnerParagraph.ChildObjects.IndexOf(range) range.OwnerParagraph.ChildObjects.Insert(index, pic) range.OwnerParagraph.ChildObjects.Remove(range) } } End Class End Namespace
เอาต์พุต