I'm using Aspose.HTML for .NET version 19.2 to render an HTML document as an image, using this example as a starting point.
The document I need to convert to an image is an organisational structure diagram, created by the Google Visualization API, which is rendered as a table. This table is the only element in the body of the HTML document, and there are no nested tables.
Due to different organisations having wildly differing structures, the table can vary in size from one or two row/columns up to 20 or so rows and several hundred columns.
What I would like to do is make an image of the entire table, which I can then scale to include in a PDF.
I've written a console app to test this, using the following c# code:
private const string Directory = "C:\\Temp\\Aspose";
static void Main(string[] args)
{
GenerateImage("SimpleTable.html");
GenerateImage("ComplexTable.html");
}
private static void GenerateImage(string htmlFilename)
{
using (var imageMemoryStream = new MemoryStream())
using (var imageDevice = new ImageDevice(new ImageRenderingOptions(), imageMemoryStream))
using (var htmlRenderer = new HtmlRenderer())
using (var htmlDocument = new HTMLDocument(Path.Combine(Directory, htmlFilename)))
{
htmlRenderer.Render(imageDevice, htmlDocument);
var image = Image.FromStream(imageMemoryStream);
image.Save(Path.Combine(Directory, $"Structure{DateTime.Now:yyyyMMddHHmmssfff}.png"), ImageFormat.Png);
}
}
I have three problems:
The image seems to be rendered from an imaginary viewport. The image of the simple table is much bigger than it needs to be and has small table at the top of the image with the rest of the image empty, while the image of the complex table has exactly the same dimensions but contains a slice of the table with the rest of it cut off. Is it possible to change this so that the image is sized appropriately for the page being rendered?
The text in the cells of the complex table spills out beyond the cells.
The CSS for rounded borders seems to be completely ignored.
I've created a zip file containing sample html for a simple and complex table.
The images look like this: