Hi,
As shown below, I will do mail-merge in this <> field in doc template file.
image.png (2.4 KB)
And the content is a section of html:
<html>
<body>
<table width="2511" cellpadding="0" cellspacing="0" style="table-layout: fixed; border-collapse: collapse; border-width: 0px; border-color: #010101; ">
<colgroup>
<col width="314"></col>
<col width="314"></col>
<col width="314"></col>
<col width="314"></col>
<col width="314"></col>
<col width="314"></col>
<col width="314"></col>
<col width="314"></col>
</colgroup>
<tr>
<td align="left" valign="top" width="299" style="padding-left: 8px; padding-right: 8px; border: solid #010101 1px; ">
<div align="left" style="min-height: 9pt; "><span style="font-size: 8pt;"></span></div>
</td>
<td align="left" valign="top" width="299" style="padding-left: 8px; padding-right: 8px; border: solid #010101 1px; ">
<div align="left" style="min-height: 9pt; "><span style="font-size: 8pt;"></span></div>
</td>
<td align="left" valign="top" width="299" style="padding-left: 8px; padding-right: 8px; border: solid #010101 1px; ">
<div align="left" style="min-height: 9pt; "><span style="font-size: 8pt;"></span></div>
</td>
<td align="left" valign="top" width="299" style="padding-left: 8px; padding-right: 8px; border: solid #010101 1px; ">
<div align="left" style="min-height: 9pt; "><span style="font-size: 8pt;"></span></div>
</td>
<td align="left" valign="top" width="299" style="padding-left: 8px; padding-right: 8px; border: solid #010101 1px; ">
<div align="left" style="min-height: 9pt; "><span style="font-size: 8pt;"></span></div>
</td>
<td align="left" valign="top" width="299" style="padding-left: 8px; padding-right: 8px; border: solid #010101 1px; ">
<div align="left" style="min-height: 9pt; "><span style="font-size: 8pt;"></span></div>
</td>
<td align="left" valign="top" width="299" style="padding-left: 8px; padding-right: 8px; border: solid #010101 1px; ">
<div align="left" style="min-height: 9pt; "><span style="font-size: 8pt;"></span></div>
</td>
<td align="left" valign="top" width="299" style="padding-left: 8px; padding-right: 8px; border: solid #010101 1px; ">
<div align="left" style="min-height: 9pt; "><span style="font-size: 8pt;"></span></div>
</td></tr>
<tr>
<td align="left" valign="top" width="299" style="padding-left: 8px; padding-right: 8px; border: solid #010101 1px; ">
<div align="left" style="min-height: 9pt; "><span style="font-size: 8pt;"></span></div>
</td>
<td align="left" valign="top" width="299" style="padding-left: 8px; padding-right: 8px; border: solid #010101 1px; ">
<div align="left" style="min-height: 9pt; "><span style="font-size: 8pt;"></span></div>
</td>
<td align="left" valign="top" width="299" style="padding-left: 8px; padding-right: 8px; border: solid #010101 1px; ">
<div align="left" style="min-height: 9pt; "><span style="font-size: 8pt;"></span></div>
</td>
<td align="left" valign="top" width="299" style="padding-left: 8px; padding-right: 8px; border: solid #010101 1px; ">
<div align="left" style="min-height: 9pt; "><span style="font-size: 8pt;"></span></div>
</td>
<td align="left" valign="top" width="299" style="padding-left: 8px; padding-right: 8px; border: solid #010101 1px; ">
<div align="left" style="min-height: 9pt; "><span style="font-size: 8pt;"></span></div>
</td>
<td align="left" valign="top" width="299" style="padding-left: 8px; padding-right: 8px; border: solid #010101 1px; ">
<div align="left" style="min-height: 9pt; "><span style="font-size: 8pt;"></span></div>
</td>
<td align="left" valign="top" width="299" style="padding-left: 8px; padding-right: 8px; border: solid #010101 1px; ">
<div align="left" style="min-height: 9pt; "><span style="font-size: 8pt;"></span></div>
</td>
<td align="left" valign="top" width="299" style="padding-left: 8px; padding-right: 8px; border: solid #010101 1px; ">
<div align="left" style="min-height: 9pt; "><span style="font-size: 8pt;"></span></div>
</td></tr>
<tr>
<td align="left" valign="top" width="299" style="padding-left: 8px; padding-right: 8px; border: solid #010101 1px; ">
<div align="left" style="min-height: 9pt; "><span style="font-size: 8pt;"></span></div>
</td>
<td align="left" valign="top" width="299" style="padding-left: 8px; padding-right: 8px; border: solid #010101 1px; ">
<div align="left" style="min-height: 9pt; "><span style="font-size: 8pt;"></span></div>
</td>
<td align="left" valign="top" width="299" style="padding-left: 8px; padding-right: 8px; border: solid #010101 1px; ">
<div align="left" style="min-height: 9pt; "><span style="font-size: 8pt;"></span></div>
</td>
<td align="left" valign="top" width="299" style="padding-left: 8px; padding-right: 8px; border: solid #010101 1px; ">
<div align="left" style="min-height: 9pt; "><span style="font-size: 8pt;"></span></div>
</td>
<td align="left" valign="top" width="299" style="padding-left: 8px; padding-right: 8px; border: solid #010101 1px; ">
<div align="left" style="min-height: 9pt; "><span style="font-size: 8pt;"></span></div>
</td>
<td align="left" valign="top" width="299" style="padding-left: 8px; padding-right: 8px; border: solid #010101 1px; ">
<div align="left" style="min-height: 9pt; "><span style="font-size: 8pt;"></span></div>
</td>
<td align="left" valign="top" width="299" style="padding-left: 8px; padding-right: 8px; border: solid #010101 1px; ">
<div align="left" style="min-height: 9pt; "><span style="font-size: 8pt;"></span></div>
</td>
<td align="left" valign="top" width="299" style="padding-left: 8px; padding-right: 8px; border: solid #010101 1px; ">
<div align="left" style="min-height: 9pt; "><span style="font-size: 8pt;"></span></div>
</td></tr>
</table>
<div align="left" style="min-height: 9pt; "></div>
</body>
</html>
In HTML export file, it shows fine like: image.png (1.5 KB)
But, in pdf/docx, it’s truncated like: image.png (884 Bytes)
Are there any ways to fix this? Or I have to change my html content code to adapt itself to different formats? How?
Thanks.
@windpine
To ensure a timely and accurate response, please attach the following resources here for testing:
- Your input Word document.
- Please attach the output Word file that shows the undesired behavior.
- Please attach the expected output Word/PDF file that shows the desired behavior.
- Please create a standalone console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.
As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.
PS: To attach these resources, please zip and upload them.
@tahir.manzoor
Hi,
I’ve attached the files and code. Pls take a look if anything still needed.Resources required.zip (93.0 KB)
In these files, the HTML format output is what I expected, that is I can see all the table content. The PDF/Docx output were both truncated.
Code:
package com.aspose.words.examples.mail_merge;
import com.aspose.words.*;
import com.aspose.words.examples.Utils;
public class SupportTest {
private static final String dataDir = Utils.getSharedDataDir(InsertCheckBoxesOrHTMLDuringMailMerge.class) + "MailMerge\\";
public static void main(String[] args) throws Exception {
execute();
}
private static void execute() throws Exception {
// Load the template document.
Document doc = new Document(dataDir + "Text Template.docx");
// Setup mail merge event handler to do the custom work.
doc.getMailMerge().setFieldMergingCallback(new HandleHtmlMergeField());
// This is the data for mail merge.
String[] fieldNames = new String[]{"RN_COMMENTS"};
Object[] fieldValues = new Object[]{"<html>\n" +
"<body>\n" +
"<table width=\"2511\" cellpadding=\"0\" cellspacing=\"0\" style=\"table-layout: fixed; border-collapse: collapse; border-width: 0px; border-color: #010101; \">\n" +
" <colgroup>\n" +
" <col width=\"314\"></col>\n" +
" <col width=\"314\"></col>\n" +
" <col width=\"314\"></col>\n" +
" <col width=\"314\"></col>\n" +
" <col width=\"314\"></col>\n" +
" <col width=\"314\"></col>\n" +
" <col width=\"314\"></col>\n" +
" <col width=\"314\"></col>\n" +
" </colgroup>\n" +
" <tr>\n" +
" <td align=\"left\" valign=\"top\" width=\"299\" style=\"padding-left: 8px; padding-right: 8px; border: solid #010101 1px; \">\n" +
" <div align=\"left\" style=\"min-height: 9pt; \"><span style=\"font-size: 8pt;\">aaa</span></div>\n" +
" </td>\n" +
" <td align=\"left\" valign=\"top\" width=\"299\" style=\"padding-left: 8px; padding-right: 8px; border: solid #010101 1px; \">\n" +
" <div align=\"left\" style=\"min-height: 9pt; \"><span style=\"font-size: 8pt;\">aaa</span></div>\n" +
" </td>\n" +
" <td align=\"left\" valign=\"top\" width=\"299\" style=\"padding-left: 8px; padding-right: 8px; border: solid #010101 1px; \">\n" +
" <div align=\"left\" style=\"min-height: 9pt; \"><span style=\"font-size: 8pt;\">aaa</span></div>\n" +
" </td>\n" +
" <td align=\"left\" valign=\"top\" width=\"299\" style=\"padding-left: 8px; padding-right: 8px; border: solid #010101 1px; \">\n" +
" <div align=\"left\" style=\"min-height: 9pt; \"><span style=\"font-size: 8pt;\">aaa</span></div>\n" +
" </td>\n" +
" <td align=\"left\" valign=\"top\" width=\"299\" style=\"padding-left: 8px; padding-right: 8px; border: solid #010101 1px; \">\n" +
" <div align=\"left\" style=\"min-height: 9pt; \"><span style=\"font-size: 8pt;\">aaa</span></div>\n" +
" </td>\n" +
" <td align=\"left\" valign=\"top\" width=\"299\" style=\"padding-left: 8px; padding-right: 8px; border: solid #010101 1px; \">\n" +
" <div align=\"left\" style=\"min-height: 9pt; \"><span style=\"font-size: 8pt;\">aaa</span></div>\n" +
" </td>\n" +
" <td align=\"left\" valign=\"top\" width=\"299\" style=\"padding-left: 8px; padding-right: 8px; border: solid #010101 1px; \">\n" +
" <div align=\"left\" style=\"min-height: 9pt; \"><span style=\"font-size: 8pt;\">aaa</span></div>\n" +
" </td>\n" +
" <td align=\"left\" valign=\"top\" width=\"299\" style=\"padding-left: 8px; padding-right: 8px; border: solid #010101 1px; \">\n" +
" <div align=\"left\" style=\"min-height: 9pt; \"><span style=\"font-size: 8pt;\">aaa</span></div>\n" +
" </td></tr>\n" +
" <tr>\n" +
" <td align=\"left\" valign=\"top\" width=\"299\" style=\"padding-left: 8px; padding-right: 8px; border: solid #010101 1px; \">\n" +
" <div align=\"left\" style=\"min-height: 9pt; \"><span style=\"font-size: 8pt;\">aaa</span></div>\n" +
" </td>\n" +
" <td align=\"left\" valign=\"top\" width=\"299\" style=\"padding-left: 8px; padding-right: 8px; border: solid #010101 1px; \">\n" +
" <div align=\"left\" style=\"min-height: 9pt; \"><span style=\"font-size: 8pt;\">aaa</span></div>\n" +
" </td>\n" +
" <td align=\"left\" valign=\"top\" width=\"299\" style=\"padding-left: 8px; padding-right: 8px; border: solid #010101 1px; \">\n" +
" <div align=\"left\" style=\"min-height: 9pt; \"><span style=\"font-size: 8pt;\">aaa</span></div>\n" +
" </td>\n" +
" <td align=\"left\" valign=\"top\" width=\"299\" style=\"padding-left: 8px; padding-right: 8px; border: solid #010101 1px; \">\n" +
" <div align=\"left\" style=\"min-height: 9pt; \"><span style=\"font-size: 8pt;\">aaa</span></div>\n" +
" </td>\n" +
" <td align=\"left\" valign=\"top\" width=\"299\" style=\"padding-left: 8px; padding-right: 8px; border: solid #010101 1px; \">\n" +
" <div align=\"left\" style=\"min-height: 9pt; \"><span style=\"font-size: 8pt;\">aaa</span></div>\n" +
" </td>\n" +
" <td align=\"left\" valign=\"top\" width=\"299\" style=\"padding-left: 8px; padding-right: 8px; border: solid #010101 1px; \">\n" +
" <div align=\"left\" style=\"min-height: 9pt; \"><span style=\"font-size: 8pt;\">aaa</span></div>\n" +
" </td>\n" +
" <td align=\"left\" valign=\"top\" width=\"299\" style=\"padding-left: 8px; padding-right: 8px; border: solid #010101 1px; \">\n" +
" <div align=\"left\" style=\"min-height: 9pt; \"><span style=\"font-size: 8pt;\">aaa</span></div>\n" +
" </td>\n" +
" <td align=\"left\" valign=\"top\" width=\"299\" style=\"padding-left: 8px; padding-right: 8px; border: solid #010101 1px; \">\n" +
" <div align=\"left\" style=\"min-height: 9pt; \"><span style=\"font-size: 8pt;\">aaa</span></div>\n" +
" </td></tr>\n" +
" <tr>\n" +
" <td align=\"left\" valign=\"top\" width=\"299\" style=\"padding-left: 8px; padding-right: 8px; border: solid #010101 1px; \">\n" +
" <div align=\"left\" style=\"min-height: 9pt; \"><span style=\"font-size: 8pt;\">aaa</span></div>\n" +
" </td>\n" +
" <td align=\"left\" valign=\"top\" width=\"299\" style=\"padding-left: 8px; padding-right: 8px; border: solid #010101 1px; \">\n" +
" <div align=\"left\" style=\"min-height: 9pt; \"><span style=\"font-size: 8pt;\">aaa</span></div>\n" +
" </td>\n" +
" <td align=\"left\" valign=\"top\" width=\"299\" style=\"padding-left: 8px; padding-right: 8px; border: solid #010101 1px; \">\n" +
" <div align=\"left\" style=\"min-height: 9pt; \"><span style=\"font-size: 8pt;\">aaa</span></div>\n" +
" </td>\n" +
" <td align=\"left\" valign=\"top\" width=\"299\" style=\"padding-left: 8px; padding-right: 8px; border: solid #010101 1px; \">\n" +
" <div align=\"left\" style=\"min-height: 9pt; \"><span style=\"font-size: 8pt;\">aaa</span></div>\n" +
" </td>\n" +
" <td align=\"left\" valign=\"top\" width=\"299\" style=\"padding-left: 8px; padding-right: 8px; border: solid #010101 1px; \">\n" +
" <div align=\"left\" style=\"min-height: 9pt; \"><span style=\"font-size: 8pt;\">aaa</span></div>\n" +
" </td>\n" +
" <td align=\"left\" valign=\"top\" width=\"299\" style=\"padding-left: 8px; padding-right: 8px; border: solid #010101 1px; \">\n" +
" <div align=\"left\" style=\"min-height: 9pt; \"><span style=\"font-size: 8pt;\">aaa</span></div>\n" +
" </td>\n" +
" <td align=\"left\" valign=\"top\" width=\"299\" style=\"padding-left: 8px; padding-right: 8px; border: solid #010101 1px; \">\n" +
" <div align=\"left\" style=\"min-height: 9pt; \"><span style=\"font-size: 8pt;\">aaa</span></div>\n" +
" </td>\n" +
" <td align=\"left\" valign=\"top\" width=\"299\" style=\"padding-left: 8px; padding-right: 8px; border: solid #010101 1px; \">\n" +
" <div align=\"left\" style=\"min-height: 9pt; \"><span style=\"font-size: 8pt;\">aaa</span></div>\n" +
" </td></tr>\n" +
"</table>\n" +
"<div align=\"left\" style=\"min-height: 9pt; \"></div>\n" +
"</body>\n" +
"</html>"};
// Execute the mail merge.
doc.getMailMerge().execute(fieldNames, fieldValues);
// Save the finished document.
SaveOptions saveOptions = SaveOptions.createSaveOptions(SaveFormat.PDF);
doc.save(dataDir + "Text Template Out.pdf", saveOptions);
}
}
class HandleHtmlMergeField implements IFieldMergingCallback {
/**
* This handler is called for every mail merge field found in the document,
* for every record found in the data source.
*/
public void fieldMerging(FieldMergingArgs e) throws Exception {
if (mBuilder == null)
mBuilder = new DocumentBuilder(e.getDocument());
// We want to insert html during mail merge.
if ("RN_COMMENTS".equals(e.getFieldName())) {
mBuilder.moveToMergeField(e.getFieldName());
mBuilder.insertHtml((String) e.getFieldValue());
}
}
public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception {
// Do nothing.
}
private DocumentBuilder mBuilder;
}
@windpine
In your HTML, the table’s width is in points. Please use the following code snippet to get the desired output.
for(Table table :(Iterable<Table>)doc.getChildNodes(NodeType.TABLE, true))
{
table.setPreferredWidth(PreferredWidth.fromPercent(100));
}
doc.save(MyDir + "Text Template Out.pdf", saveOptions);
@tahir.manzoor
Hi tahir,
Really thanks for your solution. However, if two tables lay in parallel. Then setting it 100% width will also affect the result. Is there any ways to make it more adaptable? Maybe just only affect the cell or table where the html is put so that it can adapt itself to the outside frame width?
Pls take a look at the newest zip file.New Resources required.zip (66.0 KB)
@windpine
Pleas note that Aspose.Words mimics the behavior of MS Word. If you open the HTML document in MS Word, copy and paste it into input document, you will get the same output.
In your case, we suggest you please set the table’s width in points and use following code snippet to get the desired output. We have attached the input and output documents in this post for your kind reference. Docs.zip (41.5 KB)
for(Table table :(Iterable<Table>)doc.getChildNodes(NodeType.TABLE, true))
{
if(table.getAncestor(NodeType.TABLE) != null)
table.setPreferredWidth(PreferredWidth.fromPercent(100));
}
// Save the finished document.
SaveOptions saveOptions = SaveOptions.createSaveOptions(SaveFormat.PDF);
doc.save(MyDir + "Text Template Out.pdf", saveOptions);
@tahir.manzoor
Really appreciated for your solution. I’ll try to apply it to our real code.