Question regarding creating Tables using DocumentBuilder

Hello,
I’m currently developing a TestDocument to see if Aspose can handle our requirements. I need to create complex tables including merged and splitted cells. So far it works good. I just have two questions.

  1. When creating a vertical merged cell the last cell in table doesn’t have a border (see attached document and source) anything I’m doing wrong?
  2. How can I set the table width to fill the whole page? It would be ok to get the maximum width I can set as the CellWidth’s to have the table fill the whole page?

Any help is appreciated!
Thx in advance!
Best regards
Jan Thewes

Hi Jan,
Thanks for considering Aspose.
1. Yes the basis of your code is correct, there were only a few changes needed to achieve the correct output. These changes stop the bottom line dissapearing and merge the cell vertically. I have made the changes to your code below.

Document doc = new Document();
DocumentBuilder docBuilder = new DocumentBuilder();
docBuilder.setDocument(doc);
docBuilder.getRowFormat().setAllowAutoFit(false);
docBuilder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
docBuilder.getCellFormat().setWidth(50);
docBuilder.startTable();
docBuilder.insertCell();
docBuilder.insertCell();
docBuilder.insertCell();
docBuilder.endRow();
docBuilder.getCellFormat().setWidth(75);
docBuilder.getCellFormat().getShading().setBackgroundPatternColor(Color.GRAY);
docBuilder.insertCell();
docBuilder.insertCell();
docBuilder.getCellFormat().getShading().clearFormatting();
docBuilder.endRow();
docBuilder.insertCell();
docBuilder.write("Merged Cell");
docBuilder.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
docBuilder.insertCell();
docBuilder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
docBuilder.endRow();
docBuilder.getCellFormat().setHorizontalMerge(CellMerge.NONE);
docBuilder.insertCell();
docBuilder.insertCell();
docBuilder.getCellFormat().setVerticalMerge(CellMerge.FIRST);
docBuilder.endRow();
docBuilder.insertCell();
docBuilder.insertCell();
docBuilder.getCellFormat().setVerticalMerge(CellMerge.PREVIOUS);
docBuilder.endRow();
docBuilder.getCellFormat().setVerticalMerge(CellMerge.NONE);
docBuilder.endTable();

2. Currently there is no API method to achieve this functionality in one line of code. We will look into implementing this in the future and will keep you informed of any developments (through a message on this thread when this feature is avaliable).
In the mean time it is easy to achieve this functionality manually using the Aspose.Words API. I have put together a quick implementation below which takes a table passed to the method and fits it to the page width, while keeping the cell sizes of each row in proportion. Please find the method below.

public static void fitTableToPageWidth(Table table) throws Exception
{
    // Get the page settings from the section where this table occurs, as each section can have different page settings.
    Section section = (Section) table.getAncestor(NodeType.SECTION);

    // First run simply gets the table size (the widest row). This is used to calculate the ratio below instead of just each row length
    // as the last cell in one row could be shorter than the last cell in the other row. This will preserve these different sizes when fitting.
    double tableWidth = 0;
    for (Row row: table.getRows())
    {
        double rowWidth = 0;
        for (Cell cell: row.getCells())
        {
            rowWidth += cell.getCellFormat().getWidth();
        }
        // If this row is larger than previous set this width as the longest row.
        if (rowWidth> tableWidth)
            tableWidth = rowWidth;
    }
    // Calculate the width of the page
    double pageWidth = section.getPageSetup().getPageWidth() - (section.getPageSetup().getLeftMargin() + section.getPageSetup().getRightMargin());
    // In the second run set each cell in the row proportionally to the width of the page
    for (Row row: table.getRows())
    {
        for (Cell cell: row.getCells())
        {
            // Calculate the ratio of each cell to the row width and then translate this ratio to the page width.
            double cellRatio = cell.getCellFormat().getWidth() / tableWidth;
            cell.getCellFormat().setWidth(cellRatio * pageWidth);
        }
    }
}

To use this method you can simply change one of the lines in your main code to get the table object and then pass it to the method above:

Table table = docBuilder.startTable();
// Rest of code here
fitTableToPageWidth(table);

If you have any other queries please feel free to ask.
Thanks,

Hey thanks for that fast answer.
I managed to get it all propably working. Looks really great so far. Just one more question.
Cann you give me an example where a table with one row and two columns is inserted where the first column has NO border and the second one has a single line? That would be so great. I havn’t managed to disable the border for the first cell in the table and activate it for all the others!
Thanks so far!

Hi Jan,
Sure, please see the code below. It will insert a table with one row and two cells, the first having no borders and the second having all borders with single line.

Document doc = new Document();
DocumentBuilder docBuilder = new DocumentBuilder(doc);
docBuilder.getCellFormat().setWidth(50);
docBuilder.startTable();
docBuilder.insertCell();
docBuilder.getCellFormat().getBorders().setLineStyle(LineStyle.NONE);
docBuilder.insertCell();
docBuilder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
docBuilder.endRow();

Hopefully this was what you were after, if it is not could you please post an example here and I will take a closer look.
Thanks,

Hey,

thanks a lot.
It’s fun to work with you! Your support is incredible fast and the quality of the answers is great. Just one more thing. Is there a possibility to enable hyphenation in the document? I havn’t figured out how i can enable hyphenation using the Aspose API?

Thx for your help!

With best regards!
https://dict.leo.org/englisch-deutsch/hyphenation

Hi

Thanks for your inquiry. Unfortunately, there is no way to enable/disable this option for whole document. Currently you can only disable hyphenation for a particular paragraph:
https://reference.aspose.com/words/net/aspose.words/paragraphformat/suppressautohyphens/
If you are building document from scratch, you can create an empty document in MS Word, enable auto-hyphenation in this document and use it as template for your documents.
Best regards,

The issues you have found earlier (filed as WORDSNET-352) have been fixed in this .NET update and in this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(67)

The issues you have found earlier (filed as WORDSNET-2338) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.

Hi,

It is to update you that we have made a new HyphenationOptions class public. HyphenationOptions instance is available as a readonly property on the Document class and contains the following properties to configure document hyphenation options:

///
/// Gets or sets value determining whether automatic hyphenation is turned on for the document.
/// Default value for this property is false.
///
public bool AutoHyphenation
{
    get;
    set;
}
///
/// Gets or sets the maximum number of consecutive lines that can end with hyphens.
/// Default value for this property is 0.
///
///
/// If value of this property is set to 0, any number of consecutive lines can end with hyphens.
/// The property does not have effect when saving to fixed page formats e.g. PDF.
///
public int ConsecutiveHyphenLimit
{
    get;
    set;
}
///
/// Gets or sets the distance in 1/20 of a point from the right margin within which you do not want
/// to hyphenate words.
/// Default value for this property is 360 (0.25 inch).
///
public int HyphenationZone
{
    get;
    set;
}
///
/// Gets or sets value determining whether words written in all capital letters are hyphenated.
/// Default value for this property is true.
///
public bool HyphenateCaps
{
    get;
    set;
}

Here is sample usage:

Document doc = new Document();
doc.getHyphenationOptions().setAutoHyphenation(true);
doc.getHyphenationOptions().setConsecutiveHyphenLimit(2);
doc.getHyphenationOptions().setHyphenationZone(720); // 0.5 inch
doc.getHyphenationOptions().setHyphenateCaps(false);

Hope, this helps.

Best regards,