We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

DocumentBuilder Cell Formatting problems

Hello,

I’m currently running the trial version of Aspose Words for Java v10.8.0 and I seem to be running into a few problems with inserting cells into my word document. The following code is a section of what I’m doing. It was working just fine yesterday and today when I ran the code it isn’t working. I’m running all of this is NetBeans 6.9.1 and using a Word Template to jump to various fields to merge in the data. runList is just an array list of runs that I call and format run is simply a method to get text formatting.

docBuilder.moveToMergeField(“ttIndvTaskNumTable”);
docBuilder.startTable();
docBuilder.getCellFormat().getBorders().clearFormatting();
for (int i = 0; i < runList.size(); i++) {
if (i == 0) {
docBuilder.insertCell();
docBuilder.getCellFormat().setWrapText(false);
docBuilder.write(formatRun(document, docBuilder, runList.get(i).getText()).getText());
} else if (i % 2 == 0) {
docBuilder.endRow();
docBuilder.insertCell();
docBuilder.getCellFormat().setWrapText(false);
docBuilder.write(formatRun(document, docBuilder, runList.get(i).getText()).getText());
} else {
docBuilder.insertCell();
docBuilder.getCellFormat().setWrapText(true);
docBuilder.write(formatRun(document, docBuilder, runList.get(i).getText()).getText());
}
}
docBuilder.endTable();

public Run formatRun(Document aDocument, DocumentBuilder aBuilder, String aString) {
Run run = new Run(aDocument);
run.getFont().setBold(aBuilder.getBold());
run.getFont().setItalic(aBuilder.getItalic());
run.getFont().setUnderline(aBuilder.getUnderline());
run.getFont().setSize(aBuilder.getFont().getSize());
run.setText(aString);
return run;
}


Thanks in advance guys.

Michael

Hi Michael,


Thanks for your inquiry. First of all, I would suggest you please upgrade to the latest version of Aspose.Words for Java 11.0.0 and let us know, if you still face problem on your side. Secondly, I noticed that you have not shared the complete code and Word template document. Could you please attach your input template document and complete code here for testing? I will investigate the issue on my side and provide you more information.

Best Regards,

Sorry for the late response getting back to you about the code. Attached is the full code and a copy of the template I’m trying to merge to:

package testPdfCreation;

import com.aspose.words.Cell;
import com.aspose.words.CellVerticalAlignment;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.LineStyle;
import com.aspose.words.Run;
import com.aspose.words.Table;
import com.aspose.words.Underline;
import com.lowagie.text.DocumentException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import com.aspose.words.NodeType;

/
*
* @author michael
*/
public class pdfCreate {

/

* Main method.
*
* @param args
* @throws DocumentException
* @throws FileNotFoundException
* @throws IOException
* @throws Exception
/
public static void main(String[] args) throws DocumentException, FileNotFoundException, IOException, Exception {
pdfCreate test1 = new pdfCreate();
test1.generateSectionOne();
}

/**

* @throws Exception
/
public void generateSectionOne() throws Exception {
//Enter the document location here…
Document document = new Document("");

ArrayList runList = new ArrayList();
runList.add(new Run(document, "ABC-def-ghi(
)"));
runList.add(new Run(document, “Testing an amount of text in this area.”));
runList.add(new Run(document, "645-789-2180(
)"));
runList.add(new Run(document, “More testing involved here.”));
runList.add(new Run(document, "000-000-0000(
)"));
runList.add(new Run(document, “Purposefully writing an absurd amount of text to ensure it carries over to the next line to see how the document builder handles something like this.”));
runList.add(new Run(document, “987-654-3210(*)”));
runList.add(new Run(document, “TEST!!!”));

docBuilder.moveToMerge("<!–[if gte mso 9]>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-US</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:DontVertAlignCellWithSp/>
<w:DontBreakConstrainedForcedTables/>
<w:DontVertAlignInTxbx/>
<w:Word11KerningPairs/>
<w:CachedColBalance/>
</w:Compatibility>
<m:mathPr>
<m:mathFont m:val=“Cambria Math”/>
<m:brkBin m:val=“before”/>
<m:brkBinSub m:val="–"/>
<m:smallFrac m:val=“off”/>
<m:dispDef/>
<m:lMargin m:val=“0”/>
<m:rMargin m:val=“0”/>
<m:defJc m:val=“centerGroup”/>
<m:wrapIndent m:val=“1440”/>
<m:intLim m:val=“subSup”/>
<m:naryLim m:val=“undOvr”/>
</m:mathPr></w:WordDocument>
<![endif]–><!–[if gte mso 10]>

/* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin-top:0in; mso-para-margin-right:0in; mso-para-margin-bottom:10.0pt; mso-para-margin-left:0in; line-height:115%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:"Times New Roman"; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin;}

<![endif]–><span style=“font-size: 10pt; line-height: 115%; font-family: “Calibri”,“sans-serif”;”>ttIndvTaskNumTable");
docBuilder.getCellFormat().getBorders().clearFormatting();
for (int i = 0; i < runList.size(); i++) {
if (i == 0) {
docBuilder.insertCell();
docBuilder.getCellFormat().setWrapText(false);
docBuilder.write(formatRun(document, docBuilder, runList.get(i).getText()).getText());
} else if (i % 2 == 0) {
docBuilder.endRow();
docBuilder.insertCell();
docBuilder.getCellFormat().setWrapText(false);
docBuilder.write(formatRun(document, docBuilder, runList.get(i).getText()).getText());
} else {
docBuilder.insertCell();
docBuilder.getCellFormat().setWrapText(true);
docBuilder.write(formatRun(document, docBuilder, runList.get(i).getText()).getText());
}
}
docBuilder.endTable();

//Directory where you wish to save.
document.save("");
}

public Run formatRun(Document aDocument, DocumentBuilder aBuilder, String aString) {
Run run = new Run(aDocument);
run.getFont().setBold(aBuilder.getBold());
run.getFont().setItalic(aBuilder.getItalic());
run.getFont().setUnderline(aBuilder.getUnderline());
run.getFont().setSize(aBuilder.getFont().getSize());
run.setText(aString);
return run;
}

Hi Michael,


Thanks for the additional information.

I’m afraid, I still can’t reproduce any issue on my side using the latest version of Aspose.Words i.e. 11.1.0 and the code you provided. Moreover, I have attached DOC file i.e. generated on my side here for your reference. I would suggest you please upgrade to Aspose.Words v11.1.0.

I hope, this will help.

Best Regards,

Hello,

The problem is showing up in your out.doc as well. I may not have explained what my problem is well enough so I apologize for the misunderstanding. Under Individual where the Id’s are being added, the test should be on one line:

"ABC-def-ghi()"

As opposed to how it is now:

"ABC-def-
ghi(
)"

My problem is that even if I am using the wrapText() method and passing in false, the text still carries onto the next line. The document needs to be formatted with the Id’s on a single line regardless of how much information is to its right in the table.


Thanks,

Michael

Hi Michael,

Please read following documentation links for your kind reference. Please set width of table’s cell by using <span style=“font-size: 10pt; font-family: “Courier New”;”>PreferredWidth.fromPercent.

http://docs.aspose.com/display/wordsjava/AutoFitBehavior
http://docs.aspose.com/display/wordsjava/CellFormat
http://docs.aspose.com/display/wordsjava/Specifying+Table+and+Cell+Widths

I have modified your code, please see the following code snippet.
<!–[if gte mso 9]>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-US</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplexScript>AR-SA</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:DontVertAlignCellWithSp/>
<w:DontBreakConstrainedForcedTables/>
<w:DontVertAlignInTxbx/>
<w:Word11KerningPairs/>
<w:CachedColBalance/>
</w:Compatibility>
<w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
<m:mathPr>
<m:mathFont m:val=“Cambria Math”/>
<m:brkBin m:val=“before”/>
<m:brkBinSub m:val="–"/>
<m:smallFrac m:val=“off”/>
<m:dispDef/>
<m:lMargin m:val=“0”/>
<m:rMargin m:val=“0”/>
<m:defJc m:val=“centerGroup”/>
<m:wrapIndent m:val=“1440”/>
<m:intLim m:val=“subSup”/>
<m:naryLim m:val=“undOvr”/>
</m:mathPr></w:WordDocument>
<![endif]–><!–[if gte mso 10]>

/* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin-top:0cm; mso-para-margin-right:0cm; mso-para-margin-bottom:10.0pt; mso-para-margin-left:0cm; line-height:115%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:"Times New Roman"; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin;}

<![endif]–>

docBuilder.getCellFormat().getBorders().clearFormatting();

for (int i = 0; i < runList.size(); i++) {

if (i == 0) {

docBuilder.insertCell();

docBuilder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(20));

docBuilder.write(formatRun(document, docBuilder, runList.get(i).getText()).getText());

} else if (i % 2 == 0) {

docBuilder.endRow();

docBuilder.insertCell();

docBuilder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(20));

docBuilder.write(formatRun(document, docBuilder, runList.get(i).getText()).getText());

} else {

docBuilder.insertCell();

docBuilder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(80));

docBuilder.write(formatRun(document, docBuilder, runList.get(i).getText()).getText());

}

}

docBuilder.endTable();


Hope this helps you. Please let me know, If you have any more queries. I have attached the output file with this post. Also, please see the attached image for text wrapping concept with a table.

It appears as though it is giving me all the functionality I need with setting the preferred width with a percent. Thanks so much for the help. I really appreciate it.






Michael Platt

Hi Michael,


Thanks for your feedback. It is perfect that you managed to achieve what you were looking for. Please let us know any time you have any further queries.

Best Regards,