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,
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]>
<![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,
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]>
<![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,