Checkboxes、テキスト入力、または画像を挿入します
マージエンジンはドキュメントを入力として受け取り、その中のMERGEFIELDフィールドを探し、データソースから取得したデータに置き換えます。 通常、プレーンテキストとHTMLが挿入されますが、Aspose.WordsユーザーはMail Mergeフィールドのより珍しいシナリオを処理する文書を生成することもできます。
強力なAspose.Words機能を使用するとMail Mergeプロセスを拡張できます:
- checkboxesおよびテキスト入力フォームフィールドをmail merge中にドキュメントに挿入します
- 任意のカスタムストレージ(ファイル、BLOBフィールドなど)から画像を挿入します。)
Mail Merge中にCheckboxesとテキスト入力を挿入します
差し込み項目でテキストが置換されるのではなく、checkboxまたはテキスト入力フィールドで置換されるように、Mail Merge操作を実行する必要がある場合があります。 これは最も一般的なシナリオではありませんが、一部のタスクでは非常に便利です。
Word文書の次のスクリーンショットは、差し込み項目を含むテンプレートを示しています:

下のWord文書のこのスクリーンショットは、すでに生成された文書を示しています:

Subjectフィールドはテキスト入力フィールドに置
次のコード例は、mail merge中にcheckboxesと入力テキストフィールドをドキュメントに挿入する方法を示しています:
| // 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"); |
| // For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
| private static class HandleMergeField implements IFieldMergingCallback | |
| { | |
| /// <summary> | |
| /// This handler is called for every mail merge field found in the document, | |
| /// for every record found in the data source. | |
| /// </summary> | |
| public void /*IFieldMergingCallback.*/fieldMerging(FieldMergingArgs e) throws Exception | |
| { | |
| if (mBuilder == null) | |
| mBuilder = new DocumentBuilder(e.getDocument()); | |
| // We decided that we want all boolean values to be output as check box form fields. | |
| if (e.getFieldValue() instanceof /*boolean*/Boolean) | |
| { | |
| // Move the "cursor" to the current merge field. | |
| mBuilder.moveToMergeField(e.getFieldName()); | |
| String checkBoxName = MessageFormat.format("{0}{1}", e.getFieldName(), e.getRecordIndex()); | |
| mBuilder.insertCheckBox(checkBoxName, (Boolean) e.getFieldValue(), 0); | |
| return; | |
| } | |
| switch (e.getFieldName()) | |
| { | |
| case "Body": | |
| mBuilder.moveToMergeField(e.getFieldName()); | |
| mBuilder.insertHtml((String) e.getFieldValue()); | |
| break; | |
| case "Subject": | |
| { | |
| mBuilder.moveToMergeField(e.getFieldName()); | |
| String textInputName = MessageFormat.format("{0}{1}", e.getFieldName(), e.getRecordIndex()); | |
| mBuilder.insertTextInput(textInputName, TextFormFieldType.REGULAR, "", (String) e.getFieldValue(), 0); | |
| break; | |
| } | |
| } | |
| } | |
| public void imageFieldMerging(ImageFieldMergingArgs args) | |
| { | |
| args.setImageFileName("Image.png"); | |
| args.getImageWidth().setValue(200.0); | |
| args.setImageHeight(new MergeFieldImageDimension(200.0, MergeFieldImageDimensionUnit.PERCENT)); | |
| } | |
| private DocumentBuilder mBuilder; | |
| } |
Mail Merge中に画像を挿入する
Mail Merge操作を実行するときは、特殊なimageMail Mergeフィールドを使用してデータベースからドキュメントにイメージを挿入できます。 ImageMail MergeフィールドはImage:MyFieldNameという名前の差し込み項目です。
データベースからの画像の挿入
Mail merge中に、ドキュメント内で画像Mail Mergeフィールドが検出されると、FieldMergingCallbackイベントが発生します。 このイベントに応答して、ファイル名、ストリーム、または画像オブジェクトをMail Mergeエンジンに返し、ドキュメントに挿入できるようにすることができます。
次のコード例は、データベースBLOBフィールドに格納されている画像をレポートに挿入する方法を示しています:
| // For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
| public void mailMergeImageFromBlob() throws Exception | |
| { | |
| Document doc = new Document(getMyDir() + "Mail merge destination - Northwind employees.docx"); | |
| doc.getMailMerge().setFieldMergingCallback(new HandleMergeImageFieldFromBlob()); | |
| Class.forName("net.ucanaccess.jdbc.UcanaccessDriver"); | |
| String connString = "jdbc:ucanaccess://" + getDatabaseDir() + "Northwind.mdb"; | |
| Connection connection = DriverManager.getConnection(connString, "Admin", ""); | |
| Statement statement = connection.createStatement(); | |
| ResultSet resultSet = statement.executeQuery("SELECT * FROM Employees"); | |
| DataTable dataTable = new DataTable(resultSet, "Employees"); | |
| IDataReader dataReader = new DataTableReader(dataTable); | |
| doc.getMailMerge().executeWithRegions(dataReader, "Employees"); | |
| connection.close(); | |
| doc.save(getArtifactsDir() + "WorkingWithFields.MailMergeImageFromBlob.docx"); | |
| } | |
| public static class HandleMergeImageFieldFromBlob implements IFieldMergingCallback | |
| { | |
| public void fieldMerging(FieldMergingArgs args) | |
| { | |
| // Do nothing. | |
| } | |
| /// <summary> | |
| /// This is called when mail merge engine encounters Image:XXX merge field in the document. | |
| /// You have a chance to return an Image object, file name, or a stream that contains the image. | |
| /// </summary> | |
| public void imageFieldMerging(ImageFieldMergingArgs e) throws Exception | |
| { | |
| // The field value is a byte array, just cast it and create a stream on it. | |
| ByteArrayInputStream imageStream = new ByteArrayInputStream((byte[]) e.getFieldValue()); | |
| // Now the mail merge engine will retrieve the image from the stream. | |
| e.setImageStream(imageStream); | |
| } | |
| } |
Mail Merge中にイメージプロパティを設定する
画像の差し込み項目をマージする際に、WrapTypeなどのさまざまな画像プロパティを制御する必要がある場合があります。
現在、ImageFieldMergingArgsを使用すると、それぞれ画像の幅または高さのプロパティのみを設定できます。 この問題を解決するために、Aspose.WordsはShapeプロパティを提供し、挿入された画像やその他の形状を完全に制御するのを容易にします。
次のコード例は、さまざまなイメージプロパティを設定する方法を示しています:
| // For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
| Document doc = new Document(); | |
| DocumentBuilder builder = new DocumentBuilder(doc); | |
| builder.writeln("{{#foreach example}}"); | |
| builder.writeln("{{Image(126pt;126pt):stempel}}"); | |
| builder.writeln("{{/foreach example}}"); | |
| doc.getMailMerge().setUseNonMergeFields(true); | |
| doc.getMailMerge().setTrimWhitespaces(true); | |
| doc.getMailMerge().setUseWholeParagraphAsRegion(false); | |
| doc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_EMPTY_TABLE_ROWS | |
| | MailMergeCleanupOptions.REMOVE_CONTAINING_FIELDS | |
| | MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS | |
| | MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS); | |
| doc.getMailMerge().setFieldMergingCallback(new ImageFieldMergingHandler()); | |
| doc.getMailMerge().executeWithRegions(new DataSourceRoot()); | |
| doc.save(getArtifactsDir() + "WorkingWithFields.MailMergeImageField.docx"); |
| // For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
| private static class ImageFieldMergingHandler implements IFieldMergingCallback | |
| { | |
| public void fieldMerging(FieldMergingArgs args) | |
| { | |
| // Implementation is not required. | |
| } | |
| public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception | |
| { | |
| Shape shape = new Shape(args.getDocument(), ShapeType.IMAGE); | |
| { | |
| shape.setWidth(126.0); shape.setHeight(126.0); shape.setWrapType(WrapType.SQUARE); | |
| } | |
| shape.getImageData().setImage(getMyDir() + "Mail merge image.png"); | |
| args.setShape(shape); | |
| } | |
| } |