Mail Merge เทมเพลต

เป็นเรื่องธรรมดาที่จะใช้แม่แบบผสานเป็นเอกสารฐานสำหรับ Mail Merge การดำเนินงานอย่างใดอย่างหนึ่งถ้ามันเป็นเรื่องง่าย Mail Merge หรือ Mail Merge กับภูมิภาค Mail merge กับภูมิภาคที่มีประสิทธิภาพมากขึ้นและเป็นที่นิยมกว่าที่เรียบง่าย mail merge. ง่าย Mail Merge ถือเป็นกรณีเฉพาะของ Mail Merge กับภูมิภาคที่ภูมิภาคเป็นเอกสารทั้งหมด ทั้งหมดจะมีการอธิบายไว้ในบทความถัดไป"ประเภทของ Mail Merge การดำเนินงาน"ในรายละเอียดเพิ่มเติม.

แม่แบบเพื่อให้แน่ใจว่าข้อความในเอกสารที่ผสานเอาต์พุตจะถูกจัดรูปแบบอย่างถูกต้องและ Mail Merge การดำเนินการรับประกันว่าข้อความจากแหล่งข้อมูลที่ถูกป้อนอย่างถูกต้องในแม่แบบผสาน.

Aspose.Words ให้ความสามารถในการสร้าง Mail Merge แม่แบบเพื่อกำหนดเนื้อหาคงที่และจากนั้นสร้างเอกสารผสานโดยใช้เขตข้อมูลผสาน ดังนั้นแม่แบบผสานจะมีข้อความที่จำเป็นซึ่งเป็นเหมือนกันในทุกเอกสารที่ส่งออกและเขตข้ ข้อมูลจากแหล่งข้อมูลที่ระบุจะถูกเพิ่มลงในเทมเพลตผสานผ่านฟิลด์เหล่านี้ในระหว่างการ.

อะไรคือสิ่งที่ Mail Merge เทมเพลต

A Mail Merge แม่แบบเป็นเอกสารส่วนบุคคลที่ประกอบด้วยข้อมูลคงที่และเขตข้อมูลที่ผสานที่คุณต้องการใ แม่แบบผสานสามารถอยู่ในรูปแบบใดๆที่สนับสนุนฟิลด์เช่น, DOC, DOCX, DOT, DOTX, RTF. นอกจากนี้คุณยังสามารถใช้ mustache แม่แบบที่อธิบายไว้ในบทความ"Mustache ไวยากรณ์แม่แบบ"ในรายละเอียดเพิ่มเติม.

คุณสามารถสร้างเทมเพลตผสานให้เป็นแบบจำลองสำหรับเอกสารใหม่และควรมีข้อความห การเพิ่มฟิลด์ผสานภายในเทมเพลตจะแสดงข้อมูลการกำหนดค่าส่วนบุคคลเช่นชื่อหรือที่อยู่ A Mail Merge การดำเนินการจะแทรกข้อมูลส่วนบุคคลจากแหล่งข้อมูลของคุณไปยังเอกสารแม่แบบผสา.

นอกจากนี้คุณสามารถเพิ่ม Mail Merge ภูมิภาคในเทมเพลตของคุณโดยการใส่สอง Mail Merge ฟิลด์เพื่อทำเครื่องหมายจุดเริ่มต้นและจุดสิ้นสุดของพื้นที่อีเมล บทความถัดไป"ประเภทของ Mail Merge การดำเนินงาน"อธิบายว่าในรายละเอียดมากขึ้น.

สร้าง Mail Merge เทมเพลต

คุณสามารถสร้างแม่แบบและเพิ่มเขตข้อมูลผสานที่เฉพาะเจาะจงไปยังมันที่จะถูกแทนที่ด้วยค่าจากแหล่งข้อมูลอย่างใดอย่างหนึ่งด้วยตนเองเช่นการใช้ Microsoft Word หรือโปรแกรมโดยใช้ Aspose.Words. ในบทความนี้เราจะดูที่วิธีการเขียนโปรแกรมของการสร้างแม่แบบ.

ใช้ DocumentBuilder คลาสที่จะสร้างแม่แบบผสานที่จำเป็นโดยใช้ Aspose.Words. การเขียนข้อความโฆษณา,การเขียนในนามคนอื่น,บทความ,การเขียนบทความใหม่,การเขียน InsertTextInput, InsetField และ InsertParagraph กเขา.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการสร้าง Mail Merge เทมเพลต:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
System::SharedPtr<Aspose::Words::Document> CreateMailMergeTemplate()
{
auto doc = System::MakeObject<Document>();
auto builder = System::MakeObject<DocumentBuilder>(doc);
// Insert a text input field the unique name of this field is "Hello", the other parameters define
// what type of FormField it is, the format of the text, the field result and the maximum text length (0 = no limit)
builder->InsertTextInput(u"TextInput", TextFormFieldType::Regular, u"", u"Hello", 0);
builder->InsertField(uR"(MERGEFIELD CustomerFirstName \* MERGEFORMAT)");
builder->InsertTextInput(u"TextInput1", TextFormFieldType::Regular, u"", u" ", 0);
builder->InsertField(uR"(MERGEFIELD CustomerLastName \* MERGEFORMAT)");
builder->InsertTextInput(u"TextInput1", TextFormFieldType::Regular, u"", u" , ", 0);
// Inserts a paragraph break into the document
builder->InsertParagraph();
// Insert mail body
builder->InsertTextInput(u"TextInput", TextFormFieldType::Regular, u"", u"Thanks for purchasing our ", 0);
builder->InsertField(uR"(MERGEFIELD ProductName \* MERGEFORMAT)");
builder->InsertTextInput(u"TextInput", TextFormFieldType::Regular, u"", u", please download your Invoice at ",
0);
builder->InsertField(uR"(MERGEFIELD InvoiceURL \* MERGEFORMAT)");
builder->InsertTextInput(u"TextInput", TextFormFieldType::Regular, u"",
u". If you have any questions please call ", 0);
builder->InsertField(uR"(MERGEFIELD Supportphone \* MERGEFORMAT)");
builder->InsertTextInput(u"TextInput", TextFormFieldType::Regular, u"", u", or email us at ", 0);
builder->InsertField(uR"(MERGEFIELD SupportEmail \* MERGEFORMAT)");
builder->InsertTextInput(u"TextInput", TextFormFieldType::Regular, u"", u".", 0);
builder->InsertParagraph();
// Insert mail ending
builder->InsertTextInput(u"TextInput", TextFormFieldType::Regular, u"", u"Best regards,", 0);
builder->InsertBreak(BreakType::LineBreak);
builder->InsertField(uR"(MERGEFIELD EmployeeFullname \* MERGEFORMAT)");
builder->InsertTextInput(u"TextInput1", TextFormFieldType::Regular, u"", u" ", 0);
builder->InsertField(uR"(MERGEFIELD EmployeeDepartment \* MERGEFORMAT)");
return doc;
}

รูปภาพด้านล่างแสดงเทมเพลตที่สร้างขึ้น:

mail_merge_template-aspose-words-cpp

ปรับแต่ง Mail Merge คุณสมบัติเทมเพลต

Aspose.Words ช่วยให้คุณสามารถปรับแต่งแม่แบบของคุณผ่านคุณสมบัติมากมาย การปรับแต่งแม่แบบจะอธิบายไว้ด้านล่างผ่านตัวอย่างของการปรับแต่งคุณสมบัติบางอย่า.

ปรับแต่งคุณสมบัติของภาพ

คุณสามารถระบุคุณสมบัติของรูปภาพได้โดยใช้ ImageFieldMergingArgs ชั้นเรียน.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการระบุชื่อไฟล์ภาพและขนาดภาพ:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
void HandleMergeField::ImageFieldMerging(System::SharedPtr<ImageFieldMergingArgs> args)
{
args->set_ImageFileName(u"Image.png");
args->get_ImageWidth()->set_Value(200);
args->set_ImageHeight(System::MakeObject<MergeFieldImageDimension>(200, MergeFieldImageDimensionUnit::Percent));
}

ปรับแต่งคุณสมบัติของข้อความ

คุณสามารถใช้ Text คุณสมบัติเพื่อแทรกข้อความลงในเอกสารสำหรับฟิลด์ผสานปัจจุบัน นอกจากนี้คุณสามารถเปลี่ยนการจัดรูปแบบของข้อความและย่อหน้าภายในแม่แบบของคุณ Font และ ParagraphFormat ยวข้อง คุณสามารถจัดการกับข้อความที่จะแทรกก่อนหรือหลังเขตข้อมูลผสานโดยใช้ TextBefore และ TextAfter คุณสมบัติที่รวมอยู่ใน FieldMergeField ชั้นเรียน.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแทรกกล่องกาเครื่องหมายหรือ HTML ระหว่าง Mail Merge การดำเนินงาน:

//For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
auto doc = MakeObject<Document>(MyDir + u"Mail merge destinations - Fax.docx");
// Setup mail merge event handler to do the custom work.
doc->get_MailMerge()->set_FieldMergingCallback(MakeObject<WorkingWithFields_::HandleMergeField>());
// Trim trailing and leading whitespaces mail merge values.
doc->get_MailMerge()->set_TrimWhitespaces(false);
ArrayPtr<String> fieldNames =
MakeArray<String>({u"RecipientName", u"SenderName", u"FaxNumber", u"PhoneNumber", u"Subject", u"Body", u"Urgent", u"ForReview", u"PleaseComment"});
ArrayPtr<SharedPtr<System::Object>> fieldValues = MakeArray<SharedPtr<System::Object>>(
{System::ObjectExt::Box<String>(u"Josh"), System::ObjectExt::Box<String>(u"Jenny"), System::ObjectExt::Box<String>(u"123456789"),
System::ObjectExt::Box<String>(u""), System::ObjectExt::Box<String>(u"Hello"), System::ObjectExt::Box<String>(u"<b>HTML Body Test message 1</b>"),
System::ObjectExt::Box<bool>(true), System::ObjectExt::Box<bool>(false), System::ObjectExt::Box<bool>(true)});
doc->get_MailMerge()->Execute(fieldNames, fieldValues);
doc->Save(ArtifactsDir + u"WorkingWithFields.MailMergeFormFields.docx");

ดูเพิ่มเติม

  • สำหรับรายละเอียดเพิ่มเติมเกี่ยวกับวิธีการสร้างแม่แบบใน Microsoft Word ด้วยตนเองโปรดตรวจสอบ สร้างเทมเพลต บทความใน Microsoft เอกสารประกอบ