Free Support Forum - aspose.com

Table size identification and accordingly change the page size

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");
    }
}