Determining table width to conditionally autofit table to window

We are using Aspose Word to convert HTML to PDF. We could potentially have tables that are very large so we use the following -

table.autoFit(AutoFitBehavior.AUTO_FIT_TO_WINDOW);

in order to make them fit the PDF. However, we only want to do this if the table width is larger than the PDF width. In other cases, we want the table to either be LEFT ALIGNED or aligned as per the CSS of the HTML. Is there a way by which we can find out if the width of the table is going to be larger than A4 PDF? Could you please advise.

Thanks.

To determine if the width of a table exceeds the width of an A4 PDF page before applying the AUTO_FIT_TO_WINDOW option, you can follow these steps:

  1. Calculate the Table Width: You can use the GetWidth() method to retrieve the width of the table after it has been populated with content. This method will give you the width based on the current layout of the table.

  2. Compare with A4 Width: The width of an A4 page in points is approximately 595 points (8.27 inches). You can compare the width of your table against this value.

  3. Conditional Autofit: If the table width exceeds the A4 width, you can then apply the AUTO_FIT_TO_WINDOW behavior. Otherwise, you can leave the table aligned as per your CSS or set it to left-aligned.

Here is a sample code snippet in Java that demonstrates this logic:

import com.aspose.words.*;

public class TableWidthCheck {
    public static void main(String[] args) throws Exception {
        Document doc = new Document("your-document.docx");
        Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);

        // Calculate the width of the table
        double tableWidth = table.getWidth();

        // A4 width in points
        double a4Width = 595.0;

        // Check if the table width exceeds A4 width
        if (tableWidth > a4Width) {
            table.autoFit(AutoFitBehavior.AUTO_FIT_TO_WINDOW);
        } else {
            // Set alignment as per CSS or left aligned
            table.setAlignment(TableAlignment.LEFT);
        }

        doc.save("output-document.pdf");
    }
}

In this code, replace "your-document.docx" with the path to your actual document. This will ensure that the table is only autofitted to the window if it exceeds the A4 width, otherwise, it will be aligned as specified.

Feel free to ask if you have any further questions!

Hi

We dont seem to have table.getWidth() method in our API. The closest method we have is table.getPreferredWidth().

@gs01 You can use LayoutCollector and LayoutEnumerator to calculate actual width of the table. For example see the following code:

Document doc = new Document("C:\\Temp\\in.docx");

// Determine the maximum table width using LayoutEnumerator.
LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);

double maxTableWidth = 0;
for (Section s : doc.getSections())
{
    for (Table t : s.getBody().getTables())
    {
        enumerator.setCurrent(collector.getEntity(t.getFirstRow().getFirstCell().getFirstParagraph()));
        while (enumerator.getType() != LayoutEntityType.ROW)
            enumerator.moveParent();

        maxTableWidth = Math.max(maxTableWidth, enumerator.getRectangle().getWidth());
    }
}

PageSetup ps = doc.getFirstSection().getPageSetup();
double pageWidth = ps.getPageWidth() - ps.getLeftMargin() - ps.getRightMargin();
double pageHeight = ps.getPageHeight() - ps.getTopMargin() - ps.getBottomMargin();

if (pageWidth < maxTableWidth)
    ps.setPageWidth(maxTableWidth + ps.getLeftMargin() + ps.getRightMargin());

// Update page layout is required since LayoutCollector and LayoutEnumerator were used.
doc.updatePageLayout();
doc.save("C:\\Temp\\out.pdf");

Thanks for this. Is there a way to align the content of table cells to left or right?

@gs01 Alignment of cell content is defined by the paragraph alignment inside the cell. So you can use ParagraphFormat.Alignment property.