วิธีการใช้ลอจิกแบบกำหนดเองกับพื้นที่ที่ไม่รวม
มีบางสถานการณ์ที่ไม่ต้องการลบพื้นที่ที่ไม่ได้รวมออกจากเอกสารในระหว่างMail Mergeหรือผลลัพธ์ในเอกสารที่ดูไม่สมบูรณ์ มื่อการขาดงานของข้อมูลการป้อนข้อมูลควรจะแสดงให้กับผู้ใช้ในรูปแบบของข้อความแทน.
บทความนี้ไม่มีการอ้างอิงจากเอกสารอ้างอิงหรือแหล่งข้อมูลโปรดช่วยพัฒนาบทความนี้โด ถ้าพื้นที่นี้ไม่ได้ใช้แล้วชื่อและตารางจะยังคงอยู่หลังจากที่ภูมิภาคจะถูกลบออกซึ่งจะมีลักษณ.
บทความนี้จัดเตรียมโซลูชันการกำหนดวิธีการจัดการพื้นที่ที่ไม่ได้ใช้ในเอกสารด้วยตนเอง รหัสฐานสำหรับการทำงานนี้จะถูกจัดมาและสามารถนำกลับมาใช้ได้อย่างง่ายดายในโค.
ตรรกะที่จะนำไปใช้กับแต่ละพื้นที่ถูกกำหนดภายในคลาสที่ใช้อินเทอร์เฟซIFieldMergingCallback ในทำนองเดียวกันตัวจัดการMail Mergeสามารถตั้งค่าเพื่อควบคุมวิธีการรวมแต่ละฟิลด์ตัวจัดการนี้สาม ภายในตัวจัดการนี้คุณสามารถตั้งค่ารหัสเพื่อเปลี่ยนข้อความของภูมิภาคเอาโหนดหรือแถวที่ว่างเปล่าและเซลล์ฯลฯ.
ในตัวอย่างนี้เราจะใช้เอกสารที่แสดงด้านล่าง พื้นที่ที่ซ้อนกันและพื้นที่ที่อยู่ภายในตาราง
เพื่อเป็นการสาธิตอย่างรวดเร็ว เราสามารถเรียกใช้ฐานข้อมูลตัวอย่างบนเอกสารตัวอย่างโดยเปิดใช้งานแฟล็ก MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS คุณสมบัติจะลบภูมิภาคที่ไม่รวมออกจากเอกสารโดยอัตโนมัติระหว่างการ mail merge.
แหล่งข้อมูลประกอบด้วยสองระเบียนสำหรับภูมิภาคStoreDetailsแต่จงใจจะมีข้อมูลใดๆสำหรับเด็กContactDetailsภูมิภาค นอกจากนี้พื้นที่Suppliersไม่มีแถวข้อมูลใดๆเช่นกัน ทำให้พื้นที่ที่ไม่ได้ใช้ยังคงอยู่ในเอกสาร ผลลัพธ์หลังจากการรวมเอกสารกับแหล่งข้อมูลนี้อยู่ด้านล่าง
ดังที่ระบุไว้ในภาพคุณจะเห็นว่าภูมิภาคContactDetailsสำหรับเรกคอร์ดที่สองและSuppliersภูมิภาคถูกลบออกโดยอัตโนมัติโดยเครื่องยนต์Mail Mergeเนื่องจากไม่มีข้อมูล อย่างไรก็ตามมีปัญหาบางอย่างที่ทำให้เอกสารที่ส่งออกนี้ดูไม่สมบูรณ์:
- พื้นที่ContactDetailsยังคงทิ้งย่อหน้าไว้ด้วยข้อความ"รายละเอียดการติดต่อ".
- ในกรณีเดียวกันมีข้อบ่งชี้ว่ามีหมายเลขโทรศัพท์ไม่มีเพียงพื้นที่ว่างซึ่งอาจนำไปสู่ความสับส.
- ตารางและหัวเรื่องที่เกี่ยวข้องกับพื้นที่Suppliersยังคงอยู่หลังจากพื้นที่ภายในตารางถูกลบออก.
เทคนิคที่ให้ไว้ในบทความนี้แสดงให้เห็นถึงวิธีการใช้ตรรกะแบบกำหนดเองกับแต่ละภูมิภาค.
การแก้ปัญหา
เมื่อต้องการใช้ตรรกะกับแต่ละพื้นที่ที่ไม่ได้ใช้ในเอกสารด้วยตนเองเราใช้ประโยชน์จากคุณลักษณะที่มีอยู่แล้วในAspose.Words.
เครื่องยนต์Mail Mergeให้คุณสมบัติในการลบพื้นที่ที่ไม่ได้ใช้ผ่านธงMailMergeCleanupOptions.RemoveUnusedRegions สามารถปิดใช้งานเพื่อไม่ให้พื้นที่ดังกล่าวถูกแตะต้องในระหว่างmail merge นี้ช่วยให้เราสามารถออกจากพื้นที่ที่ไม่รวมในเอกสารและจัดการกับพวกเขาด้วยตนเองแ.
จากนั้นเราสามารถใช้ประโยชน์จากคุณสมบัติMailMerge.FieldMergingCallbackเป็นวิธีการที่จะใช้ตรรกะที่กำหนดเองของเราเองกับภูมิภาคที่ไม่รวมเหล่านี้ในระหว่างMail Mergeผ่านการใช้คลาสจัดการการใช้อินเตอร์เฟซIFieldMergingCallback.
รหัสนี้ภายในคลาสตัวจัดการเป็นคลาสเดียวที่คุณจะต้องปรับเปลี่ยนเพื่อควบคุมตรรกะที่ใช้กั รหัสอื่นๆในตัวอย่างนี้สามารถนำกลับมาใช้ได้โดยไม่ต้องปรับเปลี่ยนในโครงการใดๆ.
โครงการตัวอย่างนี้แสดงให้เห็นถึงเทคนิคนี้ มันเกี่ยวข้องกับขั้นตอนต่อไปนี้:
- ประมวลผลMail Mergeบนเอกสารโดยใช้แหล่งข้อมูลของคุณ ธงMailMergeCleanupOptions.RemoveUnusedRegionsถูกปิดใช้งานสำหรับตอนนี้เราต้องการให้ภูมิภาคยังคงอยู่เพื่อให้เราสามารถจัดการกับ ภูมิภาคใดๆที่ไม่มีข้อมูลจะถูกทิ้งไว้ในเอกสาร.
- เรียกวิธีการExecuteCustomLogicOnEmptyRegions วิธีการนี้มีอยู่ในตัวอย่างนี้ นินการซึ่งอนุญาตให้เรียกตัวจัดการที่ระบุสำหรับแต่ละพื้นที่ที่ไม่รวม วิธีนี้สามารถนำมาใช้ใหม่และสามารถคัดลอกไม่ได้เปลี่ยนแปลงไปยังโครงการใดๆที่ต้องใวิธีนี้ดำเนินการขั้นตอนต่อไปนี้:
- ตั้งค่าตัวจัดการที่ระบุโดยผู้ใช้ในคุณสมบัติMailMerge.FieldMergingCallback.
- เรียกวิธีการCreateDataSourceFromDocumentRegionsที่ยอมรับของผู้ใช้DocumentและArrayListที่มีชื่อพื้นที่ เมธอดนี้จะสร้างแหล่งข้อมูลแบบจำลองที่มีตารางสำหรับแต่ละพื้นที่ที่ไม่รวมในเอกสาร.
- รันMail Mergeบนเอกสารโดยใช้แหล่งข้อมูลแบบจำลอง เมื่อMail Mergeถูกเรียกใช้กับแหล่งข้อมูลนี้จะอนุญาตให้เรียกใช้ตัวจัดการที่ผู้ใช้ระบุสำหรับแต่ละพื้น
รหัส
การใช้งานสำหรับวิธีการExecuteCustomLogicOnEmptyRegionsจะพบด้านล่าง วิธีนี้ยอมรับพารามิเตอร์หลาย:
- ออบเจกต์Documentที่มีพื้นที่ที่ไม่รวมซึ่งจะถูกจัดการโดยตัวจัดการที่ผ่าน.
- คลาสตัวจัดการที่กำหนดตรรกะที่จะนำไปใช้กับพื้นที่ที่ไม่รวม การจัดการนี้จะต้องใช้ IFieldMergingCallback อินเตอร์เฟซ.
- ผ่านการใช้เกินที่เหมาะสมวิธีการยังสามารถยอมรับพารามิเตอร์ที่สาม-รายการของชื่อภู หากระบุสิ่งนี้แล้วจะมีเพียงชื่อพื้นที่ที่เหลืออยู่ในเอกสารที่ระบุในรายการเท่านั้นที่จะถูกจัดก ภูมิภาคอื่นๆที่พบจะไม่ถูกเรียกโดยตัวจัดการและลบออกโดยอัตโนมัติ เมื่อมีการระบุการโอเวอร์โหลดที่มีพารามิเตอร์เพียงสองพารามิเตอร์ทุกพื้นที่ที่เหลืออยู่ในเ.
งคุณ
แสดงวิธีรันลอจิกแบบกำหนดเองบนพื้นที่ที่ไม่ได้ใช้โดยใช้ตัวจัดการที่ระบุ.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
/** | |
* Applies logic defined in the passed handler class to all unused regions | |
* in the document. This allows to manually control how unused regions are | |
* handled in the document. | |
* | |
* @param doc The document containing unused regions. | |
* @param handler The handler which implements the IFieldMergingCallback | |
* interface and defines the logic to be applied to each unmerged | |
* region. | |
*/ | |
public static void executeCustomLogicOnEmptyRegions(Document doc, IFieldMergingCallback handler) throws Exception { | |
executeCustomLogicOnEmptyRegions(doc, handler, null); // Pass null to handle all regions found in the document. | |
} | |
/** | |
* Applies logic defined in the passed handler class to specific unused | |
* regions in the document as defined in regionsList. This allows to | |
* manually control how unused regions are handled in the document. | |
* | |
* @param doc The document containing unused regions. | |
* @param handler The handler which implements the IFieldMergingCallback | |
* interface and defines the logic to be applied to each unmerged | |
* region. | |
* @param regionsList A list of strings corresponding to the region names that are | |
* to be handled by the supplied handler class. Other regions | |
* encountered will not be handled and are removed automatically. | |
*/ | |
public static void executeCustomLogicOnEmptyRegions(Document doc, IFieldMergingCallback handler, ArrayList regionsList) throws Exception { | |
// Certain regions can be skipped from applying logic to by not adding the table name inside the CreateEmptyDataSource method. | |
// Enable this cleanup option so any regions which are not handled by the user's logic are removed automatically. | |
doc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS); | |
// Set the user's handler which is called for each unmerged region. | |
doc.getMailMerge().setFieldMergingCallback(handler); | |
// Execute mail merge using the dummy dataset. The dummy data source contains the table names of | |
// each unmerged region in the document (excluding ones that the user may have specified to be skipped). This will allow the handler | |
// to be called for each field in the unmerged regions. | |
doc.getMailMerge().executeWithRegions(createDataSourceFromDocumentRegions(doc, regionsList)); | |
} | |
/** | |
* A helper method that creates an empty Java disconnected ResultSet with | |
* the specified columns. | |
*/ | |
private static ResultSet createCachedRowSet(String[] columnNames) throws Exception { | |
RowSetMetaDataImpl metaData = new RowSetMetaDataImpl(); | |
metaData.setColumnCount(columnNames.length); | |
for (int i = 0; i < columnNames.length; i++) { | |
metaData.setColumnName(i + 1, columnNames[i]); | |
metaData.setColumnType(i + 1, java.sql.Types.VARCHAR); | |
} | |
CachedRowSet rowSet = RowSetProvider.newFactory().createCachedRowSet(); | |
; | |
rowSet.setMetaData(metaData); | |
return rowSet; | |
} | |
/** | |
* A helper method that adds a new row with the specified values to a | |
* disconnected ResultSet. | |
*/ | |
private static void addRow(ResultSet resultSet, String[] values) throws Exception { | |
resultSet.moveToInsertRow(); | |
for (int i = 0; i < values.length; i++) | |
resultSet.updateString(i + 1, values[i]); | |
resultSet.insertRow(); | |
// This "dance" is needed to add rows to the end of the result set properly. | |
// If I do something else then rows are either added at the front or the result | |
// set throws an exception about a deleted row during mail merge. | |
resultSet.moveToCurrentRow(); | |
resultSet.last(); | |
} |
งคุณ
กำหนดวิธีที่ใช้ในการจัดการพื้นที่ที่ไม่รวมด้วยตนเอง.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
/** | |
* Returns a DataSet object containing a DataTable for the unmerged regions | |
* in the specified document. If regionsList is null all regions found | |
* within the document are included. If an ArrayList instance is present the | |
* only the regions specified in the list that are found in the document are | |
* added. | |
*/ | |
private static DataSet createDataSourceFromDocumentRegions(Document doc, ArrayList regionsList) throws Exception { | |
final String TABLE_START_MARKER = "TableStart:"; | |
DataSet dataSet = new DataSet(); | |
String tableName = null; | |
for (String fieldName : doc.getMailMerge().getFieldNames()) { | |
if (fieldName.contains(TABLE_START_MARKER)) { | |
tableName = fieldName.substring(TABLE_START_MARKER.length()); | |
} else if (tableName != null) { | |
// Only add the table as a new DataTable if it doesn't already exists in the DataSet. | |
if (dataSet.getTables().get(tableName) == null) { | |
ResultSet resultSet = createCachedRowSet(new String[]{fieldName}); | |
// We only need to add the first field for the handler to be called for the fields in the region. | |
if (regionsList == null || regionsList.contains(tableName)) { | |
addRow(resultSet, new String[]{"FirstField"}); | |
} | |
dataSet.getTables().add(new DataTable(resultSet, tableName)); | |
} | |
tableName = null; | |
} | |
} | |
return dataSet; | |
} |
วิธีการนี้เกี่ยวข้องกับการค้นหาพื้นที่ที่ไม่ได้รวมทั้งหมดในเอกสาร วิธีนี้ทำได้โดยใช้วิธีการMailMerge.GetFieldNames เมธอดนี้จะส่งคืนฟิลด์ที่ผสานทั้งหมดในเอกสารรวมถึงเครื่องหมายจุดเริ่มต้นและจุดสิ้นสุดของพื้นที่(แสดงโดยฟิลด์ผสานกับคำนำหน้าTableStartหรือTableEnd).
เมื่อพบฟิลด์TableStart
ผสานนี้จะถูกเพิ่มเป็นDataTableใหม่ในDataSet รรวมกับระเบียนหลายรายการ)ตารางจะสร้างและเพิ่มก็ต่อเมื่อไม่มีอยู่แล้วในDataSet.
เมื่อพบจุดเริ่มต้นภูมิภาคที่เหมาะสมและเพิ่มลงในฐานข้อมูลแล้ว ฟิลด์ถัดไป (ซึ่งสอดคล้องกับฟิลด์แรกในภูมิภาค) จะถูกเพิ่มลงใน DataTable จำเป็นต้องเพิ่มเฉพาะฟิลด์แรกสำหรับแต่ละฟิลด์ในภูมิภาคที่จะผสานและส่งไปยังตัวจัดการ.
นอกจากนี้เรายังตั้งค่าฟิลด์ของฟิลด์แรกเป็น"FirstField"เพื่อให้ง่ายต่อการใช้ตรรกะกับฟิลด์แรกหรืออื่ โดยรวมนี้มันหมายความว่ามันไม่จำเป็นที่จะยากรหัสชื่อของฟิลด์แรกหรือใช้รหัสพิเศษเพื่อ.
รหัสด้านล่างแสดงให้เห็นถึงวิธีการทำงานของระบบนี้ เอกสารที่แสดงในช่วงเริ่มต้นของบทความนี้จะถูกจดจำด้วยแหล่งข้อมูลเดียวกันแต่คราวนี้.
งคุณ
แสดงวิธีจัดการพื้นที่ที่ไม่รวมหลังจากMail Mergeด้วยโค้ดที่ผู้ใช้กำหนด.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Open the document. | |
Document doc = new Document(dataDir + "TestFile.doc"); | |
// Create a data source which has some data missing. | |
// This will result in some regions that are merged and some that remain after executing mail merge. | |
DataSet data = getDataSource(); | |
// Make sure that we have not set the removal of any unused regions as we will handle them manually. | |
// We achieve this by removing the RemoveUnusedRegions flag from the cleanup options by using the AND and NOT bitwise operators. | |
doc.getMailMerge().setCleanupOptions(doc.getMailMerge().getCleanupOptions() & ~MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS); | |
// Execute mail merge. Some regions will be merged with data, others left unmerged. | |
doc.getMailMerge().executeWithRegions(data); | |
// The regions which contained data now would of been merged. Any regions which had no data and were | |
// not merged will still remain in the document. | |
Document mergedDoc = doc.deepClone(); //ExSkip | |
// Apply logic to each unused region left in the document using the logic set out in the handler. | |
// The handler class must implement the IFieldMergingCallback interface. | |
executeCustomLogicOnEmptyRegions(doc, new EmptyRegionsHandler()); | |
// Save the output document to disk. | |
doc.save(dataDir + "TestFile.CustomLogicEmptyRegions1 Out.doc"); |
รหัสดำเนินการที่แตกต่างกันตามชื่อของพื้นที่ที่ดึงข้อมูลโดยใช้คุณสมบัติFieldMergingArgs.TableName โปรดทราบว่าขึ้นอยู่กับเอกสารและภูมิภาคของคุณคุณสามารถรหัสตัวจัดการเพื่อเรียกใช้ตรรกะขึ้นอยู่กับแต่ละภูมิภาคหรือรหัสที่ใช้กับทุกพื้นที่ที่ไม่ได้รวมในเอกสารหรือการรวมกันของทั้งสอง.
ตรรกะสำหรับภูมิภาคContactDetailsเกี่ยวข้องกับการเปลี่ยนข้อความของแต่ละฟิลด์ในภูมิภาคContactDetailsด้วยข้อคว ชื่อของแต่ละฟิลด์จะตรงกันภายในตัวจัดการโดยใช้คุณสมบัติFieldMergingArgs.FieldName.
กระบวนการที่คล้ายกันจะถูกนำไปใช้กับภูมิภาคSuppliersด้วยการเพิ่มรหัสพิเศษเพื่อจัดการตารางที่ รหัสจะตรวจสอบว่าภูมิภาคอยู่ภายในตาราง(ตามที่มันอาจจะถูกลบออกแล้ว) หากเป็นเช่นนั้นมันจะลบตารางทั้งหมดออกจากเอกสารรวมทั้งย่อหน้าที่นำหน้าตราบใดที่มีการจัดรูปแบบด้วยรูปแบบหัวเรื่องเช่น"Heading 1".
งคุณ
แสดงวิธีกำหนดลอจิกแบบกำหนดเองในตัวจัดการการดำเนินการIFieldMergingCallbackที่ถูกดำเนินการสำหรับพื้.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
public static class EmptyRegionsHandler implements IFieldMergingCallback { | |
/** | |
* Called for each field belonging to an unmerged region in the | |
* document. | |
*/ | |
public void fieldMerging(FieldMergingArgs args) throws Exception { | |
// Change the text of each field of the ContactDetails region individually. | |
if ("ContactDetails".equals(args.getTableName())) { | |
// Set the text of the field based off the field name. | |
if ("Name".equals(args.getFieldName())) | |
args.setText("(No details found)"); | |
else if ("Number".equals(args.getFieldName())) | |
args.setText("(N/A)"); | |
} | |
// Remove the entire table of the Suppliers region. Also check if the previous paragraph | |
// before the table is a heading paragraph and if so remove that too. | |
if ("Suppliers".equals(args.getTableName())) { | |
Table table = (Table) args.getField().getStart().getAncestor(NodeType.TABLE); | |
// Check if the table has been removed from the document already. | |
if (table.getParentNode() != null) { | |
// Try to find the paragraph which precedes the table before the table is removed from the document. | |
if (table.getPreviousSibling() != null && table.getPreviousSibling().getNodeType() == NodeType.PARAGRAPH) { | |
Paragraph previousPara = (Paragraph) table.getPreviousSibling(); | |
if (isHeadingParagraph(previousPara)) | |
previousPara.remove(); | |
} | |
table.remove(); | |
} | |
} | |
} | |
/** | |
* Returns true if the paragraph uses any Heading style e.g Heading 1 to | |
* Heading 9 | |
*/ | |
private boolean isHeadingParagraph(Paragraph para) throws Exception { | |
return (para.getParagraphFormat().getStyleIdentifier() >= StyleIdentifier.HEADING_1 && para.getParagraphFormat().getStyleIdentifier() <= StyleIdentifier.HEADING_9); | |
} | |
public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception { | |
// Do Nothing | |
} | |
} |
ผลของรหัสข้างต้นจะแสดงด้านล่าง รกจะถูกแทนที่ด้วยข้อความที่ให้ข้อมูลและการลบของตารางและส่วนหัวช่วยให้เอกสารที่.
รันในทุกพื้นที่ที่ไม่ได้ใช้แทนที่จะเป็นเพียงภูมิภาคที่เฉพาะเจาะจงโดยการลบการตรวจสอบชื่ ในกรณีนี้หากภูมิภาคใดๆภายในตารางไม่รวมกับข้อมูลใดๆทั้งภูมิภาคและตารางคอนเทนเนอร์.
เราสามารถแทรกโค้ดที่แตกต่างกันในตัวจัดการเพื่อควบคุมวิธีการจัดการภูมิภาคที่ไม่มีกา บทความนี้ไม่มีการอ้างอิงจากเอกสารอ้างอิงหรือแหล่งข้อมูลโปรดช่วยพัฒนาบทความนี้โด ย่อหน้าอื่นๆเหล่านี้จะถูกลบออกเนื่องจากจะยังคงอยู่ในภูมิภาคหลังจากการรวมข้อความขอ.
ข้อความการแทนที่จะถูกผสานลงในฟิลด์แรกโดยการตั้งค่าข้อความที่ระบุไว้ในคุณสมบัติFieldMergingArgs.Text ข้อความจากคุณสมบัตินี้ถูกรวมเข้ากับฟิลด์โดยเครื่องยนต์Mail Merge.
รหัสนี้ใช้เฉพาะฟิลด์แรกในพื้นที่โดยการตรวจสอบคุณสมบัติFieldMergingArgs.FieldValue ค่าฟิลด์ของฟิลด์แรกในพื้นที่ถูกทำเครื่องหมายด้วย"FirstField" นี้ทำให้ตรรกะชนิดนี้ง่ายต่อการใช้ในหลายภูมิภาคเนื่องจากไม่มีรหัสพิเศษเป็นสิ่งจำเป็น.
งคุณ
แสดงวิธีแทนที่พื้นที่ที่ไม่ได้ใช้ด้วยข้อความและลบย่อหน้าพิเศษ.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Store the parent paragraph of the current field for easy access. | |
Paragraph parentParagraph = args.getField().getStart().getParentParagraph(); | |
// Define the logic to be used when the ContactDetails region is encountered. | |
// The region is removed and replaced with a single line of text stating that there are no records. | |
if ("ContactDetails".equals(args.getTableName())) { | |
// Called for the first field encountered in a region. This can be used to execute logic on the first field | |
// in the region without needing to hard code the field name. Often the base logic is applied to the first field and | |
// different logic for other fields. The rest of the fields in the region will have a null FieldValue. | |
if ("FirstField".equals(args.getFieldValue())) { | |
FindReplaceOptions opts = new FindReplaceOptions(); | |
opts.setMatchCase(false); | |
opts.setFindWholeWordsOnly(false); | |
// Remove the "Name:" tag from the start of the paragraph | |
parentParagraph.getRange().replace("Name:", "", opts); | |
// Set the text of the first field to display a message stating that there are no records. | |
args.setText("No records to display"); | |
} else { | |
// We have already inserted our message in the paragraph belonging to the first field. The other paragraphs in the region | |
// will still remain so we want to remove these. A check is added to ensure that the paragraph has not already been removed. | |
// which may happen if more than one field is included in a paragraph. | |
if (parentParagraph.getParentNode() != null) | |
parentParagraph.remove(); | |
} | |
} |
เอกสารผลลัพธ์หลังจากที่รหัสข้างต้นได้รับการดำเนินการจะแสดงด้านล่าง พื้นที่ที่ไม่ได้ใช้จะถูกแทนที่ด้วยข้อความที่ระบุว่าไม่มีระเบียนที่จะแสดง.
เป็นอีกตัวอย่างหนึ่ง,เราสามารถแทรกรหัสด้านล่างแทนรหัสเดิมจัดการSuppliersRegion. นี้จะแสดงข้อความภายในตารางและผสานเซลล์แทนการเอาตารางออกจากเอกสาร เนื่องจากภูมิภาคอยู่ในตารางที่มีหลายเซลล์,มันดูดีกว่าที่จะมีเซลล์ของตารางรวมกันและข้อ.
งคุณ
แสดงวิธีผสานเซลล์ระดับบนทั้งหมดของพื้นที่ที่ไม่ได้ใช้และแสดงข้อความภายในตาราง.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Replace the unused region in the table with a "no records" message and merge all cells into one. | |
if ("Suppliers".equals(args.getTableName())) { | |
if ("FirstField".equals(args.getFieldValue())) { | |
// We will use the first paragraph to display our message. Make it centered within the table. The other fields in other cells | |
// within the table will be merged and won't be displayed so we don't need to do anything else with them. | |
parentParagraph.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER); | |
args.setText("No records to display"); | |
} | |
// Merge the cells of the table together. | |
Cell cell = (Cell) parentParagraph.getAncestor(NodeType.CELL); | |
if (cell != null) { | |
if (cell.isFirstCell()) | |
cell.getCellFormat().setHorizontalMerge(CellMerge.FIRST); // If this cell is the first cell in the table then the merge is started using "CellMerge.First". | |
else | |
cell.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS); // Otherwise the merge is continued using "CellMerge.Previous". | |
} | |
} |
เอกสารผลลัพธ์หลังจากที่รหัสข้างต้นได้รับการดำเนินการจะแสดงด้านล่าง.
สุดท้าย,เราสามารถเรียกวิธีการExecuteCustomLogicOnEmptyRegionsและระบุชื่อตารางที่ควรจะจัดการภายในวิธีการจัดการข.
งคุณ
แสดงวิธีระบุเฉพาะพื้นที่ContactDetails
ที่จะจัดการผ่านคลาสตัวจัดการ.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
ArrayList<String> regions = new ArrayList<String>(); | |
regions.add("ContactDetails"); | |
executeCustomLogicOnEmptyRegions(doc, new EmptyRegionsHandler(), regions); |
การโทรเกินพิกัดนี้ด้วยArrayListที่ระบุจะสร้างแหล่งข้อมูลซึ่งมีเฉพาะแถวข้อมูลสำหรับพื้นที่ที่ระบุ ภูมิภาคอื่นที่ไม่ใช่ภูมิภาคContactDetails
จะไม่ถูกจัดการและจะถูกลบออกโดยอัตโนมัติโดยเครื่องยนต์Mail Mergeแทน ผลของการโทรข้างต้นโดยใช้รหัสในตัวจัดการเดิมของเราจะแสดงด้านล่าง