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("<span style=" font - size:10 pt;
        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.

https://reference.aspose.com/words/java/com.aspose.words/AutoFitBehavior
https://reference.aspose.com/words/java/com.aspose.words/CellFormat
https://docs.aspose.com/words/java/specifying-table-and-cell-widths/

I have modified your code, please see the following code snippet.

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,