Increase Width/Height of Pages to Fit Wide HTML Table Columns in PNG Images

Hello,

We are using Aspose.Words v19.5 to convert an HTML document to PNG format. The HTML document contains a table. In the PNG, output 2 leftmost and 2 rightmost table columns have been trucated.

Following code is used to convert HTML to png:

Document doc = new Document("Inventory_List_Table.html");

DocumentBuilder builder = new DocumentBuilder(doc);
	
PageSetup pageSetup = builder.getPageSetup();
pageSetup.setLeftMargin(72d);
pageSetup.setRightMargin(72d);
pageSetup.setTopMargin(72d);
pageSetup.setBottomMargin(72d);

ImageSaveOptions options = new ImageSaveOptions(SaveFormat.PNG);
options.setJpegQuality(100);
options.setResolution(150);
options.setUseHighQualityRendering(true);

for (int i = 0; i < doc.getPageCount(); i++) {
	options.setPageIndex(i);
	doc.save("output" + (i+1) +".png", options);
}

Please find attached the source document and the png output:
attachments.zip (112.0 KB)

Can you please look into this.

Thanks,
Neha

@Neha_Gautam,

Please try to increase Width and Height inside PageSetup. This will fit wide table content inside PNG images. Hope, this helps.

Document doc = new Document("E:\\Temp\\Attachments\\Inventory_List_Table.html");

PageSetup pageSetup = doc.getFirstSection().getPageSetup();
// Specify JIS B3 paper size
pageSetup.setPageWidth(14.33 * 72);
pageSetup.setPageHeight(20.28 * 72);

ImageSaveOptions options = new ImageSaveOptions(SaveFormat.PNG);
options.setJpegQuality(100);
options.setResolution(150);
options.setUseHighQualityRendering(true);

for (int i = 0; i < doc.getPageCount(); i++) {
    options.setPageIndex(i);
    doc.save("E:\\Temp\\Attachments\\awjava-19.11-" + (i + 1) + ".png", options);
}

Hi,

Thanks for your reply.

We have a dynamic set of HTML documents and we cannot hardcode the page height and width. As you are suggesting to increase page width (14.33 * 72) and height (20.28 * 72), can you please let me know on what basis these values are calculated and will they work for all html documents?

Thanks,
Neha

@Neha_Gautam,

Please right click on your ‘Inventory_List_Table.html’ and choose ‘Open With’ and then ‘Word’ to open it in MS Word. You will see that the default Letter size Width/Height (8.5" x 11") is too narrow to fit the content of this HTML file. At this point, if you Save As the document to PDF, you will see incorrect result in PDF. By default, Aspose.Words mimics this behavior. Now, go to ‘Layout’ tab of MS Word, inside the ‘Page Setup’ section choose ‘JIS B3’ paper size to fit the content. At this point, if you Save As the document to PDF again, you will see the deisred result in PDF. So in this case, you need to adjust the page width/height accordingly.

Hi,

Our input HTML documents can come from multiple channels and we cannot open them all to figure out the height and width to be set for any particular HTML. We need to figure that out programatically. Is there any API provided by Aspose.Words that takes the HTML (or the Document object) as input and returns the appropriate height and width to be set during conversion to PNG?

Thanks,
Neha

@Neha_Gautam,

HTML documents can horizontally and vertically grow unlimitedly but MS Word documents have a maximum horizontal width and vertical height limit of 22 inches. The following code might help you to achieve what you are looking for:

Document doc = new Document("E:\\Temp\\Attachments\\Inventory_List_Table.html");

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

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

doc.updatePageLayout();

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

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

pageSetup.setPageWidth(maxWidth - pageSetup.getLeftMargin() - pageSetup.getRightMargin());

doc.updatePageLayout();

ImageSaveOptions options = new ImageSaveOptions(SaveFormat.PNG);
options.setJpegQuality(100);
options.setResolution(96);
options.setUseHighQualityRendering(true);

for (int i = 0; i < doc.getPageCount(); i++) {
    options.setPageIndex(i);
    doc.save("E:\\Temp\\Attachments\\awjava-19.12-" + (i + 1) + ".png", options);
}

Please note that Aspose.Words tries to mimic the behavior of MS Word. But, you may use Aspose.HTML for Java API to convert HTML files to PNG.

1 Like