Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Công cụ hợp nhất lấy một tài liệu làm đầu vào, tìm kiếm các trường MERGEFIELD
trong đó và thay thế chúng bằng dữ liệu thu được từ nguồn dữ liệu. Thông thường, văn bản thuần túy và HTML được chèn, nhưng Aspose.Words người dùng cũng có thể tạo một tài liệu xử lý các tình huống bất thường hơn cho các trường Mail Merge.
Chức năng Aspose.Words mạnh mẽ cho phép bạn mở rộng quy trình Mail Merge:
Đôi khi cần phải thực hiện thao tác Mail Merge để không phải văn bản được thay thế trong trường hợp nhất, mà là trường nhập checkbox hoặc văn bản. Mặc dù đây không phải là kịch bản phổ biến nhất, nhưng nó rất tiện dụng cho một số nhiệm vụ.
Ảnh chụp màn hình sau của tài liệu Word hiển thị một mẫu với các trường hợp nhất:
Ảnh chụp màn hình này của tài liệu Word bên dưới hiển thị tài liệu đã được tạo:
Subject
đã được thay thế bằng trường nhập văn bản.
Ví dụ mã sau đây cho thấy cách chèn checkbox es và nhập các trường văn bản vào tài liệu trong mail merge:
// 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; | |
} |
Khi thực hiện thao tác Mail Merge, bạn có thể chèn hình ảnh từ cơ sở dữ liệu vào tài liệu bằng các trường hình ảnh Mail Merge đặc biệt. Trường image Mail Merge là một trường hợp nhất Có Tên Image: MyFieldName.
Trong một mail merge, khi một trường hình ảnh Mail Merge gặp phải trong một tài liệu, sự kiện FieldMergingCallback được kích hoạt. Bạn có thể trả lời sự kiện này để trả về tên tệp, luồng hoặc đối tượng hình ảnh cho công cụ Mail Merge để nó có thể được chèn vào tài liệu.
Ví dụ mã sau đây cho thấy cách chèn hình ảnh được lưu trữ trong trường BLOB cơ sở dữ liệu vào báo cáo:
// 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); | |
} | |
} |
Trong khi hợp nhất trường hợp nhất hình ảnh, đôi khi bạn có thể cần kiểm soát các thuộc tính hình ảnh khác nhau, chẳng hạn như WrapType.
Hiện tại, sử dụng ImageFieldMergingArgs, bạn chỉ có thể đặt thuộc tính chiều rộng hoặc chiều cao hình ảnh tương ứng. Để khắc phục vấn đề này, Aspose.Words cung cấp thuộc tính Shape, tạo điều kiện để có toàn quyền kiểm soát hình ảnh được chèn hoặc bất kỳ hình dạng nào khác.
Ví dụ mã sau đây cho thấy cách đặt các thuộc tính hình ảnh khác nhau:
// 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); | |
} | |
} |
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.