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テンプレートとは何ですか

Mail Mergeテンプレートは、固定データと、変数テキストを配置するマージされたフィールドを含むパーソナライズされた文書です。 マージテンプレートは、フィールドをサポートする任意の形式にすることができます。, DOC, DOCX, DOT, DOTX, RTF. さらに、記事"Mustacheテンプレート構文"で詳しく説明されているmustacheテンプレートを使用することもできます。

マージテンプレートを作成して新しいドキュメントのモデルにすることができ、マージされたドキュメントの各バージョンで同じである必要があるメインテ テンプレート内に差し込み項目を追加すると、データソースから取得された名前や住所などの個人用設定データが表示されます。 Mail Merge操作により、データソースからマージテンプレートドキュメントに個人用設定データが自動的に挿入されます。

さらに、テンプレートにMail Merge領域を追加するには、2つのMail Mergeフィールドを挿入して、メール領域の先頭と末尾をマークします。 次の記事"Mail Merge操作の種類"では、これについて詳しく説明しています。

Mail Mergeテンプレートを作成する

テンプレートを作成し、それに特定の差し込み項目を追加すると、Microsoft Wordを使用して手動で、またはAspose.Wordsを使用してプログラムでデータソースの値に置き換えら この記事では、テンプレートを作成するプログラム的な方法を見ていきます。

DocumentBuilderクラスを使用して、Aspose.Wordsを使用して必要なマージテンプレートを作成します。 このようなテンプレートには、テキスト、差し込み項目、および改行を含めることができます。InsertTextInput, InsertField, とInsertParagraphメソッド。

次のコード例は、Mail Mergeテンプレートを作成する方法を示しています:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
public static Document CreateMailMergeTemplate() throws Exception {
DocumentBuilder builder = new DocumentBuilder();
// 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("TextInput", TextFormFieldType.REGULAR, "", "Hello", 0);
builder.insertField("MERGEFIELD CustomerFirstName \\* MERGEFORMAT");
builder.insertTextInput("TextInput1", TextFormFieldType.REGULAR, "", " ", 0);
builder.insertField("MERGEFIELD CustomerLastName \\* MERGEFORMAT");
builder.insertTextInput("TextInput1", TextFormFieldType.REGULAR, "", " , ", 0);
// Inserts a paragraph break into the document
builder.insertParagraph();
// Insert mail body
builder.insertTextInput("TextInput", TextFormFieldType.REGULAR, "", "Thanks for purchasing our ", 0);
builder.insertField("MERGEFIELD ProductName \\* MERGEFORMAT");
builder.insertTextInput("TextInput", TextFormFieldType.REGULAR, "", ", please download your Invoice at ",
0);
builder.insertField("MERGEFIELD InvoiceURL \\* MERGEFORMAT");
builder.insertTextInput("TextInput", TextFormFieldType.REGULAR, "",
". If you have any questions please call ", 0);
builder.insertField("MERGEFIELD Supportphone \\* MERGEFORMAT");
builder.insertTextInput("TextInput", TextFormFieldType.REGULAR, "", ", or email us at ", 0);
builder.insertField("MERGEFIELD SupportEmail \\* MERGEFORMAT");
builder.insertTextInput("TextInput", TextFormFieldType.REGULAR, "", ".", 0);
builder.insertParagraph();
// Insert mail ending
builder.insertTextInput("TextInput", TextFormFieldType.REGULAR, "", "Best regards,", 0);
builder.insertBreak(BreakType.LINE_BREAK);
builder.insertField("MERGEFIELD EmployeeFullname \\* MERGEFORMAT");
builder.insertTextInput("TextInput1", TextFormFieldType.REGULAR, "", " ", 0);
builder.insertField("MERGEFIELD EmployeeDepartment \\* MERGEFORMAT");
return builder.getDocument();
}

下の図は、作成されたテンプレートを示しています:

mail_merge_template_aspose_words_java

Mail Mergeテンプレートプロパティをカスタマイズする

Aspose.Words多くのプロパティを使用してテンプレートをカスタマイズできます。 テンプレートのカスタマイズは、画像とテキストのいくつかのプロパティをカスタマイズする例を介して以下に説明されます。

画像のプロパティをカスタマイズする

ImageFieldMergingArgsクラスを使用してイメージプロパティを指定できます。 ここで説明するように、データベースからイメージを挿入できます Aspose.Words GitHub.

次のコード例は、イメージファイル名とイメージサイズを指定する方法を示しています:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception {
args.setImageFileName("Image.png");
args.setImageWidth(new MergeFieldImageDimension(200, MergeFieldImageDimensionUnit.POINT));
args.setImageHeight(new MergeFieldImageDimension(200, MergeFieldImageDimensionUnit.PERCENT));
}

テキストプロパティのカスタマイズ

[テキスト]https://reference.aspose.com/words/java/com.aspose.words/Fieldmergingargs#Text)現在の差し込み項目のドキュメントにテキストを挿入するプロパティですまた、[フロント]を使用して、テンプレート内のテキストや段階の式を変更することもできます](https://reference.aspose.com/words/java/com.aspose.words/font/)クラスとParagraphFormatクラスを使用できます。 差し込み項目の前後に挿入されるテキストは、FieldMergeFieldクラスに含まれるTextBeforeおよびTextAfterプロパティを使用して処理できます。

次のコード例は、Mail Merge操作中にチェックボックスまたはHTMLを挿入する方法を示しています:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document(getMyDir() + "Mail merge destinations - Fax.docx");
// Setup mail merge event handler to do the custom work.
doc.getMailMerge().setFieldMergingCallback(new HandleMergeField());
// Trim trailing and leading whitespaces mail merge values.
doc.getMailMerge().setTrimWhitespaces(false);
String[] fieldNames = {
"RecipientName", "SenderName", "FaxNumber", "PhoneNumber",
"Subject", "Body", "Urgent", "ForReview", "PleaseComment"
};
Object[] fieldValues = {
"Josh", "Jenny", "123456789", "", "Hello",
"<b>HTML Body Test message 1</b>", true, false, true
};
doc.getMailMerge().execute(fieldNames, fieldValues);
doc.save(getArtifactsDir() + "WorkingWithFields.MailMergeFormFields.docx");

また見て下さい

  • Microsoft Wordでテンプレートを手動で作成する方法の詳細については、以下を確認してください テンプレートを作成する Microsoftドキュメントの記事