Free Support Forum - aspose.com

Calculate Table Width & Size in Word Document & Adjust Page Setup Width Size accordingly using Java

Hi,

My requirement is to identify the table size, and accordingly have to change the page size.

I am inserting html contents that contain tables as well as text, on document . When the tables’ size are big then it’s overflowing from page but as per my requirement i want to detect the table size and if it is more than current particular page’s size then set the new page size for that particular page and place the table .

if table size is less than or equal to page size , i want to retain the table and page as it is.

I am attaching a sample doc for your reference (This is how my generated document will look a like).

Please do let me know for any further info.

Thank you…
table_requirement.zip (11.1 KB)

@Pintutrnt,

Please ZIP and upload your input HTML file, Aspose.Words generated DOCX document showing the undesired behavior and your expected document showing the desired output here for testing. Please create expected document by using MS Word and attach it here for our reference. We will then investigate the issue on our end and provide you more information.

@awais.hafeez As requested i have attached the sample code , input & output documents And expected documents. In source code , i have used one token named “agTitles” (case “agTitles” ) . This token will print the html content details (that content may or may not contain small or big table(s)).

Please do let me know for any info.

Thank you…
samplecode_and_documents.zip (34.5 KB)

@Pintutrnt,

HTML documents/strings can horizontally and vertically grow unlimitedly but MS Word documents have a maximum horizontal width and vertical height limit of 22 inches. The table in your HTML is so big that it even does not fit inside 22 inches wide Word document. So, you first need to set page width to maximum like this and then use InsertHtml method.

PageSetup pageSetup = doc.getFirstSection().getPageSetup();
// Specify maximum allowed paper size i.e. 22 inches
pageSetup.setPageWidth(22 * 72);
pageSetup.setPageHeight(22 * 72);

You can also estimate the width of Table by using the following code:

double maxWidth = 0;
for (Paragraph para : (Iterable<Paragraph>) targetTable.getChildNodes(NodeType.PARAGRAPH, true)) {
    enumerator.setCurrent(collector.getEntity(para));
    if ((enumerator.getRectangle().getX() + enumerator.getRectangle().width > maxWidth))
        maxWidth = enumerator.getRectangle().getX() + enumerator.getRectangle().width;
}

You can also try using the targetTable.autoFit(AutoFitBehavior.AUTO_FIT_TO_WINDOW); to fit table with the Page bounds. Or adjust the widths of individual Cells by using CellFormat.Width Property:

@awais.hafeez,

Thank you for your reply. I have one more query :

How will we get to know that this particular HTML string contains bigger table (since whatever the code i shared to you that contains the bigger table at index 1 of the given list but in our real data it can be at any index of the list. once I will get to know the index or location or any info about bigger table at run time , i can go ahead and use the code that you shared to set up the page width and height for that particular page)?

Thank you…

@Pintutrnt,

I think, you can meet this requirement by using the following code:

Document doc = new Document("E:\\Temp\\samplecode_and_documents\\awjava-20.1.docx");

LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);

double maxWidth = 0;
Paragraph targetPara = null;
for (Table targetTable : (Iterable<Table>) doc.getChildNodes(NodeType.TABLE, true)) {
    for (Paragraph para : (Iterable<Paragraph>) targetTable.getChildNodes(NodeType.PARAGRAPH, true)) {
        enumerator.setCurrent(collector.getEntity(para));
        if ((enumerator.getRectangle().getX() + enumerator.getRectangle().width > maxWidth)) {
            maxWidth = enumerator.getRectangle().getX() + enumerator.getRectangle().width;
            targetPara = para;
        }
    }
}

if (targetPara != null) {
    Table tab = (Table) targetPara.getAncestor(NodeType.TABLE);
    if (tab != null) {
        System.out.println("This table occupies maximum width which is " + maxWidth / 72 + " inches");
    }
}

@awais.hafeez,

The code you shared is working but its getting applied to all the pages of document means all pages width and height are becoming 22 *72. But , as per my requirement , modified page set up height and width should applied to that page(s) which contains the bigger table( than A4 size) and other pages should of default size (A4 Size) .

@Pintutrnt,

You can get parent Section of target Table and just set its Width and Height.

DocumentBuilder builder = new DocumentBuilder(doc);

Table targetTable = ...
Section section =  (Section) targetTable.getAncestor(NodeType.SECTION);

After that insert a section break and adjust width/height of remaining pages:

builder.moveTo(targetTable.getNextSibling());
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);

Section newSec = builder.getCurrentSection();
// adjust width/height accordingly

Hope, this helps in achieving what you are looking for.

@awais.hafeez,

The code you shared above is working but this logic is making all the pages of document of bigger size from the place of bigger table. But as per my requirement , i have the make those pages to be of bigger size (22*72) who are having bigger table . rest other pages size will be default size means not modified one.

Below is the code that i am trying but i not able to set the olderSection (or content of olderSection) back to document.

NodeCollection

tables = document.getChildNodes(NodeType.TABLE, true);
Iterator
tabIterator = tables.iterator();
while (tabIterator.hasNext()) {
Table table = tabIterator.next();
double currentPgHeight = 0;
double currentPgWidth = 0;
DocumentBuilder docbuilder = new DocumentBuilder(document);
Section oldSec = docbuilder.getCurrentSection();
Section section = (Section) table.getAncestor(NodeType.SECTION);
Section newSec = docbuilder.getCurrentSection();
for (Paragraph para : (Iterable) table.getChildNodes(NodeType.PARAGRAPH, true)) {
enumerator.setCurrent(collector.getEntity(para));
double pageWidthA4Size = 8.322;
double pageHeightA4Size = 11.7
22;
double left = enumerator.getRectangle().getX();
if (enumerator.getRectangle().width > (pageWidthA4Size - left)) {
docbuilder.moveTo(table.getNextSibling());
docbuilder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
PageSetup pageSetup = docbuilder.getPageSetup();
currentPgHeight = pageSetup.getPageHeight();
currentPgWidth = pageSetup.getPageWidth();
pageSetup.setPageWidth(22 * 72);
pageSetup.setPageHeight(22 * 72);
targetParas.add(para);
}
}
table.autoFit(AutoFitBehavior.AUTO_FIT_TO_WINDOW);
//from here i want to retain the older pageSetup.
docbuilder.moveToCell(tables.indexOf(table),table.indexOf(table.getLastRow()),table.getLastRow().indexOf(table.getLastRow().getLastCell()),0);
docbuilder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS);
document.getLastSection().appendContent(oldSec);
docbuilder.getCurrentSection().getPageSetup().setPageHeight(currentPgHeight);
docbuilder.getCurrentSection().getPageSetup().setPageWidth(currentPgWidth);
}

can you please help me ASAP.

Thank you !!

@Pintutrnt,

We are working on your query and will get back to you soon.

@awais.hafeez,

can you please let me know the status.

@Pintutrnt,

Please see these input/output Word documents (Docs.zip (18.5 KB)) and try running the following code:

Document doc = new Document("E:\\Temp\\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

Table targetTable = doc.getFirstSection().getBody().getTables().get(0);

Paragraph prevPara = (Paragraph) targetTable.getPreviousSibling();
Paragraph nextPara = (Paragraph) targetTable.getNextSibling();

builder.moveTo(prevPara);
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);

builder.moveTo(nextPara.getFirstChild());
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);

Section section =  (Section) targetTable.getAncestor(NodeType.SECTION);
PageSetup pageSetup = section.getPageSetup();
pageSetup.setPageWidth(22 * 72);
pageSetup.setPageHeight(22 * 72);

doc.save("E:\\Temp\\awjava-20.1.docx");

The code separates target Table into its own Section and sets height/width of that Section. Hope, this helps in achieving what you are looking for.